Skip to content

Commit

Permalink
Update getArgument return type in interact method
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored May 30, 2024
1 parent 2c3d666 commit af6ae0f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Type/Symfony/data/ExampleBaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, string>|string|null', $input->getArgument('diff'));
assertType('array<int, string>|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'));
Expand Down

0 comments on commit af6ae0f

Please sign in to comment.