Skip to content

Commit 74b5d79

Browse files
committed
[BetterPhpDocParser] Keep import referenced by @see/@uses tag with a trailing description
When `removeUnusedImports()` is enabled, UnusedImportRemovingPostRector collects the class names used inside doc blocks through PhpDocInfo::getGenericTagClassNames(). For generic tags such as @see, @uses and @link it only registered the full tag value. A tag like `@see Foo some description` therefore produced the single candidate "Foo some description", which never matches the imported short name "Foo". As a result the `use` import for Foo was considered unused and removed, leaving an unresolved reference (or, for a class in another namespace, one that silently resolves to the wrong class). Per the phpDocumentor syntax `@see [FQSEN] [<description>]`, the class reference is the leading token of the value. Register that leading token in addition to the full value, so the import is correctly kept when a description follows the class name.
1 parent 9e4dc59 commit 74b5d79

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,13 @@ public function getGenericTagClassNames(): array
461461

462462
// add default original value
463463
$resolvedClasses[] = $genericTagValueNode->value;
464+
465+
// add the leading reference, so "@see Foo some description" keeps the import for Foo
466+
$leadingReference = strtok($genericTagValueNode->value, " \t\n\r");
467+
if ($leadingReference !== false && $leadingReference !== $genericTagValueNode->value) {
468+
$resolvedClasses[] = $leadingReference;
469+
}
470+
464471
if (! str_contains($genericTagValueNode->value, '::')) {
465472
continue;
466473
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Rector\Tests\Issues\NamespacedUse\Fixture;
4+
5+
use Rector\Renaming\Rector\Name\RenameClassRector;
6+
7+
/**
8+
* @see RenameClassRector some description after the class reference
9+
*/
10+
final class SkipUsedSeeWithDescription
11+
{
12+
}

0 commit comments

Comments
 (0)