-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathCoversAnnotationWithValueToAttributeRector.php
More file actions
327 lines (273 loc) · 11.1 KB
/
CoversAnnotationWithValueToAttributeRector.php
File metadata and controls
327 lines (273 loc) · 11.1 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\Reflection\ReflectionProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\CoversAnnotationWithValueToAttributeRectorTest
*/
final class CoversAnnotationWithValueToAttributeRector extends AbstractRector implements MinPhpVersionInterface
{
private const string COVERS_FUNCTION_ATTRIBUTE = 'PHPUnit\Framework\Attributes\CoversFunction';
private const string COVERTS_CLASS_ATTRIBUTE = 'PHPUnit\Framework\Attributes\CoversClass';
private const string COVERTS_TRAIT_ATTRIBUTE = 'PHPUnit\Framework\Attributes\CoversTrait';
private const string COVERS_METHOD_ATTRIBUTE = 'PHPUnit\Framework\Attributes\CoversMethod';
public function __construct(
private readonly PhpDocTagRemover $phpDocTagRemover,
private readonly PhpAttributeGroupFactory $phpAttributeGroupFactory,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly ReflectionProvider $reflectionProvider
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change covers annotations with value to attribute', [
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
/**
* @covers SomeClass
*/
final class SomeTest extends TestCase
{
/**
* @covers ::someFunction()
*/
public function test()
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\CoversFunction;
#[CoversClass(SomeClass::class)]
#[CoversFunction('someFunction')]
final class SomeTest extends TestCase
{
public function test()
{
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class, ClassMethod::class];
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ATTRIBUTES;
}
/**
* @param Class_|ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}
if (! $this->reflectionProvider->hasClass(self::COVERS_FUNCTION_ATTRIBUTE)) {
return null;
}
if ($node instanceof Class_) {
$coversAttributeGroups = $this->resolveClassAttributes($node);
if ($coversAttributeGroups === []) {
return null;
}
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
$node->attrGroups = array_merge($node->attrGroups, $coversAttributeGroups);
return $node;
}
$hasChanged = $this->removeMethodCoversAnnotations($node);
if ($hasChanged === false) {
return null;
}
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
return $node;
}
private function createAttributeGroup(string $annotationValue): ?AttributeGroup
{
if (str_starts_with($annotationValue, '::')) {
$attributeClass = self::COVERS_FUNCTION_ATTRIBUTE;
$attributeValue = [trim($annotationValue, ':()')];
} elseif (str_contains($annotationValue, '::')) {
$attributeClass = self::COVERS_METHOD_ATTRIBUTE;
if (! $this->reflectionProvider->hasClass($attributeClass)) {
return null;
}
$attributeValue = [$this->getClass($annotationValue) . '::class', $this->getMethod($annotationValue)];
} else {
$attributeClass = self::COVERTS_CLASS_ATTRIBUTE;
if ($this->reflectionProvider->hasClass($annotationValue)) {
$classReflection = $this->reflectionProvider->getClass($annotationValue);
if ($classReflection->isTrait()) {
$attributeClass = self::COVERTS_TRAIT_ATTRIBUTE;
if (! $this->reflectionProvider->hasClass($attributeClass)) {
return null;
}
}
}
$attributeValue = [trim($annotationValue) . '::class'];
}
return $this->phpAttributeGroupFactory->createFromClassWithItems($attributeClass, $attributeValue);
}
/**
* @return array<string, AttributeGroup>
*/
private function resolveClassAttributes(Class_ $class): array
{
$coversDefaultGroups = [];
$coversGroups = [];
$methodGroups = [];
$hasCoversDefault = false;
$coversDefaultClass = '';
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($class);
if ($phpDocInfo instanceof PhpDocInfo) {
$coversDefaultGroups = $this->handleCoversDefaultClass($phpDocInfo);
// If there is a ::coversDefaultClass, @covers ::function will refer to class methods, otherwise it will refer to global functions.
$hasCoversDefault = $coversDefaultGroups !== [];
$coversDefaultClass = $hasCoversDefault ? $this->getName(
$coversDefaultGroups[0]->attrs[0]->args[0]->value
) : null;
$coversGroups = $this->handleCovers($phpDocInfo, $hasCoversDefault);
}
foreach ($class->getMethods() as $classMethod) {
$methodGroups = [...$methodGroups, ...$this->resolveMethodAttributes($classMethod, $coversDefaultClass)];
}
return [...$coversDefaultGroups, ...$coversGroups, ...$methodGroups];
}
/**
* @return AttributeGroup[]
*/
private function handleCoversDefaultClass(PhpDocInfo $phpDocInfo): array
{
$attributeGroups = [];
$desiredTagValueNodes = $phpDocInfo->getTagsByName('coversDefaultClass');
foreach ($desiredTagValueNodes as $desiredTagValueNode) {
if (! $desiredTagValueNode->value instanceof GenericTagValueNode) {
continue;
}
$attributeGroup = $this->createAttributeGroup($desiredTagValueNode->value->value);
// phpunit 10 may not fully support attribute
if (! $attributeGroup instanceof AttributeGroup) {
continue;
}
$attributeGroups[] = $attributeGroup;
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode);
}
return $attributeGroups;
}
/**
* @return array<string, AttributeGroup>
*/
private function handleCovers(PhpDocInfo $phpDocInfo, bool $hasCoversDefault): array
{
$attributeGroups = [];
$desiredTagValueNodes = $phpDocInfo->getTagsByName('covers');
foreach ($desiredTagValueNodes as $desiredTagValueNode) {
if (! $desiredTagValueNode->value instanceof GenericTagValueNode) {
continue;
}
$covers = $desiredTagValueNode->value->value;
if (str_starts_with($covers, '\\') || (! $hasCoversDefault && str_starts_with($covers, '::'))) {
$attributeGroup = $this->createAttributeGroup($covers);
// phpunit 10 may not fully support attribute
if (! $attributeGroup instanceof AttributeGroup) {
continue;
}
$attributeGroups[$covers] = $attributeGroup;
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode);
} elseif ($hasCoversDefault && str_starts_with($covers, '::')) {
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode);
}
}
return $attributeGroups;
}
/**
* @return array<string, AttributeGroup>
*/
private function resolveMethodAttributes(ClassMethod $classMethod, ?string $coversDefaultClass): array
{
$hasCoversDefault = $coversDefaultClass !== null;
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
if (! $phpDocInfo instanceof PhpDocInfo) {
return [];
}
$attributeGroups = [];
$desiredTagValueNodes = $phpDocInfo->getTagsByName('covers');
foreach ($desiredTagValueNodes as $desiredTagValueNode) {
if (! $desiredTagValueNode->value instanceof GenericTagValueNode) {
continue;
}
$covers = $desiredTagValueNode->value->value;
if (! str_starts_with($covers, '\\') && ! str_starts_with($covers, '::')) {
continue;
}
if ($hasCoversDefault && str_starts_with($covers, '::')) {
$covers = $coversDefaultClass . $covers;
}
$attributeGroup = $this->createAttributeGroup($covers);
if (! $attributeGroup instanceof AttributeGroup) {
continue;
}
$attributeGroups[$covers] = $attributeGroup;
}
return $attributeGroups;
}
private function removeMethodCoversAnnotations(ClassMethod $classMethod): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
if (! $phpDocInfo instanceof PhpDocInfo) {
return false;
}
$hasChanged = false;
$desiredTagValueNodes = $phpDocInfo->getTagsByName('covers');
foreach ($desiredTagValueNodes as $desiredTagValueNode) {
if (! $desiredTagValueNode->value instanceof GenericTagValueNode) {
continue;
}
if (str_contains($desiredTagValueNode->value->value, '::') && ! $this->reflectionProvider->hasClass(
self::COVERS_METHOD_ATTRIBUTE
)) {
continue;
}
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode);
$hasChanged = true;
}
return $hasChanged;
}
private function getClass(string $classWithMethod): string
{
return Strings::replace($classWithMethod, '/::.*$/');
}
private function getMethod(string $classWithMethod): string
{
return Strings::replace($classWithMethod, '/^.*::/');
}
}