-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TypeDeclaration] Add ChildDoctrineRepositoryClassTypeRector (#5695)
* [TypeDeclaration] Add ChildDoctrineRepositoryClassTypeRector * register rule * add findBy() support
- Loading branch information
1 parent
84639e6
commit 643814d
Showing
7 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...ss_/ChildDoctrineRepositoryClassTypeRector/ChildDoctrineRepositoryClassTypeRectorTest.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\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ChildDoctrineRepositoryClassTypeRectorTest 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'; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...ration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector/Fixture/array_find_by.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,46 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\Fixture; | ||
|
||
use Doctrine\ORM\EntityRepository; | ||
use Rector\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\Source\SomeObject; | ||
|
||
/** | ||
* @extends EntityRepository<SomeObject> | ||
*/ | ||
final class ArrayFindBy extends EntityRepository | ||
{ | ||
public function findSome($userId) | ||
{ | ||
return $this->findBy([ | ||
'userId' => $userId, | ||
]); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\Fixture; | ||
|
||
use Doctrine\ORM\EntityRepository; | ||
use Rector\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\Source\SomeObject; | ||
|
||
/** | ||
* @extends EntityRepository<SomeObject> | ||
*/ | ||
final class ArrayFindBy extends EntityRepository | ||
{ | ||
/** | ||
* @return SomeObject[] | ||
*/ | ||
public function findSome($userId): array | ||
{ | ||
return $this->findBy([ | ||
'userId' => $userId, | ||
]); | ||
} | ||
} | ||
|
||
?> |
43 changes: 43 additions & 0 deletions
43
...claration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector/Fixture/some_class.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,43 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\Fixture; | ||
|
||
use Doctrine\ORM\EntityRepository; | ||
use Rector\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\Source\SomeObject; | ||
|
||
/** | ||
* @extends EntityRepository<SomeObject> | ||
*/ | ||
final class ProductRepository extends EntityRepository | ||
{ | ||
public function findSome($userId) | ||
{ | ||
return $this->findOneBy([ | ||
'userId' => $userId, | ||
]); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\Fixture; | ||
|
||
use Doctrine\ORM\EntityRepository; | ||
use Rector\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\Source\SomeObject; | ||
|
||
/** | ||
* @extends EntityRepository<SomeObject> | ||
*/ | ||
final class ProductRepository extends EntityRepository | ||
{ | ||
public function findSome($userId): ?SomeObject | ||
{ | ||
return $this->findOneBy([ | ||
'userId' => $userId, | ||
]); | ||
} | ||
} | ||
|
||
?> |
7 changes: 7 additions & 0 deletions
7
...ypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector/Source/SomeObject.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\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\Source; | ||
|
||
class SomeObject | ||
{ | ||
} |
10 changes: 10 additions & 0 deletions
10
...claration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector/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\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ChildDoctrineRepositoryClassTypeRector::class); | ||
}; |
212 changes: 212 additions & 0 deletions
212
rules/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector.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,212 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypeDeclaration\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\NullableType; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\NodeFinder; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; | ||
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; | ||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; | ||
use Rector\Comments\NodeDocBlock\DocBlockUpdater; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector\ChildDoctrineRepositoryClassTypeRectorTest | ||
*/ | ||
final class ChildDoctrineRepositoryClassTypeRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly PhpDocInfoFactory $phpDocInfoFactory, | ||
private readonly NodeFinder $nodeFinder, | ||
private readonly DocBlockUpdater $docBlockUpdater | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Add return type to classes that extend Doctrine\ORM\EntityRepository', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use Doctrine\ORM\EntityRepository; | ||
/** | ||
* @extends EntityRepository<SomeType> | ||
*/ | ||
final class SomeRepository extends EntityRepository | ||
{ | ||
public function getActiveItem() | ||
{ | ||
return $this->findOneBy([ | ||
'something' | ||
]); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use Doctrine\ORM\EntityRepository; | ||
/** | ||
* @extends EntityRepository<SomeType> | ||
*/ | ||
final class SomeRepository extends EntityRepository | ||
{ | ||
public function getActiveItem(): ?SomeType | ||
{ | ||
return $this->findOneBy([ | ||
'something' | ||
]); | ||
} | ||
} | ||
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->isObjectType($node, new ObjectType('Doctrine\ORM\EntityRepository'))) { | ||
return null; | ||
} | ||
|
||
$entityClassName = $this->resolveEntityClassnameFromPhpDoc($node); | ||
if ($entityClassName === null) { | ||
return null; | ||
} | ||
|
||
$hasChanged = false; | ||
|
||
foreach ($node->getMethods() as $classMethod) { | ||
if ($this->shouldSkipClassMethod($classMethod)) { | ||
continue; | ||
} | ||
|
||
if ($this->containsMethodCallNamed($classMethod, 'findOneBy')) { | ||
$classMethod->returnType = $this->createNullableType($entityClassName); | ||
} | ||
|
||
if ($this->containsMethodCallNamed($classMethod, 'findBy')) { | ||
$classMethod->returnType = new Identifier('array'); | ||
// add docblock with type | ||
|
||
$classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod); | ||
|
||
$arrayTypeNode = new ArrayTypeNode(new IdentifierTypeNode($entityClassName)); | ||
$classMethodPhpDocInfo->addTagValueNode(new ReturnTagValueNode($arrayTypeNode, '')); | ||
|
||
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod); | ||
} | ||
|
||
$hasChanged = true; | ||
// try to figure out the return type | ||
} | ||
|
||
if ($hasChanged) { | ||
return $node; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function resolveEntityClassnameFromPhpDoc(Class_ $class): ?string | ||
{ | ||
$classPhpDocInfo = $this->phpDocInfoFactory->createFromNode($class); | ||
|
||
// we need a way to resolve entity type... 1st idea is from @extends docblock | ||
if (! $classPhpDocInfo instanceof PhpDocInfo) { | ||
return null; | ||
} | ||
|
||
$extendsTagValuePhpDocNodes = $classPhpDocInfo->getTagsByName('extends'); | ||
|
||
if ($extendsTagValuePhpDocNodes === []) { | ||
return null; | ||
} | ||
|
||
$extendsTagValueNode = $extendsTagValuePhpDocNodes[0]->value; | ||
if (! $extendsTagValueNode instanceof ExtendsTagValueNode) { | ||
return null; | ||
} | ||
|
||
// we look for generic type class | ||
if (! $extendsTagValueNode->type instanceof GenericTypeNode) { | ||
return null; | ||
} | ||
|
||
$genericTypeNode = $extendsTagValueNode->type; | ||
if ($genericTypeNode->type->name !== 'EntityRepository') { | ||
return null; | ||
} | ||
|
||
$entityGenericType = $genericTypeNode->genericTypes[0]; | ||
|
||
if (! $entityGenericType instanceof IdentifierTypeNode) { | ||
return null; | ||
} | ||
|
||
return $entityGenericType->name; | ||
} | ||
|
||
private function containsMethodCallNamed(ClassMethod $classMethod, string $desiredMethodName): bool | ||
{ | ||
return (bool) $this->nodeFinder->findFirst((array) $classMethod->stmts, static function (Node $node) use ( | ||
$desiredMethodName | ||
): bool { | ||
if (! $node instanceof MethodCall) { | ||
return false; | ||
} | ||
|
||
if (! $node->name instanceof Identifier) { | ||
return false; | ||
} | ||
|
||
$currentMethodCallName = $node->name->toString(); | ||
return $currentMethodCallName === $desiredMethodName; | ||
}); | ||
} | ||
|
||
private function shouldSkipClassMethod(ClassMethod $classMethod): bool | ||
{ | ||
if (! $classMethod->isPublic()) { | ||
return true; | ||
} | ||
|
||
if ($classMethod->isStatic()) { | ||
return true; | ||
} | ||
|
||
return $classMethod->returnType instanceof Node; | ||
} | ||
|
||
private function createNullableType(string $entityClassName): NullableType | ||
{ | ||
$name = new Name($entityClassName); | ||
return new NullableType($name); | ||
} | ||
} |
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