Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a17358

Browse files
committed
feature #19887 Sort alternatives alphabetically when a command is not found (javiereguiluz)
This PR was squashed before being merged into the 3.3-dev branch (closes #19887). Discussion ---------- Sort alternatives alphabetically when a command is not found | Q | A | | --- | --- | | Branch? | master | | Bug fix? | no | | New feature? | yes | | BC breaks? | no | | Deprecations? | no | | Tests pass? | yes | | Fixed tickets | #10893 | | License | MIT | | Doc PR | - | Commits ------- ba6c946 Sort commands like a human would do f04b1bd Sort alternatives alphabetically when a command is not found
2 parents 59dd752 + ba6c946 commit 0a17358

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ private function findAlternatives($name, $collection)
10231023
}
10241024

10251025
$alternatives = array_filter($alternatives, function ($lev) use ($threshold) { return $lev < 2 * $threshold; });
1026-
asort($alternatives);
1026+
ksort($alternatives, SORT_NATURAL | SORT_FLAG_CASE);
10271027

10281028
return array_keys($alternatives);
10291029
}

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,36 @@ public function testFindAlternativeNamespace()
477477
}
478478
}
479479

480+
public function testFindAlternativesOutput()
481+
{
482+
$application = new Application();
483+
484+
$application->add(new \FooCommand());
485+
$application->add(new \Foo1Command());
486+
$application->add(new \Foo2Command());
487+
$application->add(new \Foo3Command());
488+
489+
$expectedAlternatives = array(
490+
'afoobar',
491+
'afoobar1',
492+
'afoobar2',
493+
'foo1:bar',
494+
'foo3:bar',
495+
'foo:bar',
496+
'foo:bar1',
497+
);
498+
499+
try {
500+
$application->find('foo');
501+
$this->fail('->find() throws a CommandNotFoundException if command is not defined');
502+
} catch (\Exception $e) {
503+
$this->assertInstanceOf('Symfony\Component\Console\Exception\CommandNotFoundException', $e, '->find() throws a CommandNotFoundException if command is not defined');
504+
$this->assertSame($expectedAlternatives, $e->getAlternatives());
505+
506+
$this->assertRegExp('/Command "foo" is not defined\..*Did you mean one of these\?.*/Ums', $e->getMessage());
507+
}
508+
}
509+
480510
public function testFindNamespaceDoesNotFailOnDeepSimilarNamespaces()
481511
{
482512
$application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getNamespaces'))->getMock();

0 commit comments

Comments
 (0)