mirror of
https://github.com/rectorphp/rector.git
synced 2024-07-30 21:30:23 +00:00
ActionInjectionToConstructorInjectionRector prototype
This commit is contained in:
parent
450228e788
commit
a15c177a73
@ -0,0 +1,2 @@
|
||||
services:
|
||||
Rector\Rector\Architecture\DependencyInjection\ActionInjectionToConstructorInjectionRector: ~
|
@ -0,0 +1,135 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Architecture\DependencyInjection;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Builder\Class_\VariableInfoFactory;
|
||||
use Rector\Builder\ConstructorMethodBuilder;
|
||||
use Rector\Builder\PropertyBuilder;
|
||||
use Rector\Node\Attribute;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
final class ActionInjectionToConstructorInjectionRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var PropertyBuilder
|
||||
*/
|
||||
private $propertyBuilder;
|
||||
|
||||
/**
|
||||
* @var ConstructorMethodBuilder
|
||||
*/
|
||||
private $constructorMethodBuilder;
|
||||
|
||||
/**
|
||||
* @var VariableInfoFactory
|
||||
*/
|
||||
private $variableInfoFactory;
|
||||
|
||||
public function __construct(
|
||||
PropertyBuilder $propertyBuilder,
|
||||
ConstructorMethodBuilder $constructorMethodBuilder,
|
||||
VariableInfoFactory $variableInfoFactory
|
||||
) {
|
||||
$this->propertyBuilder = $propertyBuilder;
|
||||
$this->constructorMethodBuilder = $constructorMethodBuilder;
|
||||
$this->variableInfoFactory = $variableInfoFactory;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof ClassMethod) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Strings::endsWith((string) $node->getAttribute(Attribute::CLASS_NAME), 'Controller');
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Turns action injection in Controllers to constructor injection', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeController
|
||||
{
|
||||
public function default(ProductRepository $productRepository)
|
||||
{
|
||||
$products = $productRepository->fetchAll();
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
final class SomeController
|
||||
{
|
||||
/**
|
||||
* @var ProductRepository
|
||||
*/
|
||||
private $productRepository;
|
||||
public function __construct(ProductRepository $productRepository)
|
||||
{
|
||||
$this->productRepository = $productRepository;
|
||||
}
|
||||
|
||||
public function default()
|
||||
{
|
||||
$products = $this->productRepository->fetchAll();
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$classNode = $node->getAttribute(Attribute::CLASS_NODE);
|
||||
|
||||
foreach ($node->params as $paramNode) {
|
||||
if (! $this->isActionInjectedParamNode($paramNode)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$propertyInfo = $this->variableInfoFactory->createFromNameAndTypes(
|
||||
$paramNode->var->name,
|
||||
[(string) $paramNode->type]
|
||||
);
|
||||
|
||||
// add property
|
||||
$this->propertyBuilder->addPropertyToClass($classNode, $propertyInfo);
|
||||
|
||||
// pass via constructor
|
||||
$this->constructorMethodBuilder->addSimplePropertyAssignToClass($classNode, $propertyInfo);
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function isActionInjectedParamNode(Param $paramNode): bool
|
||||
{
|
||||
$typehint = (string) $paramNode->type;
|
||||
|
||||
if (empty($typehint)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Strings::endsWith($typehint, 'Request')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// skip non-classy types
|
||||
if (! ctype_upper($typehint[0])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Architecture\DependencyInjection\ActionInjectionToConstructorInjectionRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
/**
|
||||
* @covers \Rector\Rector\Architecture\DependencyInjection\ActionInjectionToConstructorInjectionRector
|
||||
*/
|
||||
final class ActionInjectionToConstructorInjectionRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideWrongToFixedFiles()
|
||||
*/
|
||||
public function test(string $wrong, string $fixed): void
|
||||
{
|
||||
$this->doTestFileMatchesExpectedContent($wrong, $fixed);
|
||||
}
|
||||
|
||||
public function provideWrongToFixedFiles(): Iterator
|
||||
{
|
||||
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
|
||||
}
|
||||
|
||||
protected function provideConfig(): string
|
||||
{
|
||||
return __DIR__ . '/config.yml';
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
final class SomeController
|
||||
{
|
||||
/**
|
||||
* @var ProductRepository
|
||||
*/
|
||||
private $productRepository;
|
||||
public function __construct(ProductRepository $productRepository)
|
||||
{
|
||||
$this->productRepository = $productRepository;
|
||||
}
|
||||
|
||||
public function default()
|
||||
{
|
||||
$products = $this->productRepository->fetchAll();
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
final class SomeController
|
||||
{
|
||||
public function default(ProductRepository $productRepository)
|
||||
{
|
||||
$products = $productRepository->fetchAll();
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
imports:
|
||||
- { resource: '../../../../../config/level/architecture/action-injection-to-constructor-injection.yml' }
|
Loading…
Reference in New Issue
Block a user