Skip to content

Commit a14d467

Browse files
eliashaeusslerondrejmirtes
authored andcommitted
Add support for negatable input options
Since symfony/console 5.3, input options can be marked as negatable. The appropriate return type when calling `$input->getOption()` can be either a boolean or null. With this change, support for return types of such negatable input options is added.
1 parent 82d54e8 commit a14d467

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/Type/Symfony/GetOptionTypeHelper.php

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Type\StringType;
1111
use PHPStan\Type\Type;
1212
use PHPStan\Type\TypeCombinator;
13+
use PHPStan\Type\UnionType;
1314
use Symfony\Component\Console\Input\InputOption;
1415

1516
class GetOptionTypeHelper
@@ -18,6 +19,10 @@ class GetOptionTypeHelper
1819
public function getOptionType(Scope $scope, InputOption $option): Type
1920
{
2021
if (!$option->acceptValue()) {
22+
if ($option->isNegatable()) {
23+
return new UnionType([new BooleanType(), new NullType()]);
24+
}
25+
2126
return new BooleanType();
2227
}
2328

tests/Type/Symfony/data/ExampleOptionCommand.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ protected function configure(): void
2121
$this->addOption('c', null, InputOption::VALUE_REQUIRED);
2222
$this->addOption('d', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL);
2323
$this->addOption('e', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED);
24+
$this->addOption('f', null, InputOption::VALUE_NEGATABLE);
2425

2526
$this->addOption('bb', null, InputOption::VALUE_OPTIONAL, '', 1);
2627
$this->addOption('cc', null, InputOption::VALUE_REQUIRED, '', 1);
@@ -35,13 +36,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3536
assertType('string|null', $input->getOption('c'));
3637
assertType('array<int, string|null>', $input->getOption('d'));
3738
assertType('array<int, string>', $input->getOption('e'));
39+
assertType('bool|null', $input->getOption('f'));
3840

3941
assertType('1|string|null', $input->getOption('bb'));
4042
assertType('1|string', $input->getOption('cc'));
4143
assertType('array<int, 1|string|null>', $input->getOption('dd'));
4244
assertType('array<int, 1|string>', $input->getOption('ee'));
4345

44-
assertType('array{a: bool, b: string|null, c: string|null, d: array<int, string|null>, e: array<int, string>, bb: 1|string|null, cc: 1|string, dd: array<int, 1|string|null>, ee: array<int, 1|string>, help: bool, quiet: bool, verbose: bool, version: bool, ansi: bool, no-interaction: bool}', $input->getOptions());
46+
assertType('array{a: bool, b: string|null, c: string|null, d: array<int, string|null>, e: array<int, string>, f: bool|null, bb: 1|string|null, cc: 1|string, dd: array<int, 1|string|null>, ee: array<int, 1|string>, help: bool, quiet: bool, verbose: bool, version: bool, ansi: bool|null, no-interaction: bool}', $input->getOptions());
4547
}
4648

4749
}

tests/Type/Symfony/data/ExampleOptionLazyCommand.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ protected function configure(): void
2323
$this->addOption('c', null, InputOption::VALUE_REQUIRED);
2424
$this->addOption('d', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL);
2525
$this->addOption('e', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED);
26+
$this->addOption('f', null, InputOption::VALUE_NEGATABLE);
2627

2728
$this->addOption('bb', null, InputOption::VALUE_OPTIONAL, '', 1);
2829
$this->addOption('cc', null, InputOption::VALUE_REQUIRED, '', 1);
@@ -37,13 +38,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3738
assertType('string|null', $input->getOption('c'));
3839
assertType('array<int, string|null>', $input->getOption('d'));
3940
assertType('array<int, string>', $input->getOption('e'));
41+
assertType('bool|null', $input->getOption('f'));
4042

4143
assertType('1|string|null', $input->getOption('bb'));
4244
assertType('1|string', $input->getOption('cc'));
4345
assertType('array<int, 1|string|null>', $input->getOption('dd'));
4446
assertType('array<int, 1|string>', $input->getOption('ee'));
4547

46-
assertType('array{a: bool, b: string|null, c: string|null, d: array<int, string|null>, e: array<int, string>, bb: 1|string|null, cc: 1|string, dd: array<int, 1|string|null>, ee: array<int, 1|string>, help: bool, quiet: bool, verbose: bool, version: bool, ansi: bool, no-interaction: bool}', $input->getOptions());
48+
assertType('array{a: bool, b: string|null, c: string|null, d: array<int, string|null>, e: array<int, string>, f: bool|null, bb: 1|string|null, cc: 1|string, dd: array<int, 1|string|null>, ee: array<int, 1|string>, help: bool, quiet: bool, verbose: bool, version: bool, ansi: bool|null, no-interaction: bool}', $input->getOptions());
4749
}
4850

4951
}

0 commit comments

Comments
 (0)