Skip to content

Recognise Doctrine's EntityManagerInterface in ControllerMethodInjectionToConstructorRector #9692

@Andrew-Staves-Activ

Description

@Andrew-Staves-Activ

Feature Request

When running the ControllerMethodInjectionToConstructorRector rule, it would be handy (for my team's use case, at least) if Rector treated Doctrine\ORM\EntityManagerInterface as an injected dependency that can be moved to the constructor.

Currently, Rector will skip/ignore EntityManagerInterface - see https://getrector.com/demo/35aee427-2c85-40d0-9d12-fc64c86d7f82.

I'm guessing this is because (for us) the parameter gets injected based on it being bound in the services configuration -i.e.:

services:
    _defaults:
        autoconfigure: true
        autowire: true
        bind:
            $em: "@doctrine.orm.Main_entity_manager"
            $emFoo: "@doctrine.orm.Foo_entity_manager"
        # [...]

Desired Behaviour

<?php
 
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

 class ExampleController extends AbstractController
 {
+	public function __construct(private readonly EntityManagerInterface $em)
+    {
+    }
+
-	public function example(EntityManagerInterface $em): void
+	public function example(): void
	{
-		dump($em);
+		dump($this->em);
	}
 }

If this wouldn't be considered generally useful in an "always-on" form, perhaps ControllerMethodInjectionToConstructorRector could take a list of class names to treat the same as (non-bound) services?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions