Skip to content

Commit

Permalink
[PostRector] Skip remove unused use on used as ConstFetchNode, eg: Ty…
Browse files Browse the repository at this point in the history
…peKind::* (#3560)

* [PostRector] Skip remove unused use on used as asterisk

* Fixed 🎉

* final touch: eol

* final touch: rename fixture

* rename method
  • Loading branch information
samsonasik committed Apr 4, 2023
1 parent 622e93f commit 1da2282
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\BetterPhpDocParser\PhpDocInfo;

use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
Expand All @@ -16,6 +17,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\Type\MixedType;
Expand All @@ -26,6 +28,7 @@
use Rector\BetterPhpDocParser\PhpDocNodeFinder\PhpDocNodeByTypeFinder;
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\ChangedPhpDocNodeVisitor;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\Type\ShortenedIdentifierTypeNode;
use Rector\ChangesReporting\Collector\RectorChangeCollector;
use Rector\Core\Configuration\CurrentNodeProvider;
Expand Down Expand Up @@ -448,6 +451,33 @@ public function getAnnotationClassNames(): array
return $resolvedClasses;
}

/**
* @return string[]
*/
public function getConstFetchNodeClassNames(): array
{
$phpDocNodeTraverser = new PhpDocNodeTraverser();

$classNames = [];

$phpDocNodeTraverser->traverseWithCallable($this->phpDocNode, '', static function (Node $node) use (
&$classNames,
): ?ConstTypeNode {
if (! $node instanceof ConstTypeNode) {
return null;
}

if (! $node->constExpr instanceof ConstFetchNode) {
return null;
}

$classNames[] = $node->constExpr->getAttribute(PhpDocAttributeKey::RESOLVED_CLASS);
return $node;
});

return $classNames;
}

private function resolveNameForPhpDocTagValueNode(PhpDocTagValueNode $phpDocTagValueNode): ?string
{
foreach (self::TAGS_TYPES_TO_NAMES as $tagValueNodeType => $name) {
Expand Down
3 changes: 3 additions & 0 deletions packages/PostRector/Rector/UnusedImportRemovingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ private function findNamesInDocBlocks(Namespace_|FileWithoutNamespace $namespace

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$names = array_merge($names, $phpDocInfo->getAnnotationClassNames());

$constFetchNodeNames = $phpDocInfo->getConstFetchNodeClassNames();
$names = array_merge($names, $constFetchNodeNames);
});

return $names;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Core\Tests\Issues\NamespacedUse\Fixture;

use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;

final class SkipUsedAsConstFetchNode
{
/**
* @param TypeKind::* $typeKind
*/
public function run(string $typeKind)
{
}
}

0 comments on commit 1da2282

Please sign in to comment.