From 6303e1bfc9e5f89a20080b2429cc1d853f9c1f65 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 31 Oct 2026 14:04:56 +0700 Subject: [PATCH] [perf] Reuse collected use statements when resolving PHPDoc short names --- .../ClassNameImport/ShortNameResolver.php | 18 +++++++++++++----- .../NodeAnalyzer/UseImportNameMatcher.php | 13 ------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/rules/CodingStyle/ClassNameImport/ShortNameResolver.php b/rules/CodingStyle/ClassNameImport/ShortNameResolver.php index a783a3c451a..358b35be961 100644 --- a/rules/CodingStyle/ClassNameImport/ShortNameResolver.php +++ b/rules/CodingStyle/ClassNameImport/ShortNameResolver.php @@ -9,6 +9,7 @@ use PhpParser\Node\Name; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\ClassLike; +use PhpParser\Node\Stmt\Use_; use PHPStan\PhpDocParser\Ast\NodeTraverser; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; @@ -120,16 +121,23 @@ private function resolveForStmts(array $stmts): array { $shortNamesToFullyQualifiedNames = []; $docBlockShortNames = []; + $uses = []; // single traversal collects both PHP names and docblock short names, so the file stmts are walked once $this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use ( &$shortNamesToFullyQualifiedNames, - &$docBlockShortNames + &$docBlockShortNames, + &$uses ): null { foreach ($this->resolveDocBlockShortNames($node) as $docBlockShortName) { $docBlockShortNames[] = $docBlockShortName; } + if ($node instanceof Use_) { + $uses[] = $node; + return null; + } + // class name is used! if ($node instanceof ClassLike && $node->name instanceof Identifier) { $fullyQualifiedName = $this->nodeNameResolver->getName($node); @@ -160,7 +168,7 @@ private function resolveForStmts(array $stmts): array return null; }); - $docBlockShortNamesToFullyQualifiedNames = $this->fqnizeShortNames($docBlockShortNames, $stmts); + $docBlockShortNamesToFullyQualifiedNames = $this->fqnizeShortNames($docBlockShortNames, $uses); /** @var array $result */ $result = [...$shortNamesToFullyQualifiedNames, ...$docBlockShortNamesToFullyQualifiedNames]; return $result; @@ -202,15 +210,15 @@ static function ($node) use (&$shortNames): null { /** * @param string[] $shortNames - * @param Stmt[] $stmts + * @param Use_[] $uses * @return array */ - private function fqnizeShortNames(array $shortNames, array $stmts): array + private function fqnizeShortNames(array $shortNames, array $uses): array { $shortNamesToFullyQualifiedNames = []; foreach ($shortNames as $shortName) { - $stmtsMatchedName = $this->useImportNameMatcher->matchNameWithStmts($shortName, $stmts); + $stmtsMatchedName = $this->useImportNameMatcher->matchNameWithUses($shortName, $uses); if ($stmtsMatchedName == null) { continue; diff --git a/rules/CodingStyle/NodeAnalyzer/UseImportNameMatcher.php b/rules/CodingStyle/NodeAnalyzer/UseImportNameMatcher.php index 2406252ca2c..742907f103b 100644 --- a/rules/CodingStyle/NodeAnalyzer/UseImportNameMatcher.php +++ b/rules/CodingStyle/NodeAnalyzer/UseImportNameMatcher.php @@ -6,14 +6,12 @@ use Nette\Utils\Strings; use PhpParser\Node\Identifier; -use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\GroupUse; use PhpParser\Node\Stmt\Use_; use PhpParser\Node\UseItem; use Rector\Exception\ShouldNotHappenException; use Rector\Naming\Naming\UseImportsResolver; use Rector\NodeTypeResolver\Node\AttributeKey; -use Rector\PhpParser\Node\BetterNodeFinder; use Rector\Util\StringUtils; final readonly class UseImportNameMatcher @@ -25,21 +23,10 @@ private const string SHORT_NAME_REGEX = '#^%s(\\\\[\w]+)?$#i'; public function __construct( - private BetterNodeFinder $betterNodeFinder, private UseImportsResolver $useImportsResolver ) { } - /** - * @param Stmt[] $stmts - */ - public function matchNameWithStmts(string $tag, array $stmts): ?string - { - /** @var Use_[] $uses */ - $uses = $this->betterNodeFinder->findInstanceOf($stmts, Use_::class); - return $this->matchNameWithUses($tag, $uses); - } - /** * @param array $uses * @return non-empty-string|null