Find the best Rector rule to solve your problem. Searching through 728 rules.
Found 6 rules:
Moves $this->setHelp() to the "help" named argument of #[AsCommand]
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
-#[AsCommand(name: 'app:some')]
+#[AsCommand(name: 'app:some', help: <<<'TXT'
+Some help text
+TXT)]
final class SomeCommand extends Command
{
- protected function configure(): void
- {
- $this->setHelp('Some help text');
- }
}
Changes getFilters() in TwigExtension to #[TwigFilter] marker attribute above function
use Twig\Extension\AbstractExtension;
+use Twig\Attribute\AsTwigFilter;
class SomeClass extends AbstractExtension
{
- public function getFilters()
- {
- return [
- new \Twig\TwigFilter('filter_name', [$this, 'localMethod']),
- ];
- }
-
+ #[TwigFilter('filter_name')]
public function localMethod($value)
{
return $value;
}
}
Refactor Symfony constraints using array options to named arguments syntax for better readability and type safety.
use Symfony\Component\Validator\Constraints\NotBlank;
-$constraint = new NotBlank(['message' => 'This field should not be blank.']);
+$constraint = new NotBlank(message: 'This field should not be blank.');
Changes getFunctions() in TwigExtension to #[AsTwigFunction] marker attribute above local class method
use Twig\Extension\AbstractExtension;
+use Twig\Attribute\AsTwigFunction;
class SomeClass extends AbstractExtension
{
- public function getFunctions()
- {
- return [
- new \Twig\TwigFunction('function_name', [$this, 'localMethod']),
- ];
- }
-
+ #[AsTwigFunction('function_name')]
public function localMethod($value)
{
return $value;
}
}
Adds a new $voter
argument in protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token, ?Vote $vote = null): bool
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
final class MyVoter extends Voter
{
protected function supports(string $attribute, mixed $subject): bool
{
return true;
}
- protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
+ protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token, ?\Symfony\Component\Security\Core\Authorization\Voter\Vote $vote = null): bool
{
return true;
}
}
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\Argument;
+use Symfony\Component\Console\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(name: 'argument', description: 'Argument description')]
+ string $argument,
+ #[Option]
+ bool $option = false,
+ ) {
+ $someArgument = $argument;
+ $someOption = $option;
// ...
return 1;
}
}