Skip to content

Commit

Permalink
bug #34130 [Console] Fix commands description with numeric namespaces…
Browse files Browse the repository at this point in the history
… (fancyweb)

This PR was merged into the 3.4 branch.

Discussion
----------

[Console] Fix commands description with numeric namespaces

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #34111
| License       | MIT
| Doc PR        | -

This PR fixes the linked ticket case.

It also changes the keys sorting to display the numeric namespaces first.

It also fixes another bug if your command name starts with `_global:`. In this special case the command is considered global but its full name is still `_global:xxx`. We can't do better without more refactoring since the final array of namespaces and global commands is shared, `_global` just being a special key. Currently, if your command starts with `_global`, all global commands are not displayed at all so it's better like this anyway.

It also fixes another bug if your command starts with `0:` (cf `'' ===` comparison).

Commits
-------

4d47868 [Console] Fix commands description with numeric namespaces
  • Loading branch information
nicolas-grekas committed Nov 28, 2019
2 parents 6e6ed9c + 4d47868 commit fa783f9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
Expand Up @@ -129,23 +129,29 @@ private function sortCommands(array $commands)
{
$namespacedCommands = [];
$globalCommands = [];
$sortedCommands = [];
foreach ($commands as $name => $command) {
$key = $this->application->extractNamespace($name, 1);
if (!$key) {
$globalCommands['_global'][$name] = $command;
if (\in_array($key, ['', self::GLOBAL_NAMESPACE], true)) {
$globalCommands[$name] = $command;
} else {
$namespacedCommands[$key][$name] = $command;
}
}
ksort($namespacedCommands);
$namespacedCommands = array_merge($globalCommands, $namespacedCommands);

foreach ($namespacedCommands as &$commandsSet) {
ksort($commandsSet);
if ($globalCommands) {
ksort($globalCommands);
$sortedCommands[self::GLOBAL_NAMESPACE] = $globalCommands;
}
// unset reference to keep scope clear
unset($commandsSet);

return $namespacedCommands;
if ($namespacedCommands) {
ksort($namespacedCommands);
foreach ($namespacedCommands as $key => $commandsSet) {
ksort($commandsSet);
$sortedCommands[$key] = $commandsSet;
}
}

return $sortedCommands;
}
}
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Tests\Descriptor;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Descriptor\ApplicationDescription;

final class ApplicationDescriptionTest extends TestCase
{
/**
* @dataProvider getNamespacesProvider
*/
public function testGetNamespaces(array $expected, array $names)
{
$application = new TestApplication();
foreach ($names as $name) {
$application->add(new Command($name));
}

$this->assertSame($expected, array_keys((new ApplicationDescription($application))->getNamespaces()));
}

public function getNamespacesProvider()
{
return [
[['_global'], ['foobar']],
[['a', 'b'], ['b:foo', 'a:foo', 'b:bar']],
[['_global', 'b', 'z', 22, 33], ['z:foo', '1', '33:foo', 'b:foo', '22:foo:bar']],
];
}
}

final class TestApplication extends Application
{
/**
* {@inheritdoc}
*/
protected function getDefaultCommands()
{
return [];
}
}

0 comments on commit fa783f9

Please sign in to comment.