diff --git a/rules-tests/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector/Fixture/covers_class_default.php.inc b/rules-tests/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector/Fixture/covers_class_default.php.inc index 4b666909..58715b9b 100644 --- a/rules-tests/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector/Fixture/covers_class_default.php.inc +++ b/rules-tests/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector/Fixture/covers_class_default.php.inc @@ -36,7 +36,9 @@ namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnot use PHPUnit\Framework\TestCase; #[\PHPUnit\Framework\Attributes\CoversClass(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingClass::class)] +#[\PHPUnit\Framework\Attributes\CoversMethod(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingClass::class, 'foo')] #[\PHPUnit\Framework\Attributes\CoversClass(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\HelperClass::class)] +#[\PHPUnit\Framework\Attributes\CoversMethod(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingClass::class, 'bar')] final class CoversClassDefault extends TestCase { public function testFoo() diff --git a/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php b/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php index 25d79096..1be0b56e 100644 --- a/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php +++ b/rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php @@ -186,16 +186,20 @@ private function resolveClassAttributes(Class_ $class): array $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, $hasCoversDefault)]; + $methodGroups = [...$methodGroups, ...$this->resolveMethodAttributes($classMethod, $coversDefaultClass)]; } return [...$coversDefaultGroups, ...$coversGroups, ...$methodGroups]; @@ -260,8 +264,9 @@ private function handleCovers(PhpDocInfo $phpDocInfo, bool $hasCoversDefault): a /** * @return array */ - private function resolveMethodAttributes(ClassMethod $classMethod, bool $hasCoversDefault): array + private function resolveMethodAttributes(ClassMethod $classMethod, ?string $coversDefaultClass): array { + $hasCoversDefault = $coversDefaultClass !== null; $phpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod); if (! $phpDocInfo instanceof PhpDocInfo) { return []; @@ -275,16 +280,20 @@ private function resolveMethodAttributes(ClassMethod $classMethod, bool $hasCove } $covers = $desiredTagValueNode->value->value; - if (str_starts_with($covers, '\\') || (! $hasCoversDefault && str_starts_with($covers, '::'))) { - $attributeGroup = $this->createAttributeGroup($covers); + if (! str_starts_with($covers, '\\') && ! str_starts_with($covers, '::')) { + continue; + } - // phpunit 10 may not fully support attribute - if (! $attributeGroup instanceof AttributeGroup) { - continue; - } + if ($hasCoversDefault && str_starts_with($covers, '::')) { + $covers = $coversDefaultClass . $covers; + } - $attributeGroups[$covers] = $attributeGroup; + $attributeGroup = $this->createAttributeGroup($covers); + if (! $attributeGroup instanceof AttributeGroup) { + continue; } + + $attributeGroups[$covers] = $attributeGroup; } return $attributeGroups;