Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class OptionWithOptionalValue extends Command
{
protected function configure(): void
{
$this->addOption('some-array', null, InputOption::VALUE_IS_ARRAY, '', ['third value']);
$this->addOption('no-default-array', null, InputOption::VALUE_IS_ARRAY);
$this->addOption('some-array', null, InputOption::VALUE_IS_ARRAY, '', ['third value']);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
Expand All @@ -43,10 +43,10 @@ use Symfony\Component\Console\Output\OutputInterface;
class OptionWithOptionalValue
{
public function __invoke(
#[\Symfony\Component\Console\Attribute\Option(name: 'some-array', mode: InputOption::VALUE_IS_ARRAY)]
array $someArray = ['third value'],
#[\Symfony\Component\Console\Attribute\Option(name: 'no-default-array', mode: InputOption::VALUE_IS_ARRAY)]
array $noDefaultArray
array $noDefaultArray,
#[\Symfony\Component\Console\Attribute\Option(name: 'some-array', mode: InputOption::VALUE_IS_ARRAY)]
array $someArray = ['third value']
): int
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'some_name')]
final class WithDefaultAndWithoutDefaultValue extends Command
{
protected function configure()
{
$this->addOption('option', 'o', InputOption::VALUE_NONE, 'Option description', false);
$this->addOption('another', 'a', InputOption::VALUE_REQUIRED, 'No default value');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$someOption = $input->getOption('option');
$another = $input->getOption('another');
$output->writeln('Using output too');
// ...
return 1;
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector\Fixture;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'some_name')]
final class WithDefaultAndWithoutDefaultValue
{
public function __invoke(
#[\Symfony\Component\Console\Attribute\Option(name: 'another', shortcut: 'a', mode: InputOption::VALUE_REQUIRED, description: 'No default value')]
$another,
OutputInterface $output,
#[\Symfony\Component\Console\Attribute\Option(name: 'option', shortcut: 'o', mode: InputOption::VALUE_NONE, description: 'Option description')]
bool $option = false
): int
{
$someOption = $option;
$another = $another;
$output->writeln('Using output too');
// ...
return 1;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,13 @@ public function refactor(Node $node): ?Class_

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

$invokeClassMethod->params = array_merge($invokeParams, [$executeClassMethod->params[1]]);
$executeClassMethodParams = array_merge($invokeParams, [$executeClassMethod->params[1]]);

// Ensure that optional parameters are listed last in the argument list
$invokeClassMethod->params = array_merge(
array_filter($executeClassMethodParams, fn(Param $param) => is_null($param->default)),
array_filter($executeClassMethodParams, fn(Param $param) => !is_null($param->default)),
);

// 6. remove parent class
$node->extends = null;
Expand Down
Loading