diff --git a/src/Composer/Command/CompletionTrait.php b/src/Composer/Command/CompletionTrait.php index c16c3a8f5..f49c48471 100644 --- a/src/Composer/Command/CompletionTrait.php +++ b/src/Composer/Command/CompletionTrait.php @@ -35,6 +35,8 @@ trait CompletionTrait /** * Suggestion values for "prefer-install" option + * + * @return string[] */ private function suggestPreferInstall(): array { diff --git a/src/Composer/Command/ExecCommand.php b/src/Composer/Command/ExecCommand.php index 49df0a832..f30467bd8 100644 --- a/src/Composer/Command/ExecCommand.php +++ b/src/Composer/Command/ExecCommand.php @@ -98,6 +98,10 @@ EOT return $dispatcher->dispatchScript('__exec_command', true, $input->getArgument('args')); } + /** + * @param bool $forDisplay + * @return string[] + */ private function getBinaries(bool $forDisplay): array { $composer = $this->requireComposer(); diff --git a/src/Composer/Console/Input/InputArgument.php b/src/Composer/Console/Input/InputArgument.php index 88fe62b3c..697556b3b 100644 --- a/src/Composer/Console/Input/InputArgument.php +++ b/src/Composer/Console/Input/InputArgument.php @@ -14,6 +14,8 @@ namespace Composer\Console\Input; use Symfony\Component\Console\Completion\CompletionInput; use Symfony\Component\Console\Completion\CompletionSuggestions; +use Symfony\Component\Console\Completion\Suggestion; +use Symfony\Component\Console\Exception\InvalidArgumentException; use Symfony\Component\Console\Exception\LogicException; use Symfony\Component\Console\Input\InputArgument as BaseInputArgument; @@ -27,18 +29,22 @@ use Symfony\Component\Console\Input\InputArgument as BaseInputArgument; class InputArgument extends BaseInputArgument { /** - * @var string[]|\Closure + * @var string[]|\Closure(CompletionInput,CompletionSuggestions):list */ private $suggestedValues; /** - * @inheritdoc - * + * @param string $name The argument name + * @param int|null $mode The argument mode: self::REQUIRED or self::OPTIONAL + * @param string $description A description text + * @param string|bool|int|float|string[]|null $default The default value (for self::OPTIONAL mode only) * @param string[]|\Closure(CompletionInput,CompletionSuggestions):list $suggestedValues The values used for input completion + * + * @throws InvalidArgumentException When argument mode is not valid */ public function __construct(string $name, int $mode = null, string $description = '', $default = null, $suggestedValues = []) { - parent::__construct($name, $mode, $description, $default, $suggestedValues); + parent::__construct($name, $mode, $description, $default); $this->suggestedValues = $suggestedValues; } @@ -51,10 +57,10 @@ class InputArgument extends BaseInputArgument public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void { $values = $this->suggestedValues; - if ($values instanceof \Closure && !\is_array($values = $values($input))) { + if ($values instanceof \Closure && !\is_array($values = $values($input, $suggestions))) { throw new LogicException(sprintf('Closure for option "%s" must return an array. Got "%s".', $this->getName(), get_debug_type($values))); } - if ($values) { + if ([] !== $values) { $suggestions->suggestValues($values); } } diff --git a/src/Composer/Console/Input/InputOption.php b/src/Composer/Console/Input/InputOption.php index 0d4ca2722..72807355b 100644 --- a/src/Composer/Console/Input/InputOption.php +++ b/src/Composer/Console/Input/InputOption.php @@ -14,6 +14,8 @@ namespace Composer\Console\Input; use Symfony\Component\Console\Completion\CompletionInput; use Symfony\Component\Console\Completion\CompletionSuggestions; +use Symfony\Component\Console\Completion\Suggestion; +use Symfony\Component\Console\Exception\InvalidArgumentException; use Symfony\Component\Console\Exception\LogicException; use Symfony\Component\Console\Input\InputOption as BaseInputOption; @@ -27,22 +29,25 @@ use Symfony\Component\Console\Input\InputOption as BaseInputOption; class InputOption extends BaseInputOption { /** - * @var string[]|\Closure + * @var string[]|\Closure(CompletionInput,CompletionSuggestions):list */ private $suggestedValues; /** - * @inheritdoc + * @param string|string[]|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts + * @param int|null $mode The option mode: One of the VALUE_* constants + * @param string|bool|int|float|string[]|null $default The default value (must be null for self::VALUE_NONE) + * @param string[]|\Closure(CompletionInput,CompletionSuggestions):list $suggestedValues The values used for input completionnull for self::VALUE_NONE) * - * @param string[]|\Closure(CompletionInput,CompletionSuggestions):list $suggestedValues The values used for input completion + * @throws InvalidArgumentException If option mode is invalid or incompatible */ public function __construct(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null, $suggestedValues = []) { - parent::__construct($name, $shortcut, $mode, $description, $default, $suggestedValues); + parent::__construct($name, $shortcut, $mode, $description, $default); $this->suggestedValues = $suggestedValues; - if ($suggestedValues && !$this->acceptValue()) { + if ([] !== $suggestedValues && !$this->acceptValue()) { throw new LogicException('Cannot set suggested values if the option does not accept a value.'); } } @@ -55,10 +60,10 @@ class InputOption extends BaseInputOption public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void { $values = $this->suggestedValues; - if ($values instanceof \Closure && !\is_array($values = $values($input))) { + if ($values instanceof \Closure && !\is_array($values = $values($input, $suggestions))) { throw new LogicException(sprintf('Closure for argument "%s" must return an array. Got "%s".', $this->getName(), get_debug_type($values))); } - if ($values) { + if ([] !== $values) { $suggestions->suggestValues($values); } } diff --git a/tests/Composer/Test/CompletionFunctionalTest.php b/tests/Composer/Test/CompletionFunctionalTest.php index a2f1ce8c0..422abea79 100644 --- a/tests/Composer/Test/CompletionFunctionalTest.php +++ b/tests/Composer/Test/CompletionFunctionalTest.php @@ -22,6 +22,9 @@ use Symfony\Component\Console\Tester\CommandCompletionTester; */ class CompletionFunctionalTest extends TestCase { + /** + * @return iterable> + */ public function getCommandSuggestions(): iterable { $randomProject = '104corp/cache';