Skip to content

Commit 785e975

Browse files
Fix interact method inference
1 parent af6ae0f commit 785e975

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
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/ExampleACommand.php

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ protected function configure(): void
1212
parent::configure();
1313
$this->setName('example-a');
1414

15+
$this->addArgument('required', InputArgument::REQUIRED);
1516
$this->addArgument('aaa', null, '', 'aaa');
1617
$this->addArgument('both');
1718
$this->addArgument('diff', null, '', 'ddd');

tests/Type/Symfony/data/ExampleBCommand.php

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ protected function configure(): void
1212
parent::configure();
1313
$this->setName('example-b');
1414

15+
$this->addArgument('required', InputArgument::REQUIRED);
1516
$this->addArgument('both');
1617
$this->addArgument('bbb', null, '', 'bbb');
1718
$this->addArgument('diff', InputArgument::IS_ARRAY, '', ['diff']);

tests/Type/Symfony/data/ExampleBaseCommand.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ protected function configure(): void
2020
protected function interact(InputInterface $input, OutputInterface $output): int
2121
{
2222
assertType('string|null', $input->getArgument('base'));
23-
assertType('string|null', $input->getArgument('aaa'));
24-
assertType('string|null', $input->getArgument('bbb'));
25-
assertType('array<int, string>|string|null', $input->getArgument('diff'));
26-
assertType('array<int, string>|null', $input->getArgument('arr'));
23+
assertType('string', $input->getArgument('aaa'));
24+
assertType('string', $input->getArgument('bbb'));
25+
assertType('string|null', $input->getArgument('required'));
26+
assertType('array<int, string>|string', $input->getArgument('diff'));
27+
assertType('array<int, string>', $input->getArgument('arr'));
2728
assertType('string|null', $input->getArgument('both'));
2829
assertType('Symfony\Component\Console\Helper\QuestionHelper', $this->getHelper('question'));
2930
}
@@ -33,6 +34,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3334
assertType('string|null', $input->getArgument('base'));
3435
assertType('string', $input->getArgument('aaa'));
3536
assertType('string', $input->getArgument('bbb'));
37+
assertType('string', $input->getArgument('required'));
3638
assertType('array<int, string>|string', $input->getArgument('diff'));
3739
assertType('array<int, string>', $input->getArgument('arr'));
3840
assertType('string|null', $input->getArgument('both'));

0 commit comments

Comments
 (0)