Skip to content

Commit c1870f6

Browse files
authored
[perf] Reuse collected use statements when resolving PHPDoc short names (#8901)
1 parent d11c325 commit c1870f6

2 files changed

Lines changed: 13 additions & 18 deletions

File tree

‎rules/CodingStyle/ClassNameImport/ShortNameResolver.php‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\Node\Name;
1010
use PhpParser\Node\Stmt;
1111
use PhpParser\Node\Stmt\ClassLike;
12+
use PhpParser\Node\Stmt\Use_;
1213
use PHPStan\PhpDocParser\Ast\NodeTraverser;
1314
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
1415
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
@@ -120,16 +121,23 @@ private function resolveForStmts(array $stmts): array
120121
{
121122
$shortNamesToFullyQualifiedNames = [];
122123
$docBlockShortNames = [];
124+
$uses = [];
123125

124126
// single traversal collects both PHP names and docblock short names, so the file stmts are walked once
125127
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use (
126128
&$shortNamesToFullyQualifiedNames,
127-
&$docBlockShortNames
129+
&$docBlockShortNames,
130+
&$uses
128131
): null {
129132
foreach ($this->resolveDocBlockShortNames($node) as $docBlockShortName) {
130133
$docBlockShortNames[] = $docBlockShortName;
131134
}
132135

136+
if ($node instanceof Use_) {
137+
$uses[] = $node;
138+
return null;
139+
}
140+
133141
// class name is used!
134142
if ($node instanceof ClassLike && $node->name instanceof Identifier) {
135143
$fullyQualifiedName = $this->nodeNameResolver->getName($node);
@@ -160,7 +168,7 @@ private function resolveForStmts(array $stmts): array
160168
return null;
161169
});
162170

163-
$docBlockShortNamesToFullyQualifiedNames = $this->fqnizeShortNames($docBlockShortNames, $stmts);
171+
$docBlockShortNamesToFullyQualifiedNames = $this->fqnizeShortNames($docBlockShortNames, $uses);
164172
/** @var array<string, string> $result */
165173
$result = [...$shortNamesToFullyQualifiedNames, ...$docBlockShortNamesToFullyQualifiedNames];
166174
return $result;
@@ -202,15 +210,15 @@ static function ($node) use (&$shortNames): null {
202210

203211
/**
204212
* @param string[] $shortNames
205-
* @param Stmt[] $stmts
213+
* @param Use_[] $uses
206214
* @return array<string, string>
207215
*/
208-
private function fqnizeShortNames(array $shortNames, array $stmts): array
216+
private function fqnizeShortNames(array $shortNames, array $uses): array
209217
{
210218
$shortNamesToFullyQualifiedNames = [];
211219

212220
foreach ($shortNames as $shortName) {
213-
$stmtsMatchedName = $this->useImportNameMatcher->matchNameWithStmts($shortName, $stmts);
221+
$stmtsMatchedName = $this->useImportNameMatcher->matchNameWithUses($shortName, $uses);
214222

215223
if ($stmtsMatchedName == null) {
216224
continue;

‎rules/CodingStyle/NodeAnalyzer/UseImportNameMatcher.php‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66

77
use Nette\Utils\Strings;
88
use PhpParser\Node\Identifier;
9-
use PhpParser\Node\Stmt;
109
use PhpParser\Node\Stmt\GroupUse;
1110
use PhpParser\Node\Stmt\Use_;
1211
use PhpParser\Node\UseItem;
1312
use Rector\Exception\ShouldNotHappenException;
1413
use Rector\Naming\Naming\UseImportsResolver;
1514
use Rector\NodeTypeResolver\Node\AttributeKey;
16-
use Rector\PhpParser\Node\BetterNodeFinder;
1715
use Rector\Util\StringUtils;
1816

1917
final readonly class UseImportNameMatcher
@@ -25,21 +23,10 @@
2523
private const string SHORT_NAME_REGEX = '#^%s(\\\\[\w]+)?$#i';
2624

2725
public function __construct(
28-
private BetterNodeFinder $betterNodeFinder,
2926
private UseImportsResolver $useImportsResolver
3027
) {
3128
}
3229

33-
/**
34-
* @param Stmt[] $stmts
35-
*/
36-
public function matchNameWithStmts(string $tag, array $stmts): ?string
37-
{
38-
/** @var Use_[] $uses */
39-
$uses = $this->betterNodeFinder->findInstanceOf($stmts, Use_::class);
40-
return $this->matchNameWithUses($tag, $uses);
41-
}
42-
4330
/**
4431
* @param array<Use_|GroupUse> $uses
4532
* @return non-empty-string|null

0 commit comments

Comments
 (0)