Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -260,8 +264,9 @@ private function handleCovers(PhpDocInfo $phpDocInfo, bool $hasCoversDefault): a
/**
* @return array<string, AttributeGroup>
*/
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 [];
Expand All @@ -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;
Expand Down