|
9 | 9 | use PhpParser\Node\Name; |
10 | 10 | use PhpParser\Node\Stmt; |
11 | 11 | use PhpParser\Node\Stmt\ClassLike; |
| 12 | +use PhpParser\Node\Stmt\Use_; |
12 | 13 | use PHPStan\PhpDocParser\Ast\NodeTraverser; |
13 | 14 | use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; |
14 | 15 | use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
@@ -120,16 +121,23 @@ private function resolveForStmts(array $stmts): array |
120 | 121 | { |
121 | 122 | $shortNamesToFullyQualifiedNames = []; |
122 | 123 | $docBlockShortNames = []; |
| 124 | + $uses = []; |
123 | 125 |
|
124 | 126 | // single traversal collects both PHP names and docblock short names, so the file stmts are walked once |
125 | 127 | $this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use ( |
126 | 128 | &$shortNamesToFullyQualifiedNames, |
127 | | - &$docBlockShortNames |
| 129 | + &$docBlockShortNames, |
| 130 | + &$uses |
128 | 131 | ): null { |
129 | 132 | foreach ($this->resolveDocBlockShortNames($node) as $docBlockShortName) { |
130 | 133 | $docBlockShortNames[] = $docBlockShortName; |
131 | 134 | } |
132 | 135 |
|
| 136 | + if ($node instanceof Use_) { |
| 137 | + $uses[] = $node; |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
133 | 141 | // class name is used! |
134 | 142 | if ($node instanceof ClassLike && $node->name instanceof Identifier) { |
135 | 143 | $fullyQualifiedName = $this->nodeNameResolver->getName($node); |
@@ -160,7 +168,7 @@ private function resolveForStmts(array $stmts): array |
160 | 168 | return null; |
161 | 169 | }); |
162 | 170 |
|
163 | | - $docBlockShortNamesToFullyQualifiedNames = $this->fqnizeShortNames($docBlockShortNames, $stmts); |
| 171 | + $docBlockShortNamesToFullyQualifiedNames = $this->fqnizeShortNames($docBlockShortNames, $uses); |
164 | 172 | /** @var array<string, string> $result */ |
165 | 173 | $result = [...$shortNamesToFullyQualifiedNames, ...$docBlockShortNamesToFullyQualifiedNames]; |
166 | 174 | return $result; |
@@ -202,15 +210,15 @@ static function ($node) use (&$shortNames): null { |
202 | 210 |
|
203 | 211 | /** |
204 | 212 | * @param string[] $shortNames |
205 | | - * @param Stmt[] $stmts |
| 213 | + * @param Use_[] $uses |
206 | 214 | * @return array<string, string> |
207 | 215 | */ |
208 | | - private function fqnizeShortNames(array $shortNames, array $stmts): array |
| 216 | + private function fqnizeShortNames(array $shortNames, array $uses): array |
209 | 217 | { |
210 | 218 | $shortNamesToFullyQualifiedNames = []; |
211 | 219 |
|
212 | 220 | foreach ($shortNames as $shortName) { |
213 | | - $stmtsMatchedName = $this->useImportNameMatcher->matchNameWithStmts($shortName, $stmts); |
| 221 | + $stmtsMatchedName = $this->useImportNameMatcher->matchNameWithUses($shortName, $uses); |
214 | 222 |
|
215 | 223 | if ($stmtsMatchedName == null) { |
216 | 224 | continue; |
|
0 commit comments