Skip to content

Commit e5c836e

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 e5c836e

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+
return 1;
28+
}
29+
}
30+
31+
?>
32+
-----
33+
<?php
34+
35+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;
36+
37+
use Symfony\Component\Console\Attribute\AsCommand;
38+
use Symfony\Component\Console\Command\Command;
39+
use Symfony\Component\Console\Input\InputInterface;
40+
use Symfony\Component\Console\Output\OutputInterface;
41+
use Symfony\Component\Console\Input\InputArgument;
42+
use Symfony\Component\Console\Input\InputOption;
43+
44+
#[AsCommand(name: 'some_name')]
45+
final class WithDefaultAndWithoutDefaultValue
46+
{
47+
public function __invoke(
48+
#[\Symfony\Component\Console\Attribute\Option(name: 'another', shortcut: 'a', mode: InputOption::VALUE_REQUIRED, description: 'No default value')]
49+
$another,
50+
OutputInterface $output,
51+
#[\Symfony\Component\Console\Attribute\Option(name: 'option', shortcut: 'o', mode: InputOption::VALUE_NONE, description: 'Option description')]
52+
bool $option = false
53+
): int
54+
{
55+
$someOption = $option;
56+
$another = $another;
57+
$output->writeln('Using output too');
58+
// ...
59+
return 1;
60+
}
61+
}
62+
63+
?>

0 commit comments

Comments
 (0)