Skip to content

Commit

Permalink
[Attribute] Add CoversTrait support on CoversAnnotationWithValueToAtt…
Browse files Browse the repository at this point in the history
…ributeRector
  • Loading branch information
samsonasik committed Feb 21, 2025
1 parent c0b4dfb commit 3927031
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Fixture;

use PHPUnit\Framework\TestCase;

/**
* @covers \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingTrait
*/
final class CoversTrait extends TestCase
{
public function test()
{
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Fixture;

use PHPUnit\Framework\TestCase;

#[\PHPUnit\Framework\Attributes\CoversTrait(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingTrait::class)]
final class CoversTrait extends TestCase
{
public function test()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source;

trait ExistingTrait
{
public function __construct()
{
}

public function foo(): void
{
}

public function bar(): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ final class CoversAnnotationWithValueToAttributeRector extends AbstractRector im
*/
private const COVERTS_CLASS_ATTRIBUTE = 'PHPUnit\Framework\Attributes\CoversClass';

/**
* @var string
*/
private const COVERTS_TRAIT_ATTRIBUTE = 'PHPUnit\Framework\Attributes\CoversTrait';

/**
* @var string
*/
Expand Down Expand Up @@ -150,6 +155,15 @@ private function createAttributeGroup(string $annotationValue): AttributeGroup
$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;
}
}

$attributeValue = [trim($annotationValue) . '::class'];
}

Expand Down

0 comments on commit 3927031

Please sign in to comment.