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!
Bug Report
Minimal PHP Code Causing Issue
See https://getrector.com/demo/44eaf6a6-8742-4d3e-a15b-be0127df8d83
Responsible rules
ControllerMethodInjectionToConstructorRectorExpected 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!