Find the best Rector rule to solve your problem


ReturnTypeFromSymfonySerializerRector

Add return type from symfony serializer

 final class SomeClass
 {
     private \Symfony\Component\Serializer\Serializer $serializer;

-    public function resolveEntity($data)
+    public function resolveEntity($data): SomeType
     {
         return $this->serializer->deserialize($data, SomeType::class, 'json');
     }
 }

DowngradeSymfonyCommandAttributeRector

Downgrade Symfony Command Attribute

 #[AsCommand(name: 'app:create-user', description: 'some description')]
 class CreateUserCommand extends Command
 {
+    protected function configure(): void
+    {
+        $this->setName('app:create-user');
+        $this->setDescription('some description');
+    }
 }

MaxLengthSymfonyFormOptionToAttrRector

Change form option "max_length" to a form "attr" > "max_length"

 $formBuilder = new Symfony\Component\Form\FormBuilder();

 $form = $formBuilder->create('name', 'text', [
-    'max_length' => 123,
+    'attr' => ['maxlength' => 123],
 ]);
SETS:  symfony/* 2.5

ReplaceSensioRouteAnnotationWithSymfonyRector

Replace Sensio @Route annotation with Symfony one

-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+use Symfony\Component\Routing\Annotation\Route;

 final class SomeClass
 {
     /**
      * @Route()
      */
     public function run()
     {
     }
 }
SETS:  symfony/* 3.4

GetBySymfonyStringToConstructorInjectionRector

Converts typical Symfony services like $this->get("validator") in commands/controllers to constructor injection (step 3/x)

 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\Validator\Validator\ValidatorInterface;

 final class SomeController extends Controller
 {
+    public function __construct(private ValidatorInterface $validator)
+
     public function someMethod()
     {
-        $someType = $this->get('validator');
+        $someType = $this->validator;
     }
 }

Configurable

AddPropertyTypeDeclarationRector

Add type to property by added rules, mostly public/property by parent type

 class SomeClass extends ParentClass
 {
-    public $name;
+    public string $name;
 }

StringFormTypeToClassRector

Turns string Form Type references to their CONSTANT alternatives in FormTypes in Form in Symfony. To enable custom types, add link to your container XML dump in "$rectorConfig->symfonyContainerXml(...)"

 $formBuilder = new Symfony\Component\Form\FormBuilder;
-$formBuilder->add('name', 'form.type.text');
+$formBuilder->add('name', \Symfony\Component\Form\Extension\Core\Type\TextType::class);
SETS:  symfony/* 3.0

ServiceSetStringNameToClassNameRector

Change $service->set() string names to class-type-based names, to allow $container->get() by types in Symfony 2.8. Provide XML config via $rectorConfig->symfonyContainerXml(...);

 use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

 return static function (ContainerConfigurator $containerConfigurator): void {
     $services = $containerConfigurator->services();

-    $services->set('some_name', App\SomeClass::class);
+    $services->set('app\\someclass', App\SomeClass::class);
 };
SETS:  Configs

ValidationRuleArrayStringValueToArrayRector

Convert string validation rules into arrays for Laravel's Validator.

 Validator::make($data, [
-    'field' => 'required|nullable|string|max:255',
+    'field' => ['required', 'nullable', 'string', 'max:255'],
 ]);