Skip to content

ControllerMethodInjectionToConstructorRector removing parameters from definitions but not calls #9695

@Andrew-Staves-Activ

Description

@Andrew-Staves-Activ

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/44eaf6a6-8742-4d3e-a15b-be0127df8d83

<?php

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class ExampleController extends AbstractController
{
    public function example(Foo $foo)
    {
        $this->problematic($foo);
    }
    
    public function problematic(Foo $foo, ?string $optional = "test")
    {
        dump($foo);
        dump($optional);
    }
}

Responsible rules

  • ControllerMethodInjectionToConstructorRector

Expected Behaviour

 <?php
 
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

 
 class ExampleController extends AbstractController
 {
+    public function __construct(private readonly \Foo $foo)
+    {
+    }
+
-    public function example(Foo $foo)
+    public function example()
     {
-        $this->problematic($foo);
+        $this->problematic();
     }

-    public function problematic(Foo $foo, ?string $optional = "test")
+    public function problematic(?string $optional = "test")
     {
-        dump($foo);
+        dump($this->foo);
         dump($optional);
     }
 }

Or:

 <?php
 
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

 
 class ExampleController extends AbstractController
 {
+    public function __construct(private readonly \Foo $foo)
+    {
+    }
+
-    public function example(Foo $foo)
+    public function example()
    {
-        $this->problematic($foo);
+        $this->problematic($this->foo);
    }

    public function problematic(Foo $foo, ?string $optional = "test")
    {
        dump($foo);
        dump($optional);
    }
 }

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