Skip to content

[CodeQuality] Re-use existing #[Required] method in ControllerMethodInjectionToConstructorRector - #1008

Merged
TomasVotruba merged 1 commit into
mainfrom
reuse-existing-required-method
Aug 4, 2026
Merged

[CodeQuality] Re-use existing #[Required] method in ControllerMethodInjectionToConstructorRector#1008
TomasVotruba merged 1 commit into
mainfrom
reuse-existing-required-method

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

When a controller already has an injection method – one with #[Required], or named autowire*() – the rule now appends to it, instead of creating a second autowire<ShortClassName>() method next to it.

 final class ReUseExistingRequiredMethod extends ParentControllerWithConstructor
 {
     private TranslatorInterface $translator;
+    private \Psr\Log\LoggerInterface $logger;

     #[Required]
-    public function autowireTranslator(TranslatorInterface $translator): void
+    public function autowireTranslator(TranslatorInterface $translator, \Psr\Log\LoggerInterface $logger): void
     {
         $this->translator = $translator;
+        $this->logger = $logger;
     }

     #[Route('/example', name: 'example')]
-    public function example(LoggerInterface $logger)
+    public function example()
     {
-        $logger->log('level', $this->translator->trans('value'));
+        $this->logger->log('level', $this->translator->trans('value'));
     }
 }

Only public, non-static methods with a body are considered. If none is found, a fresh autowire<ShortClassName>() method is created as before.

@TomasVotruba
TomasVotruba merged commit 7c4584c into main Aug 4, 2026
7 checks passed
@TomasVotruba
TomasVotruba deleted the reuse-existing-required-method branch August 4, 2026 11:45
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