Find the best Rector rule to solve your problem



Configurable

RenameClassRector

Replaces defined classes by new ones.

 namespace App;

-use SomeOldClass;
+use SomeNewClass;

-function someFunction(SomeOldClass $someOldClass): SomeOldClass
+function someFunction(SomeNewClass $someOldClass): SomeNewClass
 {
-    if ($someOldClass instanceof SomeOldClass) {
-        return new SomeOldClass;
+    if ($someOldClass instanceof SomeNewClass) {
+        return new SomeNewClass;
     }
 }

Configurable

AnnotationToAttributeRector

Change annotation to attribute

 use Symfony\Component\Routing\Annotation\Route;

 class SymfonyRoute
 {
-    /**
-     * @Route("/path", name="action") api route
-     */
+    #[Route(path: '/path', name: 'action')] // api route
     public function action()
     {
     }
 }

Configurable

AddParamTypeDeclarationRector

Add param types where needed

 class SomeClass
 {
-    public function process($name)
+    public function process(string $name)
     {
     }
 }

Configurable

AddReturnTypeDeclarationRector

Changes defined return typehint of method and class.

 class SomeClass
 {
-    public function getData()
+    public function getData(): array
     {
     }
 }

GetHelperControllerToServiceRector

Replace $this->getDoctrine() and $this->dispatchMessage() calls in AbstractController with direct service use

 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Doctrine\Persistence\ManagerRegistry;

 final class SomeController extends AbstractController
 {
+    public function __construct(
+        private ManagerRegistry $managerRegistry
+    ) {
+    }
+
     public function run()
     {
-        $productRepository = $this->getDoctrine()->getRepository(Product::class);
+        $productRepository = $this->managerRegistry->getRepository(Product::class);
     }
 }
SETS:  symfony/* 6.0

Configurable

ReplaceServiceArgumentRector

Replace defined service() argument in Symfony PHP config

 use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

-return service(ContainerInterface::class);
+return service('service_container');
SETS:  symfony/* 6.0