Skip to content

Commit 8b1d65f

Browse files
committed
Refactor and improve documentation for CompletionHandler::getCommandNames
1 parent 902f29d commit 8b1d65f

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/CompletionCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ protected function configure()
3434
END
3535
);
3636

37-
// setHidden() method was not available before Symfony 3.2
37+
// Hide this command from listing if supported
38+
// Command::setHidden() was not available before Symfony 3.2.0
3839
if (method_exists($this, 'setHidden')) {
3940
$this->setHidden(true);
4041
}

src/CompletionHandler.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function __construct(Application $application, CompletionContext $context
3939
$this->application = $application;
4040
$this->context = $context;
4141

42+
// Set up completions for commands that are built-into Application
4243
$this->addHandler(
4344
new Completion(
4445
'help',
@@ -436,28 +437,36 @@ protected function getAllOptions()
436437
);
437438
}
438439

440+
/**
441+
* Get command names available for completion
442+
*
443+
* Filters out hidden commands where supported.
444+
*
445+
* @return string[]
446+
*/
439447
protected function getCommandNames()
440448
{
441-
$commands = array();
442-
449+
// Command::Hidden isn't supported before Symfony Console 3.2.0
450+
// We don't complete hidden command names as these are intended to be private
443451
if (method_exists('\Symfony\Component\Console\Command\Command', 'isHidden')) {
452+
$commands = array();
453+
444454
foreach ($this->application->all() as $name => $command) {
445-
if ($command->isHidden()) {
446-
continue;
455+
if (!$command->isHidden()) {
456+
$commands[] = $name;
447457
}
448-
449-
$commands[] = $name;
450458
}
459+
460+
return $commands;
461+
451462
} else {
452-
foreach ($this->application->all() as $name => $command) {
453-
if ($name === '_completion') {
454-
continue;
455-
}
456463

457-
$commands[] = $name;
458-
}
459-
}
464+
// Fallback for compatibility with Symfony Console < 3.2.0
465+
// This was the behaviour prior to pull #75
466+
$commands = $this->application->all();
467+
unset($commands['_completion']);
460468

461-
return $commands;
469+
return array_keys($commands);
470+
}
462471
}
463472
}

0 commit comments

Comments
 (0)