Change Symfony Command with execute() + configure() to __invoke() with attributes
-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;
+use Symfony\Component\Console\Command\Argument;
+use Symfony\Component\Console\Command\Option;
-#[AsCommand(name: 'some_name')]
-final class SomeCommand extends Command
+final class SomeCommand
{
- public function configure()
- {
- $this->addArgument('argument', InputArgument::REQUIRED, 'Argument description');
- $this->addOption('option', 'o', InputOption::VALUE_NONE, 'Option description');
- }
-
- public function execute(InputInterface $input, OutputInterface $output)
- {
- $someArgument = $input->getArgument('argument');
- $someOption = $input->getOption('option');
+ public function __invoke(
+ #[Argument]
+ string $argument,
+ #[Option]
+ bool $option = false,
+ ) {
+ $someArgument = $argument;
+ $someOption = $option;
// ...
return 1;
}
}
Configure your rector.php
:
<?php
use Rector\Config\RectorConfig;
use Rector\Symfony\Symfony73\Rector\Class_\InvokableCommandRector;
return RectorConfig::configure()
->withRules([
InvokableCommandRector::class,
]);