Skip to content
Open
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
@@ -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 @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
Expand Down Expand Up @@ -167,7 +168,13 @@ public function refactor(Node $node): ?Class_
$invokeParams = [];
}

$executeClassMethod->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
$executeClassMethod->params = array_merge(
array_filter($executeClassMethodParams, fn(Param $param) => is_null($param->default)),
array_filter($executeClassMethodParams, fn(Param $param) => !is_null($param->default)),
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@florisluiten this seems cause errors in tests fixture, see

https://github.com/rectorphp/rector-symfony/actions/runs/20064007047/job/57548394124?pr=899

Please verify other expected output fixtures, you can test locally:

composer install
vendor/bin/phpunit


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