Skip to content

[depre] Deprecate GetRepositoryServiceLocatorToRepositoryServiceInjectionRector - #498

Merged
TomasVotruba merged 1 commit into
mainfrom
deprecate-get-repository-service-locator
Aug 3, 2026
Merged

[depre] Deprecate GetRepositoryServiceLocatorToRepositoryServiceInjectionRector#498
TomasVotruba merged 1 commit into
mainfrom
deprecate-get-repository-service-locator

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Aug 3, 2026

Copy link
Copy Markdown
Member

This rule turns $this->getRepository(SomeEntity::class) into a constructor-injected repository:

 final class SomeClass
 {
+    public function __construct(
+        private SomeEntityRepository $someEntityRepository
+    ) {
+    }
+
     public function run()
     {
-        return $this->getRepository(SomeEntity::class)->find(1);
+        return $this->someEntityRepository->find(1);
     }
 }

To do that, it has to know which repository belongs to the entity. RepositoryClassResolver did it by reading the entity file contents and running a regular expression over them:

private const string QUOTED_REPOSITORY_CLASS_REGEX = '#repositoryClass=\"(?<repositoryClass>.*?)\"#';

private const string USE_REPOSITORY_REGEX = '#use (?<repositoryClass>.*?Repository);#';

If the repositoryClass="..." annotation is not matched, it falls back to picking the first use ...Repository; import in the file. That silently injects the wrong repository whenever an entity imports any other class ending in Repository, and it misses attribute mapping (#[ORM\Entity(repositoryClass: ...)]) entirely.

That is a custom job for a specific project layout, not something a generic rule can do safely. Deprecating it - use a custom rule tailored to your own mapping instead.

The rule was never part of any set, so it only affected users who registered the class by hand.

@TomasVotruba
TomasVotruba force-pushed the deprecate-get-repository-service-locator branch from d0fe0cc to 15fed06 Compare August 3, 2026 15:51
…tionRector

The rule resolved the repository class by reading the entity file contents
with a regular expression, falling back to the first "use ...Repository;"
import. That is too fragile and project specific for a generic rule.
@TomasVotruba
TomasVotruba force-pushed the deprecate-get-repository-service-locator branch from 15fed06 to 8c613e8 Compare August 3, 2026 15:54
@TomasVotruba
TomasVotruba merged commit da3afa9 into main Aug 3, 2026
7 checks passed
@TomasVotruba
TomasVotruba deleted the deprecate-get-repository-service-locator branch August 3, 2026 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant