From daaa760fd279aa3de4517ecf20eaaae2558943b2 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Mon, 8 Mar 2021 20:44:35 +0100 Subject: [PATCH] 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[^:]*` --- src/Symfony/Component/Console/Application.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 440b93dc5c..10202bea2b 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -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)) {