<commit_before><?php

namespace ManaPHP\Cli;

use ManaPHP\Component;
use ManaPHP\Utility\Text;

/**
 * Class ManaPHP\Cli\Router
 *
 * @package router
 *
 * @property \ManaPHP\Text\CrosswordInterface $crossword
 */
class Router extends Component implements RouterInterface
{
    /**
     * @var bool
     */
    protected $_guessCommand = true;

    /**
     * @var string
     */
    protected $_controllerName;

    /**
     * @var string
     */
    protected $_actionName;

    /**
     * @return string
     */
    public function getControllerName()
    {
        return $this->_controllerName;
    }

    /**
     * @return string
     */
    public function getActionName()
    {
        return $this->_actionName;
    }

    /**
     * @return array
     */
    protected function _getControllers()
    {
        $controllers = [];

        if ($this->alias->has('@cli')) {
            foreach ($this->filesystem->glob('@cli/*Controller.php') as $file) {
                if (preg_match('#/(\w+)Controller\.php$#', $file, $matches)) {
                    $controllers[] = $matches[1];
                }
            }
        }

        foreach ($this->filesystem->glob('@manaphp/Cli/Controllers/*Controller.php') as $file) {
            if (preg_match('#/(\w+)Controller\.php$#', $file, $matches)) {
                if (in_array($matches[1], $controllers, true)) {
                    continue;
                }

                $controllers[] = $matches[1];
            }
        }

        return $controllers;
    }

    /**
     * @param string $controller
     *
     * @return array
     */
    protected function _getCommands($controller)
    {
        $commands = [];

        if ($this->alias->has('@ns.cli')) {
            $controllerClassName = $this->alias->resolveNS('@ns.cli\\' . $controller . 'Controller');
            if (!class_exists($controllerClassName)) {
                $controllerClassName = 'ManaPHP\Cli\Controllers\\' . $controller . 'Controller';
            }
        } else {
            $controllerClassName = 'ManaPHP\Cli\Controllers\\' . $controller . 'Controller';
        }

        /** @noinspection NotOptimalIfConditionsInspection */
        if (!class_exists($controllerClassName)) {
            return [];
        }

        foreach (get_class_methods($controllerClassName) as $method) {
            if (preg_match('#^(.*)Command$#', $method, $match) === 1 && $match[1] !== 'help') {
                $commands[] = $match[1];
            }
        }

        return $commands;
    }

    /**
     * @param array $args
     *
     * @return bool
     */
    public function route($args)
    {
        $this->_controllerName = null;
        $this->_actionName = null;

        if (count($args) === 2 && $args[1][0] === '/') {
            $path = parse_url($args[1], PHP_URL_PATH);
            $parts = explode('/', trim($path, '/'));
            if (count($parts) !== 2) {
                return false;
            }

            list($controllerName, $actionName) = $parts;
            $this->_controllerName = Text::camelize($controllerName);
        } else {
            list(, $controllerName, $actionName) = array_pad($args, 3, null);
            if ($controllerName === null) {
                $controllerName = 'help';
            } elseif ($controllerName === 'help' && $actionName !== null) {
                $controllerName = $actionName;
                $actionName = 'help';
            }

            if ($actionName === null) {
                $actionName = $controllerName === 'help' ? 'list' : 'help';
            }

            if ($this->_guessCommand && strlen($controllerName) <= 4) {
                $controllers = $this->_getControllers();
                foreach ($controllers as $k => $controller) {
                    $controllers[$k] = Text::underscore($controller);
                }

                $controllerName = $this->crossword->guess($controllers, $controllerName);
                if (!$controllerName) {
                    return false;
                }
            }
            $this->_controllerName = Text::camelize($controllerName);

            if ($actionName === 'help') {
                null;
            } elseif ($actionName === null) {
                $commands = $this->_getCommands($this->_controllerName);
                if (count($commands) === 1) {
                    $actionName = $commands[0];
                } else {
                    $actionName = 'help';
                }
            } else {
                if ($this->_guessCommand) {
                    $commands = $this->_getCommands($this->_controllerName);
                    $actionName = $this->crossword->guess($commands, $actionName);
                    if (!$actionName) {
                        return false;
                    }
                }
            }
        }

        $this->_actionName = lcfirst(Text::camelize($actionName));

        return true;
    }
}<commit_msg>Modified   ManaPHP/Cli/Router.php<commit_after><?php

namespace ManaPHP\Cli;

use ManaPHP\Component;
use ManaPHP\Utility\Text;

/**
 * Class ManaPHP\Cli\Router
 *
 * @package router
 *
 * @property \ManaPHP\Text\CrosswordInterface $crossword
 */
class Router extends Component implements RouterInterface
{
    /**
     * @var bool
     */
    protected $_guessCommand = true;

    /**
     * @var string
     */
    protected $_controllerName;

    /**
     * @var string
     */
    protected $_actionName;

    /**
     * @return string
     */
    public function getControllerName()
    {
        return $this->_controllerName;
    }

    /**
     * @return string
     */
    public function getActionName()
    {
        return $this->_actionName;
    }

    /**
     * @return array
     */
    protected function _getControllers()
    {
        $controllers = [];

        if ($this->alias->has('@cli')) {
            foreach ($this->filesystem->glob('@cli/*Controller.php') as $file) {
                if (preg_match('#/(\w+)Controller\.php$#', $file, $matches)) {
                    $controllers[] = $matches[1];
                }
            }
        }

        foreach ($this->filesystem->glob('@manaphp/Cli/Controllers/*Controller.php') as $file) {
            if (preg_match('#/(\w+)Controller\.php$#', $file, $matches)) {
                if (in_array($matches[1], $controllers, true)) {
                    continue;
                }

                $controllers[] = $matches[1];
            }
        }

        return $controllers;
    }

    /**
     * @param string $controller
     *
     * @return array
     */
    protected function _getCommands($controller)
    {
        $commands = [];

        if ($this->alias->has('@ns.cli')) {
            $controllerClassName = $this->alias->resolveNS('@ns.cli\\' . $controller . 'Controller');
            if (!class_exists($controllerClassName)) {
                $controllerClassName = 'ManaPHP\Cli\Controllers\\' . $controller . 'Controller';
            }
        } else {
            $controllerClassName = 'ManaPHP\Cli\Controllers\\' . $controller . 'Controller';
        }

        /** @noinspection NotOptimalIfConditionsInspection */
        if (!class_exists($controllerClassName)) {
            return [];
        }

        foreach (get_class_methods($controllerClassName) as $method) {
            if (preg_match('#^(.*)Command$#', $method, $match) === 1 && $match[1] !== 'help') {
                $commands[] = $match[1];
            }
        }

        return $commands;
    }

    /**
     * @param array $args
     *
     * @return bool
     */
    public function route($args)
    {
        $this->_controllerName = null;
        $this->_actionName = null;

        if (count($args) === 2 && $args[1][0] === '/') {
            $path = parse_url($args[1], PHP_URL_PATH);
            $parts = explode('/', trim($path, '/'));
            if (count($parts) !== 2) {
                return false;
            }

            list($controllerName, $actionName) = $parts;
            $this->_controllerName = Text::camelize($controllerName);
        } else {
            list(, $controllerName, $actionName) = array_pad($args, 3, null);
            if ($controllerName === null) {
                $controllerName = 'help';
            } elseif ($controllerName === 'help' && $actionName !== null) {
                $controllerName = $actionName;
                $actionName = 'help';
            }

            if ($this->_guessCommand && strlen($controllerName) <= 4) {
                $controllers = $this->_getControllers();
                foreach ($controllers as $k => $controller) {
                    $controllers[$k] = Text::underscore($controller);
                }

                $controllerName = $this->crossword->guess($controllers, $controllerName);
                if (!$controllerName) {
                    return false;
                }
            }
            $this->_controllerName = Text::camelize($controllerName);

            if ($actionName === 'help') {
                null;
            } elseif ($actionName === null) {
                $commands = $this->_getCommands($this->_controllerName);
                if (count($commands) === 1) {
                    $actionName = $commands[0];
                } else {
                    $actionName = 'help';
                }
            } else {
                if ($this->_guessCommand) {
                    $commands = $this->_getCommands($this->_controllerName);
                    $actionName = $this->crossword->guess($commands, $actionName);
                    if (!$actionName) {
                        return false;
                    }
                }
            }
        }

        $this->_actionName = lcfirst(Text::camelize($actionName));

        return true;
    }
}
{'fraction_non_alphanumeric': 0.15140091696383087, 'fraction_numerical': 0.003056546102903719, 'mean_word_length': 1.5851988411904134, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 61, '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': '2320699', 'n_tokens_mistral': 2756, 'n_tokens_neox': 2631, 'n_words': 481}