Skip to content

Commit 2e8946a

Browse files
committed
Potential fix
1 parent e5c836e commit 2e8946a

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

rules-tests/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector/Fixture/DefaultValue/option_with_array_type.php.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class OptionWithOptionalValue extends Command
1616
{
1717
protected function configure(): void
1818
{
19-
$this->addOption('some-array', null, InputOption::VALUE_IS_ARRAY, '', ['third value']);
2019
$this->addOption('no-default-array', null, InputOption::VALUE_IS_ARRAY);
20+
$this->addOption('some-array', null, InputOption::VALUE_IS_ARRAY, '', ['third value']);
2121
}
2222
protected function execute(InputInterface $input, OutputInterface $output): int
2323
{
@@ -43,10 +43,10 @@ use Symfony\Component\Console\Output\OutputInterface;
4343
class OptionWithOptionalValue
4444
{
4545
public function __invoke(
46-
#[\Symfony\Component\Console\Attribute\Option(name: 'some-array', mode: InputOption::VALUE_IS_ARRAY)]
47-
array $someArray = ['third value'],
4846
#[\Symfony\Component\Console\Attribute\Option(name: 'no-default-array', mode: InputOption::VALUE_IS_ARRAY)]
49-
array $noDefaultArray
47+
array $noDefaultArray,
48+
#[\Symfony\Component\Console\Attribute\Option(name: 'some-array', mode: InputOption::VALUE_IS_ARRAY)]
49+
array $someArray = ['third value']
5050
): int
5151
{
5252
}

rules/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,13 @@ public function refactor(Node $node): ?Class_
160160

161161
$invokeParams = $this->createInvokeParams($node);
162162

163-
$invokeClassMethod->params = array_merge($invokeParams, [$executeClassMethod->params[1]]);
163+
$executeClassMethodParams = array_merge($invokeParams, [$executeClassMethod->params[1]]);
164+
165+
// Ensure that optional parameters are listed last in the argument list
166+
$invokeClassMethod->params = array_merge(
167+
array_filter($executeClassMethodParams, fn(Param $param) => is_null($param->default)),
168+
array_filter($executeClassMethodParams, fn(Param $param) => !is_null($param->default)),
169+
);
164170

165171
// 6. remove parent class
166172
$node->extends = null;

0 commit comments

Comments
 (0)