Skip to content

Commit be33d7c

Browse files
committed
Setup test for order of optional parameters
In PHP, optional parameters should be declared after required parameters, otherwise they will be implicitly treated as required parameters.
1 parent 8360bd1 commit be33d7c

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 WithDefaultAndWithoutDefaultValue extends Command
14+
{
15+
protected function configure()
16+
{
17+
$this->addOption('option', 'o', InputOption::VALUE_NONE, 'Option description', false);
18+
$this->addOption('another', 'a', InputOption::VALUE_REQUIRED, 'No default value');
19+
}
20+
21+
protected function execute(InputInterface $input, OutputInterface $output): int
22+
{
23+
$someOption = $input->getOption('option');
24+
$another = $input->getOption('another');
25+
$output->writeln('Using output too');
26+
27+
// ...
28+
29+
return 1;
30+
}
31+
}
32+
33+
?>
34+
-----
35+
<?php
36+
37+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;
38+
39+
use Symfony\Component\Console\Attribute\AsCommand;
40+
use Symfony\Component\Console\Command\Command;
41+
use Symfony\Component\Console\Input\InputInterface;
42+
use Symfony\Component\Console\Output\OutputInterface;
43+
use Symfony\Component\Console\Input\InputArgument;
44+
use Symfony\Component\Console\Input\InputOption;
45+
46+
#[AsCommand(name: 'some_name')]
47+
final class WithDefaultAndWithoutDefaultValue
48+
{
49+
public function __invoke(#[\Symfony\Component\Console\Attribute\Option(name: 'another', shortcut: 'a', mode: InputOption::VALUE_REQUIRED, description: 'No default value')]
50+
$another, OutputInterface $output, #[\Symfony\Component\Console\Attribute\Option(name: 'option', shortcut: 'o', mode: InputOption::VALUE_NONE, description: 'Option description')]
51+
bool $option = false): int
52+
{
53+
$someOption = $option;
54+
$another = $another;
55+
$output->writeln('Using output too');
56+
57+
// ...
58+
59+
return 1;
60+
}
61+
}
62+
63+
?>

0 commit comments

Comments
 (0)