mirror of
https://github.com/rectorphp/rector.git
synced 2025-03-22 00:09:54 +01:00
[FrameworkBundle] Let GetToConstructorInjectionRector work with trait as well
This commit is contained in:
parent
218806227b
commit
24c6cc3d9f
@ -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');
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
@ -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');
|
||||
}
|
||||
}
|
@ -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'
|
||||
|
Loading…
x
Reference in New Issue
Block a user