Find a Rector rule

Find the best Rector rule to solve your problem. Searching through 1005 rules.

Found 9 rules, grouped by set: Clear

symfony/framework-bundle 9 matches
EnableValidationAttributesRector symfony/framework-bundle >=6.4

Enable "framework.validation.enable_attributes" config, to load validation rules from attributes

 $container->loadFromExtension('framework', [
     'validation' => [
-        'enable_attributes' => false,
+        'enable_attributes' => true,
     ],
 ]);
GetHelperControllerToServiceRector symfony/framework-bundle >=6.0

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);
     }
 }

Simplify use of assertions in WebTestCase

 use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

 class SomeTest extends KernelTestCase
 {
     protected function setUp(): void
     {
-        $container = self::$container;
+        $container = self::getContainer();
     }
 }

Change RouteCollectionBuilder to RoutingConfiguratorRector

 use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
 use Symfony\Component\HttpKernel\Kernel;
-use Symfony\Component\Routing\RouteCollectionBuilder;
+use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

 final class ConcreteMicroKernel extends Kernel
 {
     use MicroKernelTrait;

-    protected function configureRoutes(RouteCollectionBuilder $routes)
+    protected function configureRouting(RoutingConfigurator $routes): void
     {
-        $routes->add('/admin', 'App\Controller\AdminController::dashboard', 'admin_dashboard');
-    }
-}
+        $routes->add('admin_dashboard', '/admin')
+            ->controller('App\Controller\AdminController::dashboard')
+    }}

Change Twig template short name to bundle syntax in render calls from controllers

 use Symfony\Bundle\FrameworkBundle\Controller\Controller;

 class BaseController extends Controller
 {
     function indexAction()
     {
-        $this->render('appBundle:Landing\Main:index.html.twig');
+        $this->render('@app/Landing/Main/index.html.twig');
     }
 }
FormTypeInstanceToClassConstRector symfony/framework-bundle >=3.0

Changes createForm(new FormType), add(new FormType) to ones with "FormType::class"

 use Symfony\Bundle\FrameworkBundle\Controller\Controller;

 final class SomeController extends Controller
 {
     public function action()
     {
-        $form = $this->createForm(new TeamType);
+        $form = $this->createForm(TeamType::class);
     }
 }
GetToConstructorInjectionRector symfony/framework-bundle >=2.8

Turns fetching of dependencies via $this->get() to constructor injection in Command and Controller

 use Symfony\Bundle\FrameworkBundle\Controller\Controller;

 final class SomeController extend Controller
 {
+    public function __construct(SomeService $someService)
+    {
+        $this->someService = $someService;
+    }
+
     public function someMethod()
     {
-        // ...
-        $this->get('some_service');
+        $this->someService;
     }
 }
RedirectToRouteRector symfony/framework-bundle >=2.6

Turns redirect to route to short helper method in Controller in Symfony

-$this->redirect($this->generateUrl("homepage"));
+$this->redirectToRoute("homepage");
GetRequestRector symfony/framework-bundle >=2.5

Turns fetching of Request via $this->getRequest() to action injection

+use Symfony\Component\HttpFoundation\Request;
+
 class SomeController
 {
-    public function someAction()
+    public function someAction(Request $request)
     {
-        return $this->getRequest()->getContent();
+        return $request->getContent();
     }
 }