rector-symfony icon indicating copy to clipboard operation
rector-symfony copied to clipboard

GetToConstructorInjectionRector

Open javaDeveloperKid opened this issue 3 years ago • 5 comments

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.

javaDeveloperKid avatar Aug 20 '22 14:08 javaDeveloperKid

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.

TomasVotruba avatar Aug 20 '22 14:08 TomasVotruba

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.

javaDeveloperKid avatar Aug 20 '22 14:08 javaDeveloperKid

We'll need the expected code snippet.

TomasVotruba avatar Aug 20 '22 14:08 TomasVotruba

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).

javaDeveloperKid avatar Aug 20 '22 14:08 javaDeveloperKid

I will try to provide a code snippet.

javaDeveloperKid avatar Sep 13 '22 16:09 javaDeveloperKid

I will try to provide a code snippet.

Can you provide it?

johanadivare avatar Dec 06 '22 07:12 johanadivare

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:

TomasVotruba avatar Jun 24 '23 08:06 TomasVotruba