Skip to content

Commit b4b09a6

Browse files
authored
[Symfony 7.3] Make nullable argument param type on Optional InputArgument::OPTIONAL on InvokableCommandInputAttributeRector (#775)
1 parent 366cd51 commit b4b09a6

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;
4+
5+
use Symfony\Component\Console\Attribute\AsCommand;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputOption;
11+
12+
#[AsCommand(name: 'some_name')]
13+
final class WithOptionalArgument extends Command
14+
{
15+
public function configure()
16+
{
17+
$this->addArgument('argument', InputArgument::OPTIONAL, 'Argument description');
18+
$this->addOption('option', 'o', InputOption::VALUE_NONE, 'Option description');
19+
}
20+
21+
public function execute(InputInterface $input, OutputInterface $output): int
22+
{
23+
$someArgument = $input->getArgument('argument');
24+
$someOption = $input->getOption('option');
25+
26+
// ...
27+
28+
return 1;
29+
}
30+
}
31+
32+
?>
33+
-----
34+
<?php
35+
36+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;
37+
38+
use Symfony\Component\Console\Attribute\AsCommand;
39+
use Symfony\Component\Console\Command\Command;
40+
use Symfony\Component\Console\Input\InputInterface;
41+
use Symfony\Component\Console\Output\OutputInterface;
42+
use Symfony\Component\Console\Input\InputArgument;
43+
use Symfony\Component\Console\Input\InputOption;
44+
45+
#[AsCommand(name: 'some_name')]
46+
final class WithOptionalArgument
47+
{
48+
public function __invoke(#[\Symfony\Component\Console\Attribute\Argument(name: 'argument', mode: InputArgument::OPTIONAL, description: 'Argument description')]
49+
?string $argument, #[\Symfony\Component\Console\Attribute\Command\Option]
50+
$option, OutputInterface $output): int
51+
{
52+
$someArgument = $argument;
53+
$someOption = $option;
54+
55+
// ...
56+
57+
return 1;
58+
}
59+
}
60+
61+
?>

rules/Symfony73/NodeFactory/CommandInvokeParamsFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpParser\Node\Expr\Variable;
1111
use PhpParser\Node\Identifier;
1212
use PhpParser\Node\Name\FullyQualified;
13+
use PhpParser\Node\NullableType;
1314
use PhpParser\Node\Param;
1415
use Rector\PhpParser\Node\Value\ValueResolver;
1516
use Rector\Symfony\Enum\SymfonyAttribute;
@@ -50,6 +51,12 @@ private function createArgumentParams(array $commandArguments): array
5051
)));
5152

5253
$argumentParam->type = new Identifier('string');
54+
55+
$modeValue = $this->valueResolver->getValue($commandArgument->getMode());
56+
if ($modeValue === null || $modeValue === 2) {
57+
$argumentParam->type = new NullableType($argumentParam->type);
58+
}
59+
5360
// @todo fill type or default value
5461
// @todo default string, multiple values array
5562

0 commit comments

Comments
 (0)