<commit_before><?php
namespace Redbox\Cli\Arguments;

/**
 * This class will parse the given arguments.
 *
 * @package Redbox\Cli\Arguments
 */
class Parser
{
    /**
     * @var \Redbox\Cli\Arguments\Filter $filter;
     */
    protected $filter;

    /**
     * @var \Redbox\Cli\Arguments\Manager $manager;
     */
    protected $manager;

    /**
     * Parser constructor.
     * @var array $arguments;
     */
    protected $arguments;

    /**
     * Parser constructor.
     * @param Manager $manager
     */
    public function __construct(Manager $manager = NULL)
    {
        $this->manager = $manager;
    }

    /**
     * Set the filter for the parser.
     *
     * @param Filter $filter
     * @param array $arguments
     */
    public function setFilter(Filter $filter, array $arguments = [])
    {
        $this->filter    = $filter;
        $this->arguments = $arguments;
        $this->filter->setArguments($arguments);
    }

    /**
     * Return the script name.
     *
     * @return mixed
     */
    public function getCommand()
    {
        global $argv;
        return $argv[0];
    }

    /**
     * This is it, we parse the given arguments.
     *
     * @throws \Exception
     */
    public function parse()
    {
        $requiredArguments = $this->filter->required();
        list($shortOptions, $longOptions) = $this->buildOptions();

        $results = getopt($shortOptions, $longOptions);

        foreach ($this->arguments as $argument) {
            $name  = $argument->name;
            $value = '';

            if (isset($results[$argument->prefix]) || isset($results[$argument->longPrefix])) {
                $value = isset($results[$argument->prefix]) ? $results[$argument->prefix] : $results[$argument->longPrefix];
            } else {
                /**
                 * If we set the default value for this argument we also add it to
                 * the result array or it will fail the argument has the option required by mistake.
                 */
                if ($argument->defaultValue) {
                    $value = $argument->defaultValue;
                    $this->manager->setHasDefaultValue($name, true);
                    $results[$argument->name] = $value;
                }
            }
            $this->manager->set($name, $value);
        }
        foreach ($requiredArguments as $argument) {
            if (isset($results[$argument->prefix]) === false  && isset($results[$argument->longPrefix]) === false) {
                throw new \Exception(
                    'The following arguments are required: '
                    .print_r($argument->name, true).'.'
                );
            }
        }
    }

    /**
     * Build the option arrays that could be used with getopt()
     *
     * @return array
     */
    private function buildOptions()
    {
        $short_prefixes = $this->filter->withShortPrefix();
        $long_prefixes  = $this->filter->withLongPrefix();

        $short = '';
        $long  = array();

        foreach ($short_prefixes as $argument) {
            $short .= $argument->prefix;
            $short .= ($argument->required == true) ? ':' : '::';
        }

        foreach ($long_prefixes as $argument) {
            $rule  = $argument->longPrefix;
            $rule .= ($argument->required == true) ? ':' : '::';
            $long[] = $rule;
        }
        return [$short, $long];
    }
}<commit_msg>redbox-cli-21 Speed up the code<commit_after><?php
namespace Redbox\Cli\Arguments;

/**
 * This class will parse the given arguments.
 *
 * @package Redbox\Cli\Arguments
 */
class Parser
{
    /**
     * @var \Redbox\Cli\Arguments\Filter $filter;
     */
    protected $filter;

    /**
     * @var \Redbox\Cli\Arguments\Manager $manager;
     */
    protected $manager;

    /**
     * Parser constructor.
     * @var array $arguments;
     */
    protected $arguments;

    /**
     * Parser constructor.
     * @param Manager $manager
     */
    public function __construct(Manager $manager = NULL)
    {
        $this->manager = $manager;
    }

    /**
     * Set the filter for the parser.
     *
     * @param Filter $filter
     * @param array $arguments
     */
    public function setFilter(Filter $filter, array $arguments = [])
    {
        $this->filter    = $filter;
        $this->arguments = $arguments;
        $this->filter->setArguments($arguments);
    }

    /**
     * Return the script name.
     *
     * @return mixed
     */
    public function getCommand()
    {
        global $argv;
        return $argv[0];
    }

    /**
     * This is it, we parse the given arguments.
     *
     * @throws \Exception
     */
    public function parse()
    {
        $requiredArguments = $this->filter->required();
        list($shortOptions, $longOptions) = $this->buildOptions();

        $results = getopt($shortOptions, $longOptions);

        foreach ($this->arguments as $argument) {
            $name  = $argument->name;
            $value = '';

            if (isset($results[$argument->prefix]) || isset($results[$argument->longPrefix])) {
                $value = isset($results[$argument->prefix]) ? $results[$argument->prefix] : $results[$argument->longPrefix];
            } else {
                /**
                 * If we set the default value for this argument we also add it to
                 * the result array or it will fail the argument has the option required by mistake.
                 */
                if ($argument->defaultValue) {
                    $value = $argument->defaultValue;
                    $this->manager->setHasDefaultValue($name, true);
                    $results[$argument->name] = $value;
                } else {
                    throw new \Exception(
                        'The following arguments are required: '
                        .print_r($argument->name, true).'.'
                    );
                }
            }
            $this->manager->set($name, $value);
        }
        /*
        foreach ($requiredArguments as $argument) {
            if (isset($results[$argument->prefix]) === false  && isset($results[$argument->longPrefix]) === false) {
                throw new \Exception(
                    'The following arguments are required: '
                    .print_r($argument->name, true).'.'
                );
            }
        }
        */
    }

    /**
     * Build the option arrays that could be used with getopt()
     *
     * @return array
     */
    private function buildOptions()
    {
        $short_prefixes = $this->filter->withShortPrefix();
        $long_prefixes  = $this->filter->withLongPrefix();

        $short = '';
        $long  = array();

        foreach ($short_prefixes as $argument) {
            $short .= $argument->prefix;
            $short .= ($argument->required == true) ? ':' : '::';
        }

        foreach ($long_prefixes as $argument) {
            $rule  = $argument->longPrefix;
            $rule .= ($argument->required == true) ? ':' : '::';
            $long[] = $rule;
        }
        return [$short, $long];
    }
}
{'fraction_non_alphanumeric': 0.13669650122050447, 'fraction_numerical': 0.0005424464334147003, 'mean_word_length': 1.5256849315068493, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 70, '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': '1172272', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1874, 'n_words': 451}