Avoid unneeded preg_replace_callback in console application

Motivations for this change:
 * Avoid an unneded preg call, explode+implode is faster
 * The previous regex created to suboptimal expressions,
   due to the pipe that caused empty to be matched.

   That means an input like `foo:bar`
   was translated into `foo[^:]*[^:]*:bar[^:]*[^:]*`
   instead of simply `foo[^:]*:bar[^:]*`
This commit is contained in:
Benjamin Franzke 2021-03-08 20:44:35 +01:00
parent 10d869d835
commit daaa760fd2

View File

@ -589,7 +589,7 @@ class Application implements ResetInterface
public function findNamespace(string $namespace)
{
$allNamespaces = $this->getNamespaces();
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $namespace);
$expr = implode('[^:]*:', array_map('preg_quote', explode(':', $namespace))).'[^:]*';
$namespaces = preg_grep('{^'.$expr.'}', $allNamespaces);
if (empty($namespaces)) {
@ -645,7 +645,7 @@ class Application implements ResetInterface
}
$allCommands = $this->commandLoader ? array_merge($this->commandLoader->getNames(), array_keys($this->commands)) : array_keys($this->commands);
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
$expr = implode('[^:]*:', array_map('preg_quote', explode(':', $name))).'[^:]*';
$commands = preg_grep('{^'.$expr.'}', $allCommands);
if (empty($commands)) {