Skip to content

Commit

Permalink
Fix invalid option default argument
Browse files Browse the repository at this point in the history
  • Loading branch information
lookyman committed May 12, 2019
1 parent 1f1527e commit c4da70e
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/Rules/Symfony/InvalidOptionDefaultValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<int, string>|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<string>|null, %s given.', $defaultType->describe(VerbosityLevel::typeOnly()))];
}

return [];
Expand Down
4 changes: 4 additions & 0 deletions tests/Rules/Symfony/ExampleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/Symfony/InvalidOptionDefaultValueRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGetArgument(): void
],
[
[
'Parameter #5 $default of method Symfony\Component\Console\Command\Command::addOption() expects array<int, string>|null, array<int, int> given.',
'Parameter #5 $default of method Symfony\Component\Console\Command\Command::addOption() expects array<string>|null, array<int, int> given.',
29,
],
]
Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/Symfony/UndefinedArgumentRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testGetArgument(): void
[
[
'Command "example-rule" does not define argument "undefined".',
37,
41,
],
]
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/Symfony/UndefinedOptionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testGetArgument(): void
[
[
'Command "example-rule" does not define option "bbb".',
44,
48,
],
]
);
Expand Down

0 comments on commit c4da70e

Please sign in to comment.