From c4da70e0f8b7af56a3b87c5e70f9bad74f92a5dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Unger?= Date: Sun, 12 May 2019 19:23:41 +0200 Subject: [PATCH] Fix invalid option default argument --- src/Rules/Symfony/InvalidOptionDefaultValueRule.php | 5 +++-- tests/Rules/Symfony/ExampleCommand.php | 4 ++++ tests/Rules/Symfony/InvalidOptionDefaultValueRuleTest.php | 2 +- tests/Rules/Symfony/UndefinedArgumentRuleTest.php | 2 +- tests/Rules/Symfony/UndefinedOptionRuleTest.php | 2 +- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Rules/Symfony/InvalidOptionDefaultValueRule.php b/src/Rules/Symfony/InvalidOptionDefaultValueRule.php index 8af4da19..ddfd576f 100644 --- a/src/Rules/Symfony/InvalidOptionDefaultValueRule.php +++ b/src/Rules/Symfony/InvalidOptionDefaultValueRule.php @@ -11,6 +11,7 @@ use PHPStan\Type\Constant\ConstantBooleanType; use PHPStan\Type\Constant\ConstantIntegerType; use PHPStan\Type\IntegerType; +use PHPStan\Type\MixedType; use PHPStan\Type\NullType; use PHPStan\Type\ObjectType; use PHPStan\Type\StringType; @@ -76,8 +77,8 @@ public function processNode(Node $node, Scope $scope): array } // is array - if (($mode & 8) === 8 && !(new UnionType([new ArrayType(new IntegerType(), new StringType()), new NullType()]))->isSuperTypeOf($defaultType)->yes()) { - return [sprintf('Parameter #5 $default of method Symfony\Component\Console\Command\Command::addOption() expects array|null, %s given.', $defaultType->describe(VerbosityLevel::typeOnly()))]; + if (($mode & 8) === 8 && !(new UnionType([new ArrayType(new MixedType(), new StringType()), new NullType()]))->isSuperTypeOf($defaultType)->yes()) { + return [sprintf('Parameter #5 $default of method Symfony\Component\Console\Command\Command::addOption() expects array|null, %s given.', $defaultType->describe(VerbosityLevel::typeOnly()))]; } return []; diff --git a/tests/Rules/Symfony/ExampleCommand.php b/tests/Rules/Symfony/ExampleCommand.php index ba3fe7bf..c376d875 100644 --- a/tests/Rules/Symfony/ExampleCommand.php +++ b/tests/Rules/Symfony/ExampleCommand.php @@ -29,6 +29,10 @@ protected function configure(): void $this->addOption('b', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', [1]); $this->addOption('c', null, InputOption::VALUE_OPTIONAL, '', 1); $this->addOption('d', null, InputOption::VALUE_OPTIONAL, '', false); + + /** @var string[] $defaults */ + $defaults = []; + $this->addOption('e', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', $defaults); } protected function execute(InputInterface $input, OutputInterface $output): int diff --git a/tests/Rules/Symfony/InvalidOptionDefaultValueRuleTest.php b/tests/Rules/Symfony/InvalidOptionDefaultValueRuleTest.php index 66ea42f7..a8fb119b 100644 --- a/tests/Rules/Symfony/InvalidOptionDefaultValueRuleTest.php +++ b/tests/Rules/Symfony/InvalidOptionDefaultValueRuleTest.php @@ -21,7 +21,7 @@ public function testGetArgument(): void ], [ [ - 'Parameter #5 $default of method Symfony\Component\Console\Command\Command::addOption() expects array|null, array given.', + 'Parameter #5 $default of method Symfony\Component\Console\Command\Command::addOption() expects array|null, array given.', 29, ], ] diff --git a/tests/Rules/Symfony/UndefinedArgumentRuleTest.php b/tests/Rules/Symfony/UndefinedArgumentRuleTest.php index 842f70a7..515c4ce2 100644 --- a/tests/Rules/Symfony/UndefinedArgumentRuleTest.php +++ b/tests/Rules/Symfony/UndefinedArgumentRuleTest.php @@ -35,7 +35,7 @@ public function testGetArgument(): void [ [ 'Command "example-rule" does not define argument "undefined".', - 37, + 41, ], ] ); diff --git a/tests/Rules/Symfony/UndefinedOptionRuleTest.php b/tests/Rules/Symfony/UndefinedOptionRuleTest.php index 1c70e36f..6ab65985 100644 --- a/tests/Rules/Symfony/UndefinedOptionRuleTest.php +++ b/tests/Rules/Symfony/UndefinedOptionRuleTest.php @@ -35,7 +35,7 @@ public function testGetArgument(): void [ [ 'Command "example-rule" does not define option "bbb".', - 44, + 48, ], ] );