[FrameworkBundle] Let GetToConstructorInjectionRector work with trait as well

This commit is contained in:
Tomas Votruba 2018-08-09 11:14:35 +02:00
parent 218806227b
commit 24c6cc3d9f
6 changed files with 64 additions and 3 deletions

View File

@ -14,9 +14,17 @@ final class GetToConstructorInjectionRector extends AbstractToConstructorInjecti
*/
private $controllerClass;
public function __construct(string $controllerClass = 'Symfony\Bundle\FrameworkBundle\Controller\Controller')
{
/**
* @var string
*/
private $traitClass;
public function __construct(
string $controllerClass = 'Symfony\Bundle\FrameworkBundle\Controller\Controller',
string $traitClass = 'Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait'
) {
$this->controllerClass = $controllerClass;
$this->traitClass = $traitClass;
}
public function getDefinition(): RectorDefinition
@ -61,6 +69,10 @@ CODE_SAMPLE
return false;
}
return $this->methodCallAnalyzer->isTypeAndMethod($node, $this->controllerClass, 'get');
if ($this->methodCallAnalyzer->isTypeAndMethod($node, $this->controllerClass, 'get')) {
return true;
}
return $this->methodCallAnalyzer->isTypeAndMethod($node, $this->traitClass, 'get');
}
}

View File

@ -0,0 +1,21 @@
<?php declare (strict_types=1);
use Rector\Symfony\Tests\Rector\FrameworkBundle\GetToConstructorInjectionRector\Source\GetTrait;
class ClassWithNamedServiceAndTrait
{
use GetTrait;
/**
* @var \Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeTranslator
*/
private $someTranslator;
public function __construct(\Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeTranslator $someTranslator)
{
$this->someTranslator = $someTranslator;
}
public function render()
{
$this->someTranslator;
}
}

View File

@ -29,6 +29,7 @@ final class GetToConstructorInjectionRectorTest extends AbstractRectorTestCase
{
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'];
yield [__DIR__ . '/Wrong/wrong3.php.inc', __DIR__ . '/Correct/correct3.php.inc'];
}
protected function provideConfig(): string

View File

@ -0,0 +1,13 @@
<?php declare(strict_types=1);
namespace Rector\Symfony\Tests\Rector\FrameworkBundle\GetToConstructorInjectionRector\Source;
trait GetTrait
{
/**
* @return object
*/
public function get(string $serviceName)
{
}
}

View File

@ -0,0 +1,13 @@
<?php declare (strict_types=1);
use Rector\Symfony\Tests\Rector\FrameworkBundle\GetToConstructorInjectionRector\Source\GetTrait;
class ClassWithNamedServiceAndTrait
{
use GetTrait;
public function render()
{
$this->get('translator');
}
}

View File

@ -4,3 +4,4 @@ parameters:
services:
Rector\Symfony\Rector\FrameworkBundle\GetToConstructorInjectionRector:
$controllerClass: 'Rector\Symfony\Tests\Rector\Source\SymfonyController'
$traitClass: 'Rector\Symfony\Tests\Rector\FrameworkBundle\GetToConstructorInjectionRector\Source\GetTrait'