-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
ChildDoctrineRepositoryClassTypeRector.php
222 lines (184 loc) · 6.5 KB
/
ChildDoctrineRepositoryClassTypeRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?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 based on return Doctrine method names',
[
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, 'getOneOrNullResult')) {
$classMethod->returnType = $this->createNullableType($entityClassName);
} elseif ($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;
}
// skip if value is used in generics
if (in_array($entityGenericType->name, $classPhpDocInfo->getTemplateNames(), true)) {
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);
}
}