Skip to content

Commit a2ec380

Browse files
authored
[CodeQuality] Skip existing imported or short @ see annotation in AddSeeTestAnnotationRector (#779)
* [CodeQuality] Skip existing imported or short @ see annotation in AddSeeTestAnnotationRector * more
1 parent c4e571e commit a2ec380

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddSeeTestAnnotationRector\Fixture;
4+
5+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddSeeTestAnnotationRector\Source\SkipExistingImportedSeeTest;
6+
7+
/**
8+
* @see SkipExistingImportedSeeTest
9+
*/
10+
class SkipExistingImportedSee
11+
{
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* 2 classes here on purpose to test that the rector will not add a @see annotation to the first class, because it already has one.
5+
*/
6+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddSeeTestAnnotationRector\Fixture;
7+
8+
/**
9+
* @see SkipExistingShortSeeTest
10+
*/
11+
class SkipExistingShortSee
12+
{
13+
}
14+
15+
class SkipExistingShortSeeTest extends \PHPUnit\Framework\TestCase
16+
{
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AddSeeTestAnnotationRector\Source;
6+
7+
final class SkipExistingImportedSeeTest
8+
{
9+
}

‎rules/CodeQuality/Rector/Class_/AddSeeTestAnnotationRector.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Reflection\ReflectionProvider;
1313
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
1414
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
15+
use Rector\BetterPhpDocParser\PhpDocParser\ClassAnnotationMatcher;
1516
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
1617
use Rector\PHPUnit\Naming\TestClassNameResolver;
1718
use Rector\Rector\AbstractRector;
@@ -30,6 +31,7 @@ public function __construct(
3031
private readonly TestClassNameResolver $testClassNameResolver,
3132
private readonly DocBlockUpdater $docBlockUpdater,
3233
private readonly PhpDocInfoFactory $phpDocInfoFactory,
34+
private readonly ClassAnnotationMatcher $classAnnotationMatcher,
3335
) {
3436
}
3537

@@ -141,6 +143,12 @@ private function shouldSkipClass(Class_ $class): bool
141143
if ($this->reflectionProvider->hasClass($seeTagClass)) {
142144
return true;
143145
}
146+
147+
// the @see value can be a short name, imported or in the current namespace
148+
$resolvedSeeTagClass = $this->classAnnotationMatcher->resolveTagFullyQualifiedName($seeTagClass, $class);
149+
if ($this->reflectionProvider->hasClass($resolvedSeeTagClass)) {
150+
return true;
151+
}
144152
}
145153

146154
return false;

0 commit comments

Comments
 (0)