Rectify: Turn All Action Injects to Constructor Injection in Your Symfony Application

Action Injections are much fun, but it can turn your project to legacy very fast. How to refactor out of the legacy back to constructor injection and still keep that smile on your face?

I wrote about How to Slowly Turn your Symfony Project to Legacy with Action Injection a few weeks ago. It surprised me that the approach had mostly positive feedback:

Couldn't agree more with pretty much everything said! Action injection makes it really confusing on whether an object is treated stateful or stateless (very grey area with the Session for example).
Iltar van der Berg
I'm a Symfony trainer and I'm told to teach people how to use Symfony and talk about this injection pattern. Sob.
Alex Rock
I'm working on a Project that uses action injection pattern and I hate it. I like autowiring but the whole idea about action injection is broken. And this project is in sf28 do we don't use autowiring. Maintainance and development with this pattern is a total nightmare.
A


It's natural to try new patterns with an open heart and validate them in practice, but what if you find this way as not ideal and want to go to constructor injection instead?

How would you change all your 50 controllers with action injections...

<?php declare(strict_types=1);

namespace App\Controller;

final class SomeController
{
    public function detail(int $id, Request $request, ProductRepository $productRepository)
    {
        $this->validateRequest($request);
        $product = $productRepository->find($id);
        // ...
    }
}

...to the constructor injection:

<?php declare(strict_types=1);

namespace App\Controller;

final class SomeController
{
    /**
     * @var ProductRepository
     */
    private $productRepository;

    public function __construct(ProductRepository $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    public function detail(int $id, Request $request)
    {
        $this->validateRequest($request);
        $product = $this->productRepository->find($id);
        // ...
    }
}

How to Waste Week in 1 Team


I find the time of my team very precious, don't you? So I Let Rector do the work.

3 Steps to Instant Refactoring of All Controllers

1. Install Rector

composer install rector/rector --dev

2. Prepare Config

Import the action-injection-to-constructor-injection level and configure your Kernel class name.

# rector.yml
imports:
    - { resource: 'vendor/rector/rector/config/level/architecture/action-injection-to-constructor-injection.yml' }

parameters:
    kernel_class: 'App\Kernel' # the default value


Do you have App\Kernel in your application? Use --level in CLI this instead of rector.yml:

vendor/bin/rector ... --level action-injection-to-constructor-injection

3. Run Rector on Your Code

vendor/bin/rector process /app --dry-run

You should see diffs like:

 <?php declare(strict_types=1);

 namespace App\Controller;

 final class SomeController
 {
+    /**
+     * @var ProductRepository
+     */
+    private $productRepository;
+
+    public function __construct(ProductRepository $productRepository)
+    {
+        $this->productRepository = $productRepository;
+    }
+
-    public function detail(int $id, Request $request, ProductRepository $productRepository)
+    public function detail(int $id, Request $request)
     {
         $this->validateRequest($request);
-        $product = $productRepository->find($id);
+        $product = $this->productRepository->find($id);
         // ...
     }
 }

3. Run It

Are all looking good? Run it:

vendor/bin/rector process /app

Clean Code... Done, but What About Beautiful?

You've probably noticed that code itself is not looking too good. Rector's job is not to clean, but to change the code. It's not a hipster designer, but rather a thermonuclear engineer. That's why there are coding standards. You can apply your own or if not good enough use Rector's prepared set:

composer require symplify/easy-coding-standard --dev
vendor/bin/ecs --config vendor/rector/rector/ecs-after-rector.yml --fix

And your code is now both refactored and clean. That's it!



Happy instant refactoring!



What do you think?


GitHub RSS @votrubaT Runs on Statie Hosted on GitHub

Like what I write about? Hire me & we can work together