8000 bug #33748 [Console] Do not include hidden commands in suggested alte… · symfony/symfony@944cda7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 944cda7

Browse files
bug #33748 [Console] Do not include hidden commands in suggested alternatives (m-vo)
This PR was merged into the 3.4 branch. Discussion ---------- [Console] Do not include hidden commands in suggested alternatives | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #33398 | License | MIT | Doc PR | n/a Partially backports #33412 on 3.4, avoids leaking the names of hidden commands in suggested alternatives on command not found. Commits ------- 8a9d173 Do not include hidden commands in suggested alternatives
2 parents 293a22a + 8a9d173 commit 944cda7

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,10 @@ public function getNamespaces()
529529
{
530530
$namespaces = [];
531531
foreach ($this->all() as $command) {
532+
if ($command->isHidden()) {
533+
continue;
534+
}
535+
532536
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));
533537

534538
foreach ($command->getAliases() as $alias) {
@@ -622,6 +626,11 @@ public function find($name)
622626
$message = sprintf('Command "%s" is not defined.', $name);
623627

624628
if ($alternatives = $this->findAlternatives($name, $allCommands)) {
629+
// remove hidden commands
630+
$alternatives = array_filter($alternatives, function ($name) {
631+
return !$this->get($name)->isHidden();
632+
});
633+
625634
if (1 == \count($alternatives)) {
626635
$message .= "\n\nDid you mean this?\n ";
627636
} else {
@@ -630,7 +639,7 @@ public function find($name)
630639
$message .= implode("\n ", $alternatives);
631640
}
632641

633-
throw new CommandNotFoundException($message, $alternatives);
642+
throw new CommandNotFoundException($message, array_values($alternatives));
634643
}
635644

636645
// filter out aliases for commands which are already on the list
@@ -654,13 +663,18 @@ public function find($name)
654663
}
655664
$abbrevs = array_map(function ($cmd) use ($commandList, $usableWidth, $maxLen) {
656665
if (!$commandList[$cmd] instanceof Command) {
657-
return $cmd;
666+
$commandList[$cmd] = $this->commandLoader->get($cmd);
667+
}
668+
669+
if ($commandList[$cmd]->isHidden()) {
670+
return false;
658671
}
672+
659673
$abbrev = str_pad($cmd, $maxLen, ' ').' '.$commandList[$cmd]->getDescription();
660674

661675
return Helper::strlen($abbrev) > $usableWidth ? Helper::substr($abbrev, 0, $usableWidth - 3).'...' : $abbrev;
662676
}, array_values($commands));
663-
$suggestions = $this->getAbbreviationSuggestions($abbrevs);
677+
$suggestions = $this->getAbbreviationSuggestions(array_filter($abbrevs));
664678

665679
throw new CommandNotFoundException(sprintf("Command \"%s\" is ambiguous.\nDid you mean one of these?\n%s", $name, $suggestions), array_values($commands));
666680
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public static function setUpBeforeClass()
7474
require_once self::$fixturesPath.'/FooSubnamespaced2Command.php';
7575
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering.php';
7676
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering2.php';
77+
require_once self::$fixturesPath.'/FooHiddenCommand.php';
7778
}
7879

7980
protected function normalizeLineBreaks($text)
@@ -616,6 +617,7 @@ public function testFindAlternativesOutput()
616617
$application->add(new \Foo1Command());
617618
$application->add(new \Foo2Command());
618619
$application->add(new \Foo3Command());
620+
$application->add(new \FooHiddenCommand());
619621

620622
$expectedAlternatives = [
621623
'afoobar',
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Input\InputInterface;
5+
use Symfony\Component\Console\Output\OutputInterface;
6+
7+
class FooHiddenCommand extends Command
8+
{
9+
protected function configure()
10+
{
11+
$this
12+
->setName('foo:hidden')
13+
->setAliases(['afoohidden'])
14+
->setHidden(true)
15+
;
16+
}
17+
18+
protected function execute(InputInterface $input, OutputInterface $output)
19+
{
20+
}
21+
}

0 commit comments

Comments
 (0)
0