<commit_before><?php

namespace phparsenal\fastforward\Command;

use phparsenal\fastforward\Client;
use phparsenal\fastforward\Model\Setting;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Save and list settings
 */
class Set extends InteractiveCommand
{
    private $client;

    public function __construct(Client $client)
    {
        parent::__construct();
        $this->client = $client;
    }

    /**
     * Configures the current command.
     */
    protected function configure()
    {
        $this->setName('set')
            ->setDescription('Set or get variables')
            ->addOption('list', 'l', InputOption::VALUE_NONE, 'Show a list of all current settings.')
            ->addOption('import-file', 'f', InputOption::VALUE_REQUIRED, 'Import from the specified file or STDIN')
            ->addOption('import-stdin', 'i', InputOption::VALUE_NONE, 'Import setting via STDIN pipe')
            ->addArgument('key', InputArgument::OPTIONAL, 'Name or key of the setting')
            ->addArgument('value', InputArgument::OPTIONAL, 'Value to be set');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if ($input->getOption('list')) {
            $this->listAll($output);
            return;
        }
        $importFile = $input->getOption('import-file');
        if ($importFile !== null) {
            $this->addLines($this->getLinesFile($importFile), $output);
            return;
        }
        if ($input->getOption('import-stdin')) {
            $this->addLines($this->getLinesStdin(), $output);
            return;
        }

        $key = $input->getArgument('key');
        $value = $input->getArgument('value');
        if ($key !== null) {
            if ($value !== null) {
                $this->client->set($key, $value);
            } else {
                $this->client->getSettings()->showSupportedSettings($key);
            }
        }
    }

    protected function interact(InputInterface $input, OutputInterface $output)
    {
        if ($input->getOption('list')
            || $input->getOption('import-stdin')
            || $input->getOption('import-file') !== null
        ) {
            return;
        }
        parent::interact($input, $output); // TODO: Change the autogenerated stub
    }

    /**
     * @param OutputInterface $output
     */
    public function listAll(OutputInterface $output)
    {
        $settings = Setting::select()->orderAsc('key')->all();
        foreach ($settings as $setting) {
            $output->writeln($setting->key . ' ' . $setting->value);
        }
    }

    /**
     * @return array
     */
    private function getLinesStdin()
    {
        $h = fopen('php://stdin', 'r');
        $lines = array();
        while (!feof($h)) {
            $lines[] = fgets($h);
        }
        fclose($h);
        return $lines;
    }

    /**
     * @param $filepath
     *
     * @return array
     *
     * @throws \Exception
     */
    private function getLinesFile($filepath)
    {
        if (!file_exists($filepath)) {
            throw new \RuntimeException("Could not find file '{$filepath}'");
        }
        $lines = file($filepath);
        return $lines;
    }

    /**
     * @param $lines
     * @param OutputInterface $output
     */
    private function addLines($lines, OutputInterface $output)
    {
        foreach ($lines as $line) {
            if (preg_match('/^([^ ]+) (.*)/', $line, $matches)) {
                $this->client->set($matches[1], $matches[2]);
            } elseif (trim($line) !== '') {
                $output->writeln('Line ignored: ' . $line);
            }
        }
    }
}
<commit_msg>Throw exception when set command was called without args non-interactively<commit_after><?php

namespace phparsenal\fastforward\Command;

use phparsenal\fastforward\Client;
use phparsenal\fastforward\Model\Setting;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Save and list settings
 */
class Set extends InteractiveCommand
{
    private $client;

    public function __construct(Client $client)
    {
        parent::__construct();
        $this->client = $client;
    }

    /**
     * Configures the current command.
     */
    protected function configure()
    {
        $this->setName('set')
            ->setDescription('Set or get variables')
            ->addOption('list', 'l', InputOption::VALUE_NONE, 'Show a list of all current settings.')
            ->addOption('import-file', 'f', InputOption::VALUE_REQUIRED, 'Import from the specified file or STDIN')
            ->addOption('import-stdin', 'i', InputOption::VALUE_NONE, 'Import setting via STDIN pipe')
            ->addArgument('key', InputArgument::OPTIONAL, 'Name or key of the setting')
            ->addArgument('value', InputArgument::OPTIONAL, 'Value to be set');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if ($input->getOption('list')) {
            $this->listAll($output);
            return;
        }
        $importFile = $input->getOption('import-file');
        if ($importFile !== null) {
            $this->addLines($this->getLinesFile($importFile), $output);
            return;
        }
        if ($input->getOption('import-stdin')) {
            $this->addLines($this->getLinesStdin(), $output);
            return;
        }

        $key = $input->getArgument('key');
        $value = $input->getArgument('value');
        if ($key !== null && $value !== null) {
            $this->client->set($key, $value);
            return;
        }
        if ($key !== null) {
            $this->client->getSettings()->showSupportedSettings($key);
            return;
        }

        throw new \RuntimeException('Calling this command without arguments can only be done interactively.');
    }

    protected function interact(InputInterface $input, OutputInterface $output)
    {
        if ($input->getOption('list')
            || $input->getOption('import-stdin')
            || $input->getOption('import-file') !== null
        ) {
            return;
        }
        parent::interact($input, $output); // TODO: Change the autogenerated stub
    }

    /**
     * @param OutputInterface $output
     */
    public function listAll(OutputInterface $output)
    {
        $settings = Setting::select()->orderAsc('key')->all();
        foreach ($settings as $setting) {
            $output->writeln($setting->key . ' ' . $setting->value);
        }
    }

    /**
     * @return array
     */
    private function getLinesStdin()
    {
        $h = fopen('php://stdin', 'r');
        $lines = array();
        while (!feof($h)) {
            $lines[] = fgets($h);
        }
        fclose($h);
        return $lines;
    }

    /**
     * @param $filepath
     *
     * @return array
     *
     * @throws \Exception
     */
    private function getLinesFile($filepath)
    {
        if (!file_exists($filepath)) {
            throw new \RuntimeException("Could not find file '{$filepath}'");
        }
        $lines = file($filepath);
        return $lines;
    }

    /**
     * @param $lines
     * @param OutputInterface $output
     */
    private function addLines($lines, OutputInterface $output)
    {
        foreach ($lines as $line) {
            if (preg_match('/^([^ ]+) (.*)/', $line, $matches)) {
                $this->client->set($matches[1], $matches[2]);
            } elseif (trim($line) !== '') {
                $output->writeln('Line ignored: ' . $line);
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.1533375715193897, 'fraction_numerical': 0.000508582326764145, 'mean_word_length': 2.1577679646728223, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2087569', 'n_tokens_mistral': 2212, 'n_tokens_neox': 2184, 'n_words': 466}