-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DependencyInjection] Extract GetByTypeMethodCallToConstructorInjecti…
…onRector to make migration smoother
- Loading branch information
1 parent
cd07337
commit 2d57041
Showing
8 changed files
with
296 additions
and
27 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...yTypeToConstructorInjectionRector/ControllerGetByTypeToConstructorInjectionRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ControllerGetByTypeToConstructorInjectionRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
.../ControllerGetByTypeToConstructorInjectionRector/Fixture/controller_get_with_type.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Fixture; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
|
||
final class ControllerGetWithType extends Controller | ||
{ | ||
public function configure() | ||
{ | ||
$someType = $this->get(SomeType::class); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\Fixture; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
|
||
final class ControllerGetWithType extends Controller | ||
{ | ||
public function __construct(private SomeType $someType) | ||
{ | ||
|
||
} | ||
public function configure() | ||
{ | ||
$someType = $this->someType; | ||
} | ||
} | ||
|
||
?> | ||
|
11 changes: 11 additions & 0 deletions
11
.../Rector/Class_/ControllerGetByTypeToConstructorInjectionRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule( | ||
\Rector\Symfony\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector::class | ||
); | ||
}; |
44 changes: 44 additions & 0 deletions
44
rules/DependencyInjection/NodeDecorator/CommandConstructorDecorator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Symfony\DependencyInjection\NodeDecorator; | ||
|
||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\NodeTypeResolver\NodeTypeResolver; | ||
use Rector\ValueObject\MethodName; | ||
|
||
final class CommandConstructorDecorator | ||
{ | ||
public function __construct( | ||
private NodeTypeResolver $nodeTypeResolver | ||
) { | ||
} | ||
|
||
public function decorate(Class_ $class): void | ||
{ | ||
// special case for command to keep parent constructor call | ||
if (! $this->nodeTypeResolver->isObjectType( | ||
$class, | ||
new ObjectType('Symfony\Component\Console\Command\Command') | ||
)) { | ||
return; | ||
} | ||
|
||
$constuctClassMethod = $class->getMethod(MethodName::CONSTRUCT); | ||
if (! $constuctClassMethod instanceof ClassMethod) { | ||
return; | ||
} | ||
|
||
// empty stmts? add parent::__construct() to setup command | ||
if ((array) $constuctClassMethod->stmts === []) { | ||
$parentConstructStaticCall = new StaticCall(new Name('parent'), '__construct'); | ||
$constuctClassMethod->stmts[] = new Expression($parentConstructStaticCall); | ||
} | ||
} | ||
} |
167 changes: 167 additions & 0 deletions
167
rules/DependencyInjection/Rector/Class_/ControllerGetByTypeToConstructorInjectionRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Symfony\DependencyInjection\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ClassConstFetch; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\NodeManipulator\ClassDependencyManipulator; | ||
use Rector\PHPStan\ScopeFetcher; | ||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||
use Rector\PostRector\ValueObject\PropertyMetadata; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; | ||
use Rector\Symfony\Enum\SymfonyClass; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Symfony\Tests\DependencyInjection\Rector\Class_\ControllerGetByTypeToConstructorInjectionRector\ControllerGetByTypeToConstructorInjectionRectorTest | ||
*/ | ||
final class ControllerGetByTypeToConstructorInjectionRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer, | ||
private readonly ClassDependencyManipulator $classDependencyManipulator, | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'From `$container->get(SomeType::class)` in controllers to constructor injection (step 1/x)', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
final class SomeCommand extends Controller | ||
{ | ||
public function someMethod() | ||
{ | ||
$someType = $this->get(SomeType::class); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
final class SomeCommand extends Controller | ||
{ | ||
public function __construct(private SomeType $someType) | ||
{ | ||
} | ||
public function someMethod() | ||
{ | ||
$someType = $this->someType; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($this->shouldSkipClass($node)) { | ||
return null; | ||
} | ||
|
||
$class = $node; | ||
$propertyMetadatas = []; | ||
|
||
$this->traverseNodesWithCallable($class, function (Node $node) use (&$propertyMetadatas): ?Node { | ||
if (! $node instanceof MethodCall) { | ||
return null; | ||
} | ||
|
||
if ($node->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->name, 'get')) { | ||
return null; | ||
} | ||
|
||
// check controller / container type? | ||
if (! $this->isObjectType($node->var, new ObjectType(SymfonyClass::CONTROLLER))) { | ||
return null; | ||
} | ||
|
||
$args = $node->getArgs(); | ||
if (count($args) !== 1) { | ||
return null; | ||
} | ||
|
||
$firstArg = $args[0]; | ||
if (! $firstArg->value instanceof ClassConstFetch) { | ||
return null; | ||
} | ||
|
||
// must be class const fetch | ||
if (! $this->isName($firstArg->value->name, 'class')) { | ||
return null; | ||
} | ||
|
||
$className = $this->getName($firstArg->value->class); | ||
if (! is_string($className)) { | ||
return null; | ||
} | ||
|
||
$propertyMetadata = new PropertyMetadata($className, new FullyQualifiedObjectType($className)); | ||
|
||
$propertyMetadatas[] = $propertyMetadata; | ||
return $this->nodeFactory->createPropertyFetch('this', $propertyMetadata->getName()); | ||
}); | ||
|
||
if ($propertyMetadatas === []) { | ||
return null; | ||
} | ||
|
||
foreach ($propertyMetadatas as $propertyMetadata) { | ||
$this->classDependencyManipulator->addConstructorDependency($class, $propertyMetadata); | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
private function shouldSkipClass(Class_ $class): bool | ||
{ | ||
// keep it safe | ||
if (! $class->isFinal()) { | ||
return true; | ||
} | ||
|
||
// handled by another rule | ||
if ($this->testsNodeAnalyzer->isInTestClass($class)) { | ||
return true; | ||
} | ||
|
||
$scope = ScopeFetcher::fetch($class); | ||
$classReflection = $scope->getClassReflection(); | ||
if (! $classReflection instanceof ClassReflection) { | ||
return true; | ||
} | ||
|
||
return $classReflection->isSubclassOf(SymfonyClass::CONTROLLER); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters