Skip to content

Commit 2eacd27

Browse files
Fix interact method inference
1 parent af6ae0f commit 2eacd27

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/Type/Symfony/InputInterfaceGetArgumentDynamicReturnTypeExtension.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
5757
$argName = $argStrings[0]->getValue();
5858

5959
$argTypes = [];
60+
$canBeNullInInteract = false;
6061
foreach ($this->consoleApplicationResolver->findCommands($classReflection) as $command) {
6162
try {
6263
$command->mergeApplicationDefinition();
@@ -70,6 +71,8 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
7071
$argType = new StringType();
7172
if (!$argument->isRequired()) {
7273
$argType = TypeCombinator::union($argType, $scope->getTypeFromValue($argument->getDefault()));
74+
} else {
75+
$canBeNullInInteract = true;
7376
}
7477
}
7578
$argTypes[] = $argType;
@@ -78,16 +81,21 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
7881
}
7982
}
8083

84+
if (count($argTypes) === 0) {
85+
return null;
86+
}
87+
8188
$method = $scope->getFunction();
8289
if (
83-
$method instanceof MethodReflection
90+
$canBeNullInInteract
91+
&& $method instanceof MethodReflection
8492
&& $method->getName() === 'interact'
8593
&& in_array('Symfony\Component\Console\Command\Command', $method->getDeclaringClass()->getParentClassesNames(), true)
8694
) {
8795
$argTypes[] = new NullType();
8896
}
8997

90-
return count($argTypes) > 0 ? TypeCombinator::union(...$argTypes) : null;
98+
return TypeCombinator::union(...$argTypes);
9199
}
92100

93101
}

tests/Type/Symfony/data/ExampleBaseCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function interact(InputInterface $input, OutputInterface $output): int
2323
assertType('string|null', $input->getArgument('aaa'));
2424
assertType('string|null', $input->getArgument('bbb'));
2525
assertType('array<int, string>|string|null', $input->getArgument('diff'));
26-
assertType('array<int, string>|null', $input->getArgument('arr'));
26+
assertType('array<int, string>', $input->getArgument('arr'));
2727
assertType('string|null', $input->getArgument('both'));
2828
assertType('Symfony\Component\Console\Helper\QuestionHelper', $this->getHelper('question'));
2929
}

0 commit comments

Comments
 (0)