diff --git a/src/Type/Symfony/InputInterfaceGetArgumentDynamicReturnTypeExtension.php b/src/Type/Symfony/InputInterfaceGetArgumentDynamicReturnTypeExtension.php index 0af7c88d..a8d3eb37 100644 --- a/src/Type/Symfony/InputInterfaceGetArgumentDynamicReturnTypeExtension.php +++ b/src/Type/Symfony/InputInterfaceGetArgumentDynamicReturnTypeExtension.php @@ -10,11 +10,13 @@ use PHPStan\Type\ArrayType; use PHPStan\Type\DynamicMethodReturnTypeExtension; use PHPStan\Type\IntegerType; +use PHPStan\Type\NullType; use PHPStan\Type\StringType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; use PHPStan\Type\TypeUtils; use function count; +use function in_array; final class InputInterfaceGetArgumentDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension { @@ -76,6 +78,15 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method } } + $method = $scope->getFunction(); + if ( + $method instanceof MethodReflection + && $method->getName() === 'interact' + && in_array('Symfony\Component\Console\Command\Command', $method->getDeclaringClass()->getParentClassesNames(), true) + ) { + $argTypes[] = new NullType(); + } + return count($argTypes) > 0 ? TypeCombinator::union(...$argTypes) : null; } diff --git a/tests/Type/Symfony/data/ExampleBaseCommand.php b/tests/Type/Symfony/data/ExampleBaseCommand.php index 0ab3a322..6c1f9422 100644 --- a/tests/Type/Symfony/data/ExampleBaseCommand.php +++ b/tests/Type/Symfony/data/ExampleBaseCommand.php @@ -17,6 +17,17 @@ protected function configure(): void $this->addArgument('base'); } + protected function interact(InputInterface $input, OutputInterface $output): int + { + assertType('string|null', $input->getArgument('base')); + assertType('string|null', $input->getArgument('aaa')); + assertType('string|null', $input->getArgument('bbb')); + assertType('array|string|null', $input->getArgument('diff')); + assertType('array|null', $input->getArgument('arr')); + assertType('string|null', $input->getArgument('both')); + assertType('Symfony\Component\Console\Helper\QuestionHelper', $this->getHelper('question')); + } + protected function execute(InputInterface $input, OutputInterface $output): int { assertType('string|null', $input->getArgument('base'));