Skip to content

Commit 5dfb29c

Browse files
authored
[BetterPhpDocParser] Keep import referenced by @see/@uses tag with a trailing description (#8039)
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 21785b4 commit 5dfb29c

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
use Rector\Exception\ShouldNotHappenException;
3636
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
3737
use Rector\StaticTypeMapper\StaticTypeMapper;
38+
use Rector\Validation\RectorAssert;
39+
use Webmozart\Assert\InvalidArgumentException;
3840

3941
/**
4042
* @see \Rector\Tests\BetterPhpDocParser\PhpDocInfo\PhpDocInfo\PhpDocInfoTest
@@ -461,7 +463,19 @@ public function getGenericTagClassNames(): array
461463

462464
// add default original value
463465
$resolvedClasses[] = $genericTagValueNode->value;
466+
464467
if (! str_contains($genericTagValueNode->value, '::')) {
468+
// keep the import for the leading class reference in "@see Foo some description"
469+
$leadingReference = strtok($genericTagValueNode->value, " \t\n\r");
470+
if ($leadingReference !== false && $leadingReference !== $genericTagValueNode->value) {
471+
try {
472+
RectorAssert::className($leadingReference);
473+
$resolvedClasses[] = $leadingReference;
474+
} catch (InvalidArgumentException) {
475+
continue;
476+
}
477+
}
478+
465479
continue;
466480
}
467481

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\Tests\Issues\NamespacedUse\Fixture;
4+
5+
use Rector\Renaming\Rector\Name\RenameClassRector;
6+
7+
/**
8+
* @see some description that does not start with a class reference
9+
*/
10+
final class SeeWithNonClassDescription
11+
{
12+
}
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\Issues\NamespacedUse\Fixture;
17+
18+
/**
19+
* @see some description that does not start with a class reference
20+
*/
21+
final class SeeWithNonClassDescription
22+
{
23+
}
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)