Skip to content

[CodeQuality] Re-use parent protected property in ControllerMethodInjectionToConstructorRector - #1003

Merged
TomasVotruba merged 1 commit into
mainfrom
reuse-parent-protected-property
Aug 3, 2026
Merged

[CodeQuality] Re-use parent protected property in ControllerMethodInjectionToConstructorRector#1003
TomasVotruba merged 1 commit into
mainfrom
reuse-parent-protected-property

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

When a parent controller already exposes the very same service as a protected/public property, the rule added a duplicate constructor property with the same name — shadowing the parent one.

Now the parent property is re-used: the action param is still removed and its usage rewritten to $this->..., but no new constructor dependency is added.

Parent:

abstract class ParentController extends AbstractController
{
    public function __construct(protected LoggerInterface $logger)
    {
    }
}

Child:

 final class SomeController extends ParentController
 {
     #[Route('/example', name: 'example')]
-    public function example(LoggerInterface $logger)
+    public function example()
     {
-        $logger->log('level', 'value');
+        $this->logger->log('level', 'value');
     }
 }

A private parent property is still not accessible, so that case keeps adding an own dependency (covered by the existing child_of_parent_with_private_promoted_property fixture). The re-use also requires the parent property type to be compatible with the param type.

@TomasVotruba
TomasVotruba merged commit eb05b11 into main Aug 3, 2026
7 checks passed
@TomasVotruba
TomasVotruba deleted the reuse-parent-protected-property branch August 3, 2026 22:13
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