1
0
Fork 0

PHPStan annotations

pull/10320/head
Jérôme Tamarelle 2022-04-15 15:11:01 +02:00 committed by Jordi Boggiano
parent ed7d8219ad
commit 68fd80b630
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
5 changed files with 33 additions and 13 deletions

View File

@ -35,6 +35,8 @@ trait CompletionTrait
/** /**
* Suggestion values for "prefer-install" option * Suggestion values for "prefer-install" option
*
* @return string[]
*/ */
private function suggestPreferInstall(): array private function suggestPreferInstall(): array
{ {

View File

@ -98,6 +98,10 @@ EOT
return $dispatcher->dispatchScript('__exec_command', true, $input->getArgument('args')); return $dispatcher->dispatchScript('__exec_command', true, $input->getArgument('args'));
} }
/**
* @param bool $forDisplay
* @return string[]
*/
private function getBinaries(bool $forDisplay): array private function getBinaries(bool $forDisplay): array
{ {
$composer = $this->requireComposer(); $composer = $this->requireComposer();

View File

@ -14,6 +14,8 @@ namespace Composer\Console\Input;
use Symfony\Component\Console\Completion\CompletionInput; use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions; 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\Exception\LogicException;
use Symfony\Component\Console\Input\InputArgument as BaseInputArgument; use Symfony\Component\Console\Input\InputArgument as BaseInputArgument;
@ -27,18 +29,22 @@ use Symfony\Component\Console\Input\InputArgument as BaseInputArgument;
class InputArgument extends BaseInputArgument class InputArgument extends BaseInputArgument
{ {
/** /**
* @var string[]|\Closure * @var string[]|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion>
*/ */
private $suggestedValues; 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<string|Suggestion> $suggestedValues The values used for input completion * @param string[]|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $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 = []) 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; $this->suggestedValues = $suggestedValues;
} }
@ -51,10 +57,10 @@ class InputArgument extends BaseInputArgument
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{ {
$values = $this->suggestedValues; $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))); 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); $suggestions->suggestValues($values);
} }
} }

View File

@ -14,6 +14,8 @@ namespace Composer\Console\Input;
use Symfony\Component\Console\Completion\CompletionInput; use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions; 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\Exception\LogicException;
use Symfony\Component\Console\Input\InputOption as BaseInputOption; use Symfony\Component\Console\Input\InputOption as BaseInputOption;
@ -27,22 +29,25 @@ use Symfony\Component\Console\Input\InputOption as BaseInputOption;
class InputOption extends BaseInputOption class InputOption extends BaseInputOption
{ {
/** /**
* @var string[]|\Closure * @var string[]|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion>
*/ */
private $suggestedValues; 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<string|Suggestion> $suggestedValues The values used for input completionnull for self::VALUE_NONE)
* *
* @param string[]|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $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 = []) 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; $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.'); 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 public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{ {
$values = $this->suggestedValues; $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))); 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); $suggestions->suggestValues($values);
} }
} }

View File

@ -22,6 +22,9 @@ use Symfony\Component\Console\Tester\CommandCompletionTester;
*/ */
class CompletionFunctionalTest extends TestCase class CompletionFunctionalTest extends TestCase
{ {
/**
* @return iterable<array<string|string[]|null>>
*/
public function getCommandSuggestions(): iterable public function getCommandSuggestions(): iterable
{ {
$randomProject = '104corp/cache'; $randomProject = '104corp/cache';