Skip to content

Commit

Permalink
bug #33748 [Console] Do not include hidden commands in suggested alte…
Browse files Browse the repository at this point in the history
…rnatives (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
  • Loading branch information
nicolas-grekas committed Sep 28, 2019
2 parents 293a22a + 8a9d173 commit 944cda7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Symfony/Component/Console/Application.php
Expand Up @@ -529,6 +529,10 @@ public function getNamespaces()
{
$namespaces = [];
foreach ($this->all() as $command) {
if ($command->isHidden()) {
continue;
}

$namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));

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

if ($alternatives = $this->findAlternatives($name, $allCommands)) {
// remove hidden commands
$alternatives = array_filter($alternatives, function ($name) {
return !$this->get($name)->isHidden();
});

if (1 == \count($alternatives)) {
$message .= "\n\nDid you mean this?\n ";
} else {
Expand All @@ -630,7 +639,7 @@ public function find($name)
$message .= implode("\n ", $alternatives);
}

throw new CommandNotFoundException($message, $alternatives);
throw new CommandNotFoundException($message, array_values($alternatives));
}

// filter out aliases for commands which are already on the list
Expand All @@ -654,13 +663,18 @@ public function find($name)
}
$abbrevs = array_map(function ($cmd) use ($commandList, $usableWidth, $maxLen) {
if (!$commandList[$cmd] instanceof Command) {
return $cmd;
$commandList[$cmd] = $this->commandLoader->get($cmd);
}

if ($commandList[$cmd]->isHidden()) {
return false;
}

$abbrev = str_pad($cmd, $maxLen, ' ').' '.$commandList[$cmd]->getDescription();

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

throw new CommandNotFoundException(sprintf("Command \"%s\" is ambiguous.\nDid you mean one of these?\n%s", $name, $suggestions), array_values($commands));
}
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Expand Up @@ -74,6 +74,7 @@ public static function setUpBeforeClass()
require_once self::$fixturesPath.'/FooSubnamespaced2Command.php';
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering.php';
require_once self::$fixturesPath.'/TestAmbiguousCommandRegistering2.php';
require_once self::$fixturesPath.'/FooHiddenCommand.php';
}

protected function normalizeLineBreaks($text)
Expand Down Expand Up @@ -616,6 +617,7 @@ public function testFindAlternativesOutput()
$application->add(new \Foo1Command());
$application->add(new \Foo2Command());
$application->add(new \Foo3Command());
$application->add(new \FooHiddenCommand());

$expectedAlternatives = [
'afoobar',
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Console/Tests/Fixtures/FooHiddenCommand.php
@@ -0,0 +1,21 @@
<?php

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class FooHiddenCommand extends Command
{
protected function configure()
{
$this
->setName('foo:hidden')
->setAliases(['afoohidden'])
->setHidden(true)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
}
}

0 comments on commit 944cda7

Please sign in to comment.