Skip to content

Commit e7e84da

Browse files
authored
[AnnotationsToAttributes] Fix @Covers qualified::method conversion (#451)
1 parent 0ba734c commit e7e84da

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

rules-tests/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector/Fixture/covers_class.php.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase;
66

77
/**
88
* @covers \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingClass
9+
* @covers \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\AnotherExistingClass::someMethod
910
*/
1011
final class CoversClass extends TestCase
1112
{
@@ -23,6 +24,7 @@ namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnot
2324
use PHPUnit\Framework\TestCase;
2425

2526
#[\PHPUnit\Framework\Attributes\CoversClass(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingClass::class)]
27+
#[\PHPUnit\Framework\Attributes\CoversMethod(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\AnotherExistingClass::class, 'someMethod')]
2628
final class CoversClass extends TestCase
2729
{
2830
public function test()

rules-tests/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector/Fixture/covers_method.php.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ final class CoversMethod extends TestCase
1212
public function test()
1313
{
1414
}
15+
16+
/**
17+
* @covers \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\AnotherExistingClass::someMethod
18+
*/
19+
public function test_foo()
20+
{
21+
}
1522
}
1623

1724
?>
@@ -23,11 +30,16 @@ namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnot
2330
use PHPUnit\Framework\TestCase;
2431

2532
#[\PHPUnit\Framework\Attributes\CoversClass(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\ExistingClass::class)]
33+
#[\PHPUnit\Framework\Attributes\CoversMethod(\Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector\Source\AnotherExistingClass::class, 'someMethod')]
2634
final class CoversMethod extends TestCase
2735
{
2836
public function test()
2937
{
3038
}
39+
40+
public function test_foo()
41+
{
42+
}
3143
}
3244

3345
?>

rules/AnnotationsToAttributes/Rector/Class_/CoversAnnotationWithValueToAttributeRector.php

Lines changed: 16 additions & 4 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 COVERS_METHOD_ATTRIBUTE = 'PHPUnit\Framework\Attributes\CoversMethod';
45+
4146
public function __construct(
4247
private readonly PhpDocTagRemover $phpDocTagRemover,
4348
private readonly PhpAttributeGroupFactory $phpAttributeGroupFactory,
@@ -139,13 +144,16 @@ private function createAttributeGroup(string $annotationValue): AttributeGroup
139144
{
140145
if (str_starts_with($annotationValue, '::')) {
141146
$attributeClass = self::COVERS_FUNCTION_ATTRIBUTE;
142-
$attributeValue = trim($annotationValue, ':()');
147+
$attributeValue = [trim($annotationValue, ':()')];
148+
} elseif (str_contains($annotationValue, '::')) {
149+
$attributeClass = self::COVERS_METHOD_ATTRIBUTE;
150+
$attributeValue = [$this->getClass($annotationValue) . '::class', $this->getMethod($annotationValue)];
143151
} else {
144152
$attributeClass = self::COVERTS_CLASS_ATTRIBUTE;
145-
$attributeValue = trim($annotationValue) . '::class';
153+
$attributeValue = [trim($annotationValue) . '::class'];
146154
}
147155

148-
return $this->phpAttributeGroupFactory->createFromClassWithItems($attributeClass, [$attributeValue]);
156+
return $this->phpAttributeGroupFactory->createFromClassWithItems($attributeClass, $attributeValue);
149157
}
150158

151159
/**
@@ -235,7 +243,6 @@ private function resolveMethodAttributes(ClassMethod $classMethod, bool $hasCove
235243

236244
$covers = $desiredTagValueNode->value->value;
237245
if (str_starts_with($covers, '\\')) {
238-
$covers = $this->getClass($covers);
239246
$attributeGroups[$covers] = $this->createAttributeGroup($covers);
240247
} elseif (! $hasCoversDefault && str_starts_with($covers, '::')) {
241248
$attributeGroups[$covers] = $this->createAttributeGroup($covers);
@@ -271,4 +278,9 @@ private function getClass(string $classWithMethod): string
271278
{
272279
return Strings::replace($classWithMethod, '/::.*$/');
273280
}
281+
282+
private function getMethod(string $classWithMethod): string
283+
{
284+
return Strings::replace($classWithMethod, '/^.*::/');
285+
}
274286
}

rules/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ private function isTestFilePath(FuncCall $funcCall): bool
149149
if (str_ends_with($className, 'Test')) {
150150
return true;
151151
}
152+
152153
return str_ends_with($className, 'TestCase');
153154
}
154155

tests/Issues/DoubleAnnotation/Fixture/some_test.php.inc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace Rector\PHPUnit\Tests\Issues\DoubleAnnotation\Fixture;
2929
use PHPUnit\Framework\TestCase;
3030
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\UseSpecificWillMethodRector\Fixture\SomeClass;
3131

32-
#[\PHPUnit\Framework\Attributes\CoversClass(\SomeClass::class)]
32+
#[\PHPUnit\Framework\Attributes\CoversMethod(\SomeClass::class, 'method')]
3333
final class SomeTest extends TestCase
3434
{
3535
#[\PHPUnit\Framework\Attributes\DataProvider('generatorInput')]
@@ -42,4 +42,3 @@ final class SomeTest extends TestCase
4242
{
4343
}
4444
}
45-

0 commit comments

Comments
 (0)