-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DependencyInjection] Add TraitGetByTypeToInjectRector (#687)
- Loading branch information
1 parent
617b3af
commit da54cd8
Showing
13 changed files
with
367 additions
and
67 deletions.
There are no files selected for viewing
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
39 changes: 39 additions & 0 deletions
39
...ndencyInjection/Rector/Trait_/TraitGetByTypeToInjectRector/Fixture/trait_get_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,39 @@ | ||
<?php | ||
|
||
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Trait_\TraitGetByTypeToInjectRector\Fixture; | ||
|
||
use Rector\Symfony\Tests\DependencyInjection\Rector\Trait_\TraitGetByTypeToInjectRector\Source\SomeService; | ||
|
||
trait TraitGetType | ||
{ | ||
public function configure() | ||
{ | ||
$someType = $this->get(SomeService::class); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Trait_\TraitGetByTypeToInjectRector\Fixture; | ||
|
||
use Rector\Symfony\Tests\DependencyInjection\Rector\Trait_\TraitGetByTypeToInjectRector\Source\SomeService; | ||
|
||
trait TraitGetType | ||
{ | ||
private \Rector\Symfony\Tests\DependencyInjection\Rector\Trait_\TraitGetByTypeToInjectRector\Source\SomeService $someService; | ||
/** | ||
* @required | ||
*/ | ||
public function autowireTraitGetType(\Rector\Symfony\Tests\DependencyInjection\Rector\Trait_\TraitGetByTypeToInjectRector\Source\SomeService $someService): void | ||
{ | ||
$this->someService = $someService; | ||
} | ||
public function configure() | ||
{ | ||
$someType = $this->someService; | ||
} | ||
} | ||
|
||
?> |
7 changes: 7 additions & 0 deletions
7
...sts/DependencyInjection/Rector/Trait_/TraitGetByTypeToInjectRector/Source/SomeService.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,7 @@ | ||
<?php | ||
|
||
namespace Rector\Symfony\Tests\DependencyInjection\Rector\Trait_\TraitGetByTypeToInjectRector\Source; | ||
|
||
final class SomeService | ||
{ | ||
} |
28 changes: 28 additions & 0 deletions
28
...Injection/Rector/Trait_/TraitGetByTypeToInjectRector/TraitGetByTypeToInjectRectorTest.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\Trait_\TraitGetByTypeToInjectRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class TraitGetByTypeToInjectRectorTest 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'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...DependencyInjection/Rector/Trait_/TraitGetByTypeToInjectRector/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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Symfony\DependencyInjection\Rector\Trait_\TraitGetByTypeToInjectRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(TraitGetByTypeToInjectRector::class); | ||
}; |
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
69 changes: 69 additions & 0 deletions
69
rules/DependencyInjection/NodeFactory/AutowireClassMethodFactory.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,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Symfony\DependencyInjection\NodeFactory; | ||
|
||
use PhpParser\Comment\Doc; | ||
use PhpParser\Modifiers; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\Node\Stmt\Trait_; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Exception\ShouldNotHappenException; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
use Rector\PostRector\ValueObject\PropertyMetadata; | ||
|
||
final class AutowireClassMethodFactory | ||
{ | ||
public function __construct( | ||
private NodeNameResolver $nodeNameResolver | ||
) { | ||
|
||
} | ||
|
||
/** | ||
* @param PropertyMetadata[] $propertyMetadatas | ||
*/ | ||
public function create(Trait_ $trait, array $propertyMetadatas): ClassMethod | ||
{ | ||
$traitName = $this->nodeNameResolver->getShortName($trait); | ||
|
||
$autowireClassMethod = new ClassMethod('autowire' . $traitName); | ||
$autowireClassMethod->flags |= Modifiers::PUBLIC; | ||
$autowireClassMethod->returnType = new Identifier('void'); | ||
$autowireClassMethod->setDocComment(new Doc("/**\n * @required\n */")); | ||
|
||
foreach ($propertyMetadatas as $propertyMetadata) { | ||
$param = $this->createAutowiredParam($propertyMetadata); | ||
$autowireClassMethod->params[] = $param; | ||
|
||
$createPropertyAssign = new Assign( | ||
new PropertyFetch(new Variable('this'), new Identifier($propertyMetadata->getName())), | ||
new Variable($propertyMetadata->getName()) | ||
); | ||
$autowireClassMethod->stmts[] = new Expression($createPropertyAssign); | ||
} | ||
|
||
return $autowireClassMethod; | ||
} | ||
|
||
private function createAutowiredParam(PropertyMetadata $propertyMetadata): Param | ||
{ | ||
$param = new Param(new Variable($propertyMetadata->getName())); | ||
$objectType = $propertyMetadata->getType(); | ||
if (! $objectType instanceof ObjectType) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
$param->type = new FullyQualified($objectType->getClassName()); | ||
|
||
return $param; | ||
} | ||
} |
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
Oops, something went wrong.