minor #40419 [Console] Avoid unneeded preg_replace_callback (bnf)

This PR was merged into the 5.3-dev branch.

Discussion
----------

[Console] Avoid unneeded preg_replace_callback

| Q             | A
| ------------- | ---
| Branch?       | 5.x
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| License       | MIT

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[^:]*`

Commits
-------

daaa760fd2 Avoid unneeded preg_replace_callback in console application
This commit is contained in:
Fabien Potencier 2021-03-09 07:19:07 +01:00
commit 6dd2d7b288

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)) {