Skip to content

Commit c3ac708

Browse files
authored
Allow boolean in Command->addOption default value
1 parent 0cd5b0d commit c3ac708

File tree

4 files changed

+5
-8
lines changed

4 files changed

+5
-8
lines changed

src/Rules/Symfony/InvalidOptionDefaultValueRule.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
use PHPStan\Rules\Rule;
99
use PHPStan\ShouldNotHappenException;
1010
use PHPStan\Type\ArrayType;
11-
use PHPStan\Type\Constant\ConstantBooleanType;
11+
use PHPStan\Type\BooleanType;
1212
use PHPStan\Type\Constant\ConstantIntegerType;
1313
use PHPStan\Type\IntegerType;
1414
use PHPStan\Type\MixedType;
1515
use PHPStan\Type\NullType;
1616
use PHPStan\Type\ObjectType;
1717
use PHPStan\Type\StringType;
18-
use PHPStan\Type\TypeCombinator;
1918
use PHPStan\Type\TypeUtils;
2019
use PHPStan\Type\UnionType;
2120
use PHPStan\Type\VerbosityLevel;
@@ -70,10 +69,7 @@ public function processNode(Node $node, Scope $scope): array
7069

7170
// not an array
7271
if (($mode & 8) !== 8) {
73-
$checkType = new UnionType([new StringType(), new IntegerType(), new NullType()]);
74-
if (($mode & 4) === 4) { // https://symfony.com/doc/current/console/input.html#options-with-optional-arguments
75-
$checkType = TypeCombinator::union($checkType, new ConstantBooleanType(false));
76-
}
72+
$checkType = new UnionType([new StringType(), new IntegerType(), new NullType(), new BooleanType()]);
7773
if (!$checkType->isSuperTypeOf($defaultType)->yes()) {
7874
return [sprintf('Parameter #5 $default of method Symfony\Component\Console\Command\Command::addOption() expects %s, %s given.', $checkType->describe(VerbosityLevel::typeOnly()), $defaultType->describe(VerbosityLevel::typeOnly()))];
7975
}

tests/Rules/Symfony/ExampleCommand.php

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected function configure(): void
2929
$this->addOption('b', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', [1]);
3030
$this->addOption('c', null, InputOption::VALUE_OPTIONAL, '', 1);
3131
$this->addOption('d', null, InputOption::VALUE_OPTIONAL, '', false);
32+
$this->addOption('f', null, InputOption::VALUE_REQUIRED, '', true);
3233

3334
/** @var string[] $defaults */
3435
$defaults = [];

tests/Rules/Symfony/UndefinedArgumentRuleTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testGetArgument(): void
3838
[
3939
[
4040
'Command "example-rule" does not define argument "undefined".',
41-
41,
41+
42,
4242
],
4343
]
4444
);

tests/Rules/Symfony/UndefinedOptionRuleTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testGetArgument(): void
3838
[
3939
[
4040
'Command "example-rule" does not define option "bbb".',
41-
48,
41+
49,
4242
],
4343
]
4444
);

0 commit comments

Comments
 (0)