Skip to content

Commit ead02de

Browse files
authored
[Attribute] Add CoversTrait support on CoversAnnotationWithValueToAttributeRector (#460)
1 parent c0b4dfb commit ead02de

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
/**
8+
* @covers \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingTrait
9+
*/
10+
final class CoversTrait extends TestCase
11+
{
12+
public function test()
13+
{
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Fixture;
22+
23+
use PHPUnit\Framework\TestCase;
24+
25+
#[\PHPUnit\Framework\Attributes\CoversTrait(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingTrait::class)]
26+
final class CoversTrait extends TestCase
27+
{
28+
public function test()
29+
{
30+
}
31+
}
32+
33+
?>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source;
5+
6+
trait ExistingTrait
7+
{
8+
public function __construct()
9+
{
10+
}
11+
12+
public function foo(): void
13+
{
14+
}
15+
16+
public function bar(): void
17+
{
18+
}
19+
}

rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ final class CoversAnnotationWithValueToAttributeRector extends AbstractRector im
3838
*/
3939
private const COVERTS_CLASS_ATTRIBUTE = 'PHPUnit\Framework\Attributes\CoversClass';
4040

41+
/**
42+
* @var string
43+
*/
44+
private const COVERTS_TRAIT_ATTRIBUTE = 'PHPUnit\Framework\Attributes\CoversTrait';
45+
4146
/**
4247
* @var string
4348
*/
@@ -150,6 +155,15 @@ private function createAttributeGroup(string $annotationValue): AttributeGroup
150155
$attributeValue = [$this->getClass($annotationValue) . '::class', $this->getMethod($annotationValue)];
151156
} else {
152157
$attributeClass = self::COVERTS_CLASS_ATTRIBUTE;
158+
159+
if ($this->reflectionProvider->hasClass($annotationValue)) {
160+
$classReflection = $this->reflectionProvider->getClass($annotationValue);
161+
162+
if ($classReflection->isTrait()) {
163+
$attributeClass = self::COVERTS_TRAIT_ATTRIBUTE;
164+
}
165+
}
166+
153167
$attributeValue = [trim($annotationValue) . '::class'];
154168
}
155169

0 commit comments

Comments
 (0)