Skip to content

Commit 90755cf

Browse files
PHPUnit 10/11 Only set attribute when target attribute class exists on CoversAnnotationWithValueToAttributeRector (#493)
* PHPUnit 10/11 Only set attribute when target attribute class exists on CoversAnnotationWithValueToAttributeRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent b95bf5a commit 90755cf

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,17 @@ public function refactor(Node $node): ?Node
145145
return $node;
146146
}
147147

148-
private function createAttributeGroup(string $annotationValue): AttributeGroup
148+
private function createAttributeGroup(string $annotationValue): ?AttributeGroup
149149
{
150150
if (str_starts_with($annotationValue, '::')) {
151151
$attributeClass = self::COVERS_FUNCTION_ATTRIBUTE;
152152
$attributeValue = [trim($annotationValue, ':()')];
153153
} elseif (str_contains($annotationValue, '::')) {
154154
$attributeClass = self::COVERS_METHOD_ATTRIBUTE;
155+
if (! $this->reflectionProvider->hasClass($attributeClass)) {
156+
return null;
157+
}
158+
155159
$attributeValue = [$this->getClass($annotationValue) . '::class', $this->getMethod($annotationValue)];
156160
} else {
157161
$attributeClass = self::COVERTS_CLASS_ATTRIBUTE;
@@ -161,6 +165,9 @@ private function createAttributeGroup(string $annotationValue): AttributeGroup
161165

162166
if ($classReflection->isTrait()) {
163167
$attributeClass = self::COVERTS_TRAIT_ATTRIBUTE;
168+
if (! $this->reflectionProvider->hasClass($attributeClass)) {
169+
return null;
170+
}
164171
}
165172
}
166173

@@ -206,7 +213,13 @@ private function handleCoversDefaultClass(PhpDocInfo $phpDocInfo): array
206213
continue;
207214
}
208215

209-
$attributeGroups[] = $this->createAttributeGroup($desiredTagValueNode->value->value);
216+
$attributeGroup = $this->createAttributeGroup($desiredTagValueNode->value->value);
217+
// phpunit 10 may not fully support attribute
218+
if (! $attributeGroup instanceof AttributeGroup) {
219+
continue;
220+
}
221+
222+
$attributeGroups[] = $attributeGroup;
210223
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode);
211224
}
212225

@@ -226,10 +239,15 @@ private function handleCovers(PhpDocInfo $phpDocInfo, bool $hasCoversDefault): a
226239
}
227240

228241
$covers = $desiredTagValueNode->value->value;
229-
if (str_starts_with($covers, '\\')) {
230-
$attributeGroups[$covers] = $this->createAttributeGroup($covers);
231-
} elseif (! $hasCoversDefault && str_starts_with($covers, '::')) {
232-
$attributeGroups[$covers] = $this->createAttributeGroup($covers);
242+
if (str_starts_with($covers, '\\') || (! $hasCoversDefault && str_starts_with($covers, '::'))) {
243+
$attributeGroup = $this->createAttributeGroup($covers);
244+
245+
// phpunit 10 may not fully support attribute
246+
if (! $attributeGroup instanceof AttributeGroup) {
247+
continue;
248+
}
249+
250+
$attributeGroups[$covers] = $attributeGroup;
233251
}
234252

235253
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode);
@@ -256,10 +274,15 @@ private function resolveMethodAttributes(ClassMethod $classMethod, bool $hasCove
256274
}
257275

258276
$covers = $desiredTagValueNode->value->value;
259-
if (str_starts_with($covers, '\\')) {
260-
$attributeGroups[$covers] = $this->createAttributeGroup($covers);
261-
} elseif (! $hasCoversDefault && str_starts_with($covers, '::')) {
262-
$attributeGroups[$covers] = $this->createAttributeGroup($covers);
277+
if (str_starts_with($covers, '\\') || (! $hasCoversDefault && str_starts_with($covers, '::'))) {
278+
$attributeGroup = $this->createAttributeGroup($covers);
279+
280+
// phpunit 10 may not fully support attribute
281+
if (! $attributeGroup instanceof AttributeGroup) {
282+
continue;
283+
}
284+
285+
$attributeGroups[$covers] = $attributeGroup;
263286
}
264287
}
265288

0 commit comments

Comments
 (0)