rector-symfony
rector-symfony copied to clipboard
GetToConstructorInjectionRector
Bug Report
| Subject | Details |
|---|---|
| Rector version | e.g. v0.13.10 (invoke vendor/bin/rector --version) |
Current behaviour of GetToConstructorInjectionRector is that it creates a property name from the service's class name. When we refactor a controller that fetches from container two different services but they are of the same type we break the app.
class MyServiceClass {
public function __construct(string $value)
{
}
}
class MyController
{
public function firstAction()
{
$service = $this->get('app.service.my_service_class_v1');
}
public function secondAction()
{
$service = $this->get('app.service.my_service_class_v2');
}
}
services:
app.service.my_service_class_v1:
class: MyServiceClass
arguments: ['someValue']
app.service.my_service_class_v2:
class: MyServiceClass
arguments: ['otherValue']
After running GetToConstructorInjectionRector we end up with:
class MyController
{
private MyServiceClass $myServiceClass;
public function __construct(MyServiceClass $myServiceClass)
{
$this->myServiceClass = $myServiceClass;
}
public function firstAction()
{
$service = $this->myServiceClass;
}
public function secondAction()
{
$service = $this->myServiceClass;
}
Expected Behaviour
There should be created two separate class properties.
This looks like Symfony set rule, so the issue should this repository: https://github.com/rectorphp/rector-symfony
We'll need the expected code snippet... I don't think it's possible to be honest. This one would have to be skipped, as autowiring same type twice is not valid.
This case cannot be autowired of course but this is not the problem I wanted to show. The problem is that this rector should create 2 separate properties.
We'll need the expected code snippet.
I would just add some suffix to the variable name, let's say $myServiceClass2. Can't thing about any solution that wouldn't require manual property name change (Rector can't know how developer wants to name his fields with the same type but storing other instances).
I will try to provide a code snippet.
I will try to provide a code snippet.
Can you provide it?
Closing for lack of feedback. Failing test case would be best to finish this, so we see what is going on and what needs to be done :+1: