Skip to content

Commit

Permalink
Also support command argument and initialize method
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored and ondrejmirtes committed Jun 26, 2024
1 parent bca27f1 commit 1bd7c33
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
if (
$canBeNullInInteract
&& $method instanceof MethodReflection
&& $method->getName() === 'interact'
&& ($method->getName() === 'interact' || $method->getName() === 'initialize')
&& in_array('Symfony\Component\Console\Command\Command', $method->getDeclaringClass()->getParentClassesNames(), true)
) {
$argTypes[] = new NullType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Type\TypeUtils;
use function array_unique;
use function count;
use function in_array;

final class InputInterfaceHasArgumentDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
Expand Down Expand Up @@ -52,6 +53,17 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}
$argName = $argStrings[0]->getValue();

if ($argName === 'command') {
$method = $scope->getFunction();
if (
$method instanceof MethodReflection
&& ($method->getName() === 'interact' || $method->getName() === 'initialize')
&& in_array('Symfony\Component\Console\Command\Command', $method->getDeclaringClass()->getParentClassesNames(), true)
) {
return null;
}
}

$returnTypes = [];
foreach ($this->consoleApplicationResolver->findCommands($classReflection) as $command) {
try {
Expand Down
23 changes: 22 additions & 1 deletion tests/Type/Symfony/data/ExampleBaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,26 @@ protected function configure(): void
$this->addArgument('base');
}

protected function interact(InputInterface $input, OutputInterface $output): int
protected function initialize(InputInterface $input, OutputInterface $output): void
{
assertType('bool', $input->hasArgument('command'));
assertType('string|null', $input->getArgument('command'));

assertType('string|null', $input->getArgument('base'));
assertType('string', $input->getArgument('aaa'));
assertType('string', $input->getArgument('bbb'));
assertType('string|null', $input->getArgument('required'));
assertType('array<int, string>|string', $input->getArgument('diff'));
assertType('array<int, string>', $input->getArgument('arr'));
assertType('string|null', $input->getArgument('both'));
assertType('Symfony\Component\Console\Helper\QuestionHelper', $this->getHelper('question'));
}

protected function interact(InputInterface $input, OutputInterface $output): void
{
assertType('bool', $input->hasArgument('command'));
assertType('string|null', $input->getArgument('command'));

assertType('string|null', $input->getArgument('base'));
assertType('string', $input->getArgument('aaa'));
assertType('string', $input->getArgument('bbb'));
Expand All @@ -33,6 +51,9 @@ protected function interact(InputInterface $input, OutputInterface $output): int

protected function execute(InputInterface $input, OutputInterface $output): int
{
assertType('true', $input->hasArgument('command'));
assertType('string', $input->getArgument('command'));

assertType('string|null', $input->getArgument('base'));
assertType('string', $input->getArgument('aaa'));
assertType('string', $input->getArgument('bbb'));
Expand Down

0 comments on commit 1bd7c33

Please sign in to comment.