Skip to content

Commit 1bd7c33

Browse files
VincentLangletondrejmirtes
authored andcommitted
Also support command argument and initialize method
1 parent bca27f1 commit 1bd7c33

3 files changed

+35
-2
lines changed

src/Type/Symfony/InputInterfaceGetArgumentDynamicReturnTypeExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
8989
if (
9090
$canBeNullInInteract
9191
&& $method instanceof MethodReflection
92-
&& $method->getName() === 'interact'
92+
&& ($method->getName() === 'interact' || $method->getName() === 'initialize')
9393
&& in_array('Symfony\Component\Console\Command\Command', $method->getDeclaringClass()->getParentClassesNames(), true)
9494
) {
9595
$argTypes[] = new NullType();

src/Type/Symfony/InputInterfaceHasArgumentDynamicReturnTypeExtension.php

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPStan\Type\TypeUtils;
1414
use function array_unique;
1515
use function count;
16+
use function in_array;
1617

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

56+
if ($argName === 'command') {
57+
$method = $scope->getFunction();
58+
if (
59+
$method instanceof MethodReflection
60+
&& ($method->getName() === 'interact' || $method->getName() === 'initialize')
61+
&& in_array('Symfony\Component\Console\Command\Command', $method->getDeclaringClass()->getParentClassesNames(), true)
62+
) {
63+
return null;
64+
}
65+
}
66+
5567
$returnTypes = [];
5668
foreach ($this->consoleApplicationResolver->findCommands($classReflection) as $command) {
5769
try {

tests/Type/Symfony/data/ExampleBaseCommand.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,26 @@ protected function configure(): void
1919
$this->addArgument('base');
2020
}
2121

22-
protected function interact(InputInterface $input, OutputInterface $output): int
22+
protected function initialize(InputInterface $input, OutputInterface $output): void
2323
{
24+
assertType('bool', $input->hasArgument('command'));
25+
assertType('string|null', $input->getArgument('command'));
26+
27+
assertType('string|null', $input->getArgument('base'));
28+
assertType('string', $input->getArgument('aaa'));
29+
assertType('string', $input->getArgument('bbb'));
30+
assertType('string|null', $input->getArgument('required'));
31+
assertType('array<int, string>|string', $input->getArgument('diff'));
32+
assertType('array<int, string>', $input->getArgument('arr'));
33+
assertType('string|null', $input->getArgument('both'));
34+
assertType('Symfony\Component\Console\Helper\QuestionHelper', $this->getHelper('question'));
35+
}
36+
37+
protected function interact(InputInterface $input, OutputInterface $output): void
38+
{
39+
assertType('bool', $input->hasArgument('command'));
40+
assertType('string|null', $input->getArgument('command'));
41+
2442
assertType('string|null', $input->getArgument('base'));
2543
assertType('string', $input->getArgument('aaa'));
2644
assertType('string', $input->getArgument('bbb'));
@@ -33,6 +51,9 @@ protected function interact(InputInterface $input, OutputInterface $output): int
3351

3452
protected function execute(InputInterface $input, OutputInterface $output): int
3553
{
54+
assertType('true', $input->hasArgument('command'));
55+
assertType('string', $input->getArgument('command'));
56+
3657
assertType('string|null', $input->getArgument('base'));
3758
assertType('string', $input->getArgument('aaa'));
3859
assertType('string', $input->getArgument('bbb'));

0 commit comments

Comments
 (0)