<commit_before><?php

namespace CoRex\Command;

class Handler
{
    const TITLE = 'CoRex Command.';
    private $arguments;
    private $component;
    private $command;
    private $isHelp;
    private $hideInternal;
    private $throughComposer;
    private $indentLength;

    /**
     * Arguments from CLI.
     *
     * Handler constructor.
     * @param array $arguments
     * @param boolean $showInternalCommands
     * @param boolean $throughComposer
     * @param integer $indentLength Default 30.
     */
    public function __construct(array $arguments, $showInternalCommands, $throughComposer, $indentLength = 30)
    {
        $this->indentLength = $indentLength;
        if (isset($arguments[0])) {
            unset($arguments[0]);
            $arguments = array_values($arguments);
        }
        $this->component = '';
        $this->command = '';
        $this->isHelp = false;
        if (isset($arguments[0]) && strtolower($arguments[0]) == 'help') {
            $this->isHelp = true;
            unset($arguments[0]);
            $arguments = array_values($arguments);
        }
        $componentCommand = '';
        if (isset($arguments[0])) {
            $componentCommand = $arguments[0];
            unset($arguments[0]);
        }
        $argumentParts = $this->splitArgument($componentCommand);
        $this->component = $argumentParts['component'];
        $this->command = $argumentParts['command'];
        $this->arguments = array_values($arguments);

        // Scan for internal commands.
        $this->hideInternal = !$showInternalCommands;
        $this->registerOnPath(__DIR__);
        $this->throughComposer = $throughComposer;
    }

    /**
     * Register command-class.
     *
     * @param string $class
     * @throws \Exception
     */
    public function register($class)
    {
        SignatureHandler::register($class, $this->hideInternal);
    }

    /**
     * Register all classes in path and sub-path.
     *
     * @param string $path
     */
    public function registerOnPath($path)
    {
        $path = str_replace('\\', '/', $path);
        if (strlen($path) > 0 && substr($path, -1) == '/') {
            $path = rtrim($path, '//');
        }
        if (!is_dir($path)) {
            return;
        }
        $files = scandir($path);
        if (count($files) == 0) {
            return;
        }
        $commandSuffix = 'Command.php';
        foreach ($files as $file) {
            if (substr($file, 0, 1) == '.') {
                continue;
            }
            if (substr($file, -strlen($commandSuffix)) == $commandSuffix) {
                $class = $this->extractFullClass($path . '/' . $file);
                if ($class != '') {
                    $this->register($class);
                }
            }
            if (is_dir($path . '/' . $file)) {
                $this->registerOnPath($path . '/' . $file);
            }
        }
    }

    /**
     * Execute command.
     *
     * @param string $component Default ''.
     * @param string $command Default ''.
     * @return boolean
     * @throws \Exception
     */
    public function execute($component = '', $command = '')
    {
        if ($component != '' && $command != '') {
            $this->component = $component;
            $this->command = $command;
        }
        $signature = SignatureHandler::getSignature($this->component, $this->command);

        if ($this->component == '' && $signature === null) {
            Console::header(self::TITLE);
            Console::title('Usage:');
            Console::writeln('  {component} {command} [options] [arguments]');
            Console::writeln('');
            Console::writeln('  To show help for command: help {component} {command}');
            Console::writeln('');
        }

        if ($this->isHelp) {
            $this->show($this->component, $this->command);
            return false;
        }

        if ($signature === null) {
            if ($this->command != '') {
                Console::header(self::TITLE);
                Console::throwError(
                    'Command ' . $this->command . ' in component ' . $this->component . ' does not exist.'
                );
            }
            $this->showAll($this->component);
            return false;
        }

        // Execute command.
        $class = $signature['class'];
        if (!in_array('setProperties', get_class_methods($class))) {
            Console::throwError($class . ' does not extend CoRex\Command\BaseCommand.');
        }
        SignatureHandler::call($this->component, $this->command, $this->arguments, false, $this->throughComposer);

        return true;
    }

    /**
     * Show all commands.
     *
     * @param string $component
     * @throws \Exception
     */
    public function showAll($component = '')
    {
        if ($component != '') {
            if (!SignatureHandler::componentExist($component)) {
                Console::throwError('Component not found: ' . $component);
            }
        } else {
            Console::title('Available commands:');
        }
        $components = SignatureHandler::getComponents();
        if (count($components) > 0) {
            foreach ($components as $componentName) {
                if ($component != '' && $componentName != $component) {
                    continue;
                }
                if (!SignatureHandler::isComponentVisible($componentName)) {
                    continue;
                }
                Console::title('  ' . $componentName);
                $commands = SignatureHandler::getCommands($componentName);
                foreach ($commands as $command => $properties) {
                    if (!$properties['visible']) {
                        continue;
                    }
                    if ($componentName != '') {
                        Console::write('    ' . $componentName . ':' . $command, $this->indentLength);
                    } else {
                        Console::write('  ' . $command, $this->indentLength);
                    }
                    Console::writeln($properties['description']);
                }
            }
        }
    }

    /**
     * Show command.
     *
     * @param string $component
     * @param string $command
     * @throws \Exception
     */
    public function show($component, $command)
    {
        Console::header(self::TITLE);
        if ($component == '') {
            Console::throwError('Component not specified.');
        }
        if (!SignatureHandler::componentExist($component)) {
            Console::throwError('Component not found: ' . $component);
        }
        if ($command == '') {
            Console::throwError('Command not specified.');
        }
        $signature = SignatureHandler::getSignature($component, $command);
        if ($signature === null) {<commit_msg>Added method to set title.<commit_after><?php

namespace CoRex\Command;

class Handler
{
    private $title = 'CoRex Command.';
    private $arguments;
    private $component;
    private $command;
    private $isHelp;
    private $hideInternal;
    private $throughComposer;
    private $indentLength;

    /**
     * Arguments from CLI.
     *
     * Handler constructor.
     * @param array $arguments
     * @param boolean $showInternalCommands
     * @param boolean $throughComposer
     * @param integer $indentLength Default 30.
     */
    public function __construct(array $arguments, $showInternalCommands, $throughComposer, $indentLength = 30)
    {
        $this->indentLength = $indentLength;
        if (isset($arguments[0])) {
            unset($arguments[0]);
            $arguments = array_values($arguments);
        }
        $this->component = '';
        $this->command = '';
        $this->isHelp = false;
        if (isset($arguments[0]) && strtolower($arguments[0]) == 'help') {
            $this->isHelp = true;
            unset($arguments[0]);
            $arguments = array_values($arguments);
        }
        $componentCommand = '';
        if (isset($arguments[0])) {
            $componentCommand = $arguments[0];
            unset($arguments[0]);
        }
        $argumentParts = $this->splitArgument($componentCommand);
        $this->component = $argumentParts['component'];
        $this->command = $argumentParts['command'];
        $this->arguments = array_values($arguments);

        // Scan for internal commands.
        $this->hideInternal = !$showInternalCommands;
        $this->registerOnPath(__DIR__);
        $this->throughComposer = $throughComposer;
    }

    /**
     * Set title.
     *
     * @param string $title
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     * Register command-class.
     *
     * @param string $class
     * @throws \Exception
     */
    public function register($class)
    {
        SignatureHandler::register($class, $this->hideInternal);
    }

    /**
     * Register all classes in path and sub-path.
     *
     * @param string $path
     */
    public function registerOnPath($path)
    {
        $path = str_replace('\\', '/', $path);
        if (strlen($path) > 0 && substr($path, -1) == '/') {
            $path = rtrim($path, '//');
        }
        if (!is_dir($path)) {
            return;
        }
        $files = scandir($path);
        if (count($files) == 0) {
            return;
        }
        $commandSuffix = 'Command.php';
        foreach ($files as $file) {
            if (substr($file, 0, 1) == '.') {
                continue;
            }
            if (substr($file, -strlen($commandSuffix)) == $commandSuffix) {
                $class = $this->extractFullClass($path . '/' . $file);
                if ($class != '') {
                    $this->register($class);
                }
            }
            if (is_dir($path . '/' . $file)) {
                $this->registerOnPath($path . '/' . $file);
            }
        }
    }

    /**
     * Execute command.
     *
     * @param string $component Default ''.
     * @param string $command Default ''.
     * @return boolean
     * @throws \Exception
     */
    public function execute($component = '', $command = '')
    {
        if ($component != '' && $command != '') {
            $this->component = $component;
            $this->command = $command;
        }
        $signature = SignatureHandler::getSignature($this->component, $this->command);

        if ($this->component == '' && $signature === null) {
            Console::header($this->title);
            Console::title('Usage:');
            Console::writeln('  {component} {command} [options] [arguments]');
            Console::writeln('');
            Console::writeln('  To show help for command: help {component} {command}');
            Console::writeln('');
        }

        if ($this->isHelp) {
            $this->show($this->component, $this->command);
            return false;
        }

        if ($signature === null) {
            if ($this->command != '') {
                Console::header(self::TITLE);
                Console::throwError(
                    'Command ' . $this->command . ' in component ' . $this->component . ' does not exist.'
                );
            }
            $this->showAll($this->component);
            return false;
        }

        // Execute command.
        $class = $signature['class'];
        if (!in_array('setProperties', get_class_methods($class))) {
            Console::throwError($class . ' does not extend CoRex\Command\BaseCommand.');
        }
        SignatureHandler::call($this->component, $this->command, $this->arguments, false, $this->throughComposer);

        return true;
    }

    /**
     * Show all commands.
     *
     * @param string $component
     * @throws \Exception
     */
    public function showAll($component = '')
    {
        if ($component != '') {
            if (!SignatureHandler::componentExist($component)) {
                Console::throwError('Component not found: ' . $component);
            }
        } else {
            Console::title('Available commands:');
        }
        $components = SignatureHandler::getComponents();
        if (count($components) > 0) {
            foreach ($components as $componentName) {
                if ($component != '' && $componentName != $component) {
                    continue;
                }
                if (!SignatureHandler::isComponentVisible($componentName)) {
                    continue;
                }
                Console::title('  ' . $componentName);
                $commands = SignatureHandler::getCommands($componentName);
                foreach ($commands as $command => $properties) {
                    if (!$properties['visible']) {
                        continue;
                    }
                    if ($componentName != '') {
                        Console::write('    ' . $componentName . ':' . $command, $this->indentLength);
                    } else {
                        Console::write('  ' . $command, $this->indentLength);
                    }
                    Console::writeln($properties['description']);
                }
            }
        }
    }

    /**
     * Show command.
     *
     * @param string $component
     * @param string $command
     * @throws \Exception
     */
    public function show($component, $command)
    {
        Console::header($this->title);
        if ($component == '') {
            Console::throwError('Component not specified.');
        }
        if (!SignatureHandler::componentExist($component)) {
            Console::throwError('Component not found: ' . $component);
        }
        if ($command == '') {
            Console::throwError('Command not specified.');
        }
        $signature = SignatureHandler::getSignature($component, $command);
        if ($signature === null) {
{'fraction_non_alphanumeric': 0.15081873024992817, 'fraction_numerical': 0.002585463947141626, 'mean_word_length': 1.7277179236043094, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 84, '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': '7009137', 'n_tokens_mistral': 3653, 'n_tokens_neox': 3556, 'n_words': 724}