Skip to content

[CodeQuality] Skip type-guarded classes + unique autowire method name in ControllerMethodInjectionToConstructorRector - #1007

Merged
TomasVotruba merged 2 commits into
mainfrom
type-guarded-controller-injection
Aug 4, 2026
Merged

[CodeQuality] Skip type-guarded classes + unique autowire method name in ControllerMethodInjectionToConstructorRector#1007
TomasVotruba merged 2 commits into
mainfrom
type-guarded-controller-injection

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Aug 4, 2026

Copy link
Copy Markdown
Member

Two changes to ControllerMethodInjectionToConstructorRector.

1. Skip ->withTypeGuardedClasses([])

Removing action method params changes the method signature. If the controller is - or extends - a class listed in ->withTypeGuardedClasses([]), that is a breaking change for its child classes, so the rule leaves such classes untouched. Same guard as ResponseReturnTypeControllerActionRector already uses; final classes are never guarded, as they cannot be extended.

return RectorConfig::configure()
    ->withRules([ControllerMethodInjectionToConstructorRector::class])
    ->withTypeGuardedClasses([AbstractCustomController::class]);
 class SomeController extends AbstractCustomController
 {
-    public function someAction(LoggerInterface $logger)
+    public function __construct(private readonly LoggerInterface $logger)
+    {
+    }
+
+    public function someAction()
     {
-        $logger->log('level', 'value');
+        $this->logger->log('level', 'value');
     }
 }

With AbstractCustomController type-guarded, the change above is skipped. Non-guarded controllers still change as before.

2. autowire<ShortClassName>() method name

The added #[Required] method is now named after the class, so it never collides with an autowire() in a parent or sibling class. Replaces the previous autowire() / autowireServices() fallback pair.

 final class SomeController extends SomeParentControllerWithConstructor
 {
+    private SomeService $someService;
+
     #[Route('/some-path', name: 'some_name')]
-    public function someAction(SomeService $someService)
+    public function someAction()
     {
-        $data = $someService->getData();
+        $data = $this->someService->getData();
     }
+
+    #[Required]
+    public function autowireSomeController(SomeService $someService): void
+    {
+        $this->someService = $someService;
+    }
 }

@TomasVotruba TomasVotruba changed the title [CodeQuality] Skip type-guarded classes in ControllerMethodInjectionToConstructorRector [CodeQuality] Skip type-guarded classes + unique autowire method name in ControllerMethodInjectionToConstructorRector Aug 4, 2026
@TomasVotruba
TomasVotruba merged commit 6c7e215 into main Aug 4, 2026
7 checks passed
@TomasVotruba
TomasVotruba deleted the type-guarded-controller-injection branch August 4, 2026 10:27
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