Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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