Skip to content

Commit e52951a

Browse files
ondrejmirtesclaude
andcommitted
Move ExportedNameScope to PHPStan\Analyser\NamespaceUses
The namespace and the use statements in effect at a point in a file are not specific to exported nodes. FileTypeMapper tracks the same data while building a name scope map, so move the value object and its tracker next to the name scopes where both can use them. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0182BrC263VCQTibEPTJX2UY
1 parent 9026f70 commit e52951a

9 files changed

Lines changed: 59 additions & 56 deletions
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types = 1);
22

3-
namespace PHPStan\Dependency;
3+
namespace PHPStan\Analyser;
44

55
/**
66
* The namespace and the use statements in effect at a point in a file.
@@ -11,7 +11,7 @@
1111
* in it - far more than the exported nodes need, and it happens in the main process during a
1212
* result cache restore, in front of the analysis.
1313
*/
14-
final class ExportedNameScope
14+
final class NamespaceUses
1515
{
1616

1717
/**

src/Dependency/ExportedNameScopeTracker.php renamed to src/Analyser/NamespaceUsesTracker.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types = 1);
22

3-
namespace PHPStan\Dependency;
3+
namespace PHPStan\Analyser;
44

55
use PhpParser\Node;
66
use function sprintf;
@@ -17,7 +17,7 @@
1717
* cleared when a namespace is entered rather than when it is left. Namespace blocks cover every
1818
* statement of a file, so nothing can observe the difference.
1919
*/
20-
final class ExportedNameScopeTracker
20+
final class NamespaceUsesTracker
2121
{
2222

2323
/** @var non-empty-string|null */
@@ -29,14 +29,14 @@ final class ExportedNameScopeTracker
2929
/** @var array<string, string> */
3030
private array $constUses = [];
3131

32-
private ?ExportedNameScope $nameScope = null;
32+
private ?NamespaceUses $namespaceUses = null;
3333

3434
public function reset(): void
3535
{
3636
$this->namespace = null;
3737
$this->uses = [];
3838
$this->constUses = [];
39-
$this->nameScope = null;
39+
$this->namespaceUses = null;
4040
}
4141

4242
public function enterNode(Node $node): void
@@ -70,12 +70,12 @@ public function enterNode(Node $node): void
7070
return;
7171
}
7272

73-
$this->nameScope = null;
73+
$this->namespaceUses = null;
7474
}
7575

76-
public function getNameScope(): ExportedNameScope
76+
public function getNamespaceUses(): NamespaceUses
7777
{
78-
return $this->nameScope ??= new ExportedNameScope($this->namespace, $this->uses, $this->constUses);
78+
return $this->namespaceUses ??= new NamespaceUses($this->namespace, $this->uses, $this->constUses);
7979
}
8080

8181
}

‎src/Dependency/DependencyResolver.php‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\Node\Expr\Closure;
1010
use PhpParser\Node\Name;
1111
use PhpParser\Node\Stmt\Foreach_;
12+
use PHPStan\Analyser\NamespaceUsesTracker;
1213
use PHPStan\Analyser\Scope;
1314
use PHPStan\Broker\ClassNotFoundException;
1415
use PHPStan\Broker\FunctionNotFoundException;
@@ -100,7 +101,7 @@ final class DependencyResolver
100101
Node\Expr\FuncCall::class,
101102
];
102103

103-
/** Node classes ExportedNameScopeTracker::enterNode() reacts to */
104+
/** Node classes NamespaceUsesTracker::enterNode() reacts to */
104105
private const NAME_SCOPE_NODE_TYPES = [
105106
Node\Stmt\Namespace_::class,
106107
Node\Stmt\Use_::class,
@@ -113,7 +114,7 @@ final class DependencyResolver
113114
/** @var array<class-string, int> */
114115
private array $nodeProfiles = [];
115116

116-
private ExportedNameScopeTracker $nameScopeTracker;
117+
private NamespaceUsesTracker $nameScopeTracker;
117118

118119
private ?string $nameScopeFile = null;
119120

@@ -125,7 +126,7 @@ public function __construct(
125126
private FileTypeMapper $fileTypeMapper,
126127
)
127128
{
128-
$this->nameScopeTracker = new ExportedNameScopeTracker();
129+
$this->nameScopeTracker = new NamespaceUsesTracker();
129130
}
130131

131132
/**
@@ -164,7 +165,7 @@ public function resolveDependencies(Node $node, Scope $scope): ?NodeDependencies
164165
}
165166

166167
$exportedNode = ($nodeProfile & self::PROFILE_EXPORT) !== 0
167-
? $this->exportedNodeResolver->resolve($node, $this->nameScopeTracker->getNameScope())
168+
? $this->exportedNodeResolver->resolve($node, $this->nameScopeTracker->getNamespaceUses())
168169
: null;
169170

170171
if ($dependenciesReflections === [] && $dependenciesFilePaths === [] && $exportedNode === null) {

‎src/Dependency/ExportedNodeResolver.php‎

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Stmt\Class_;
99
use PhpParser\Node\Stmt\ClassMethod;
1010
use PhpParser\Node\Stmt\Function_;
11+
use PHPStan\Analyser\NamespaceUses;
1112
use PHPStan\Dependency\ExportedNode\ExportedAttributeNode;
1213
use PHPStan\Dependency\ExportedNode\ExportedClassConstantNode;
1314
use PHPStan\Dependency\ExportedNode\ExportedClassConstantsNode;
@@ -44,7 +45,7 @@ public function __construct(
4445
{
4546
}
4647

47-
public function resolve(Node $node, ExportedNameScope $nameScope): ?RootExportedNode
48+
public function resolve(Node $node, NamespaceUses $namespaceUses): ?RootExportedNode
4849
{
4950
if ($node instanceof Class_ && isset($node->namespacedName)) {
5051
$docComment = $node->getDocComment();
@@ -73,7 +74,7 @@ public function resolve(Node $node, ExportedNameScope $nameScope): ?RootExported
7374

7475
return new ExportedClassNode(
7576
$className,
76-
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $nameScope),
77+
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $namespaceUses),
7778
$node->isAbstract(),
7879
$node->isFinal(),
7980
$extendsName,
@@ -99,7 +100,7 @@ public function resolve(Node $node, ExportedNameScope $nameScope): ?RootExported
99100

100101
throw new ShouldNotHappenException();
101102
}, $adaptations),
102-
$this->exportClassStatements($node->stmts, $className, $nameScope),
103+
$this->exportClassStatements($node->stmts, $className, $namespaceUses),
103104
$this->exportAttributeNodes($node->attrGroups),
104105
);
105106
}
@@ -112,9 +113,9 @@ public function resolve(Node $node, ExportedNameScope $nameScope): ?RootExported
112113

113114
return new ExportedInterfaceNode(
114115
$interfaceName,
115-
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $nameScope),
116+
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $namespaceUses),
116117
$extendsNames,
117-
$this->exportClassStatements($node->stmts, $interfaceName, $nameScope),
118+
$this->exportClassStatements($node->stmts, $interfaceName, $namespaceUses),
118119
);
119120
}
120121

@@ -131,9 +132,9 @@ public function resolve(Node $node, ExportedNameScope $nameScope): ?RootExported
131132
return new ExportedEnumNode(
132133
$enumName,
133134
$scalarType,
134-
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $nameScope),
135+
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $namespaceUses),
135136
$implementsNames,
136-
$this->exportClassStatements($node->stmts, $enumName, $nameScope),
137+
$this->exportClassStatements($node->stmts, $enumName, $namespaceUses),
137138
$this->exportAttributeNodes($node->attrGroups),
138139
);
139140
}
@@ -155,7 +156,7 @@ public function resolve(Node $node, ExportedNameScope $nameScope): ?RootExported
155156

156157
return new ExportedTraitNode(
157158
$className,
158-
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $nameScope),
159+
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $namespaceUses),
159160
$usedTraits,
160161
array_map(static function (Node\Stmt\TraitUseAdaptation $adaptation): ExportedTraitUseAdaptation {
161162
if ($adaptation instanceof Node\Stmt\TraitUseAdaptation\Alias) {
@@ -177,7 +178,7 @@ public function resolve(Node $node, ExportedNameScope $nameScope): ?RootExported
177178

178179
throw new ShouldNotHappenException();
179180
}, $adaptations),
180-
$this->exportClassStatements($node->stmts, $className, $nameScope),
181+
$this->exportClassStatements($node->stmts, $className, $namespaceUses),
181182
$this->exportAttributeNodes($node->attrGroups),
182183
);
183184
}
@@ -192,10 +193,10 @@ public function resolve(Node $node, ExportedNameScope $nameScope): ?RootExported
192193

193194
return new ExportedFunctionNode(
194195
$functionName,
195-
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $nameScope),
196+
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $namespaceUses),
196197
$node->byRef,
197198
NodeTypePrinter::printType($node->returnType),
198-
$this->exportParameterNodes($node->params, $nameScope),
199+
$this->exportParameterNodes($node->params, $namespaceUses),
199200
$this->exportAttributeNodes($node->attrGroups),
200201
);
201202
}
@@ -238,7 +239,7 @@ public function resolve(Node $node, ExportedNameScope $nameScope): ?RootExported
238239
* @param Node\Param[] $params
239240
* @return ExportedParameterNode[]
240241
*/
241-
private function exportParameterNodes(array $params, ExportedNameScope $nameScope): array
242+
private function exportParameterNodes(array $params, NamespaceUses $namespaceUses): array
242243
{
243244
$nodes = [];
244245
foreach ($params as $param) {
@@ -267,32 +268,32 @@ private function exportParameterNodes(array $params, ExportedNameScope $nameScop
267268
$param->variadic,
268269
$param->default !== null,
269270
$this->exportAttributeNodes($param->attrGroups),
270-
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $nameScope),
271+
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $namespaceUses),
271272
$param->flags,
272273
);
273274
}
274275

275276
return $nodes;
276277
}
277278

278-
private function exportPhpDocNode(?string $text, ExportedNameScope $nameScope): ?ExportedPhpDocNode
279+
private function exportPhpDocNode(?string $text, NamespaceUses $namespaceUses): ?ExportedPhpDocNode
279280
{
280281
if ($text === null) {
281282
return null;
282283
}
283284

284-
return new ExportedPhpDocNode($text, $nameScope->getNamespace(), $nameScope->getUses(), $nameScope->getConstUses());
285+
return new ExportedPhpDocNode($text, $namespaceUses->getNamespace(), $namespaceUses->getUses(), $namespaceUses->getConstUses());
285286
}
286287

287288
/**
288289
* @param Node\Stmt[] $statements
289290
* @return ExportedNode[]
290291
*/
291-
private function exportClassStatements(array $statements, string $namespacedName, ExportedNameScope $nameScope): array
292+
private function exportClassStatements(array $statements, string $namespacedName, NamespaceUses $namespaceUses): array
292293
{
293294
$exportedNodes = [];
294295
foreach ($statements as $statement) {
295-
$exportedNode = $this->exportClassStatement($statement, $namespacedName, $nameScope);
296+
$exportedNode = $this->exportClassStatement($statement, $namespacedName, $namespaceUses);
296297
if ($exportedNode === null) {
297298
continue;
298299
}
@@ -303,7 +304,7 @@ private function exportClassStatements(array $statements, string $namespacedName
303304
return $exportedNodes;
304305
}
305306

306-
private function exportClassStatement(Node\Stmt $node, string $namespacedName, ExportedNameScope $nameScope): ?ExportedNode
307+
private function exportClassStatement(Node\Stmt $node, string $namespacedName, NamespaceUses $namespaceUses): ?ExportedNode
307308
{
308309
if ($node instanceof ClassMethod) {
309310
if ($node->isAbstract() || $node->isFinal() || !$node->isPrivate()) {
@@ -312,15 +313,15 @@ private function exportClassStatement(Node\Stmt $node, string $namespacedName, E
312313

313314
return new ExportedMethodNode(
314315
$methodName,
315-
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $nameScope),
316+
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $namespaceUses),
316317
$node->byRef,
317318
$node->isPublic(),
318319
$node->isPrivate(),
319320
$node->isAbstract(),
320321
$node->isFinal(),
321322
$node->isStatic(),
322323
NodeTypePrinter::printType($node->returnType),
323-
$this->exportParameterNodes($node->params, $nameScope),
324+
$this->exportParameterNodes($node->params, $namespaceUses),
324325
$this->exportAttributeNodes($node->attrGroups),
325326
);
326327
}
@@ -353,7 +354,7 @@ private function exportClassStatement(Node\Stmt $node, string $namespacedName, E
353354

354355
return new ExportedPropertiesNode(
355356
$names,
356-
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $nameScope),
357+
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $namespaceUses),
357358
NodeTypePrinter::printType($node->type),
358359
$node->isPublic(),
359360
$node->isPrivate(),
@@ -366,7 +367,7 @@ private function exportClassStatement(Node\Stmt $node, string $namespacedName, E
366367
$node->isPrivateSet(),
367368
$virtual,
368369
$this->exportAttributeNodes($node->attrGroups),
369-
$this->exportPropertyHooks($node->hooks, $nameScope),
370+
$this->exportPropertyHooks($node->hooks, $namespaceUses),
370371
);
371372
}
372373

@@ -391,7 +392,7 @@ private function exportClassStatement(Node\Stmt $node, string $namespacedName, E
391392
$node->isPublic(),
392393
$node->isPrivate(),
393394
$node->isFinal(),
394-
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $nameScope),
395+
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $namespaceUses),
395396
);
396397
}
397398

@@ -401,7 +402,7 @@ private function exportClassStatement(Node\Stmt $node, string $namespacedName, E
401402
return new ExportedEnumCaseNode(
402403
$node->name->toString(),
403404
$node->expr !== null ? $this->exprPrinter->printExpr($node->expr) : null,
404-
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $nameScope),
405+
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $namespaceUses),
405406
);
406407
}
407408

@@ -438,7 +439,7 @@ private function exportAttributeNodes(array $attributeGroups): array
438439
*/
439440
private function exportPropertyHooks(
440441
array $hooks,
441-
ExportedNameScope $nameScope,
442+
NamespaceUses $namespaceUses,
442443
): array
443444
{
444445
$nodes = [];
@@ -450,12 +451,12 @@ private function exportPropertyHooks(
450451
}
451452
$nodes[] = new ExportedPropertyHookNode(
452453
$hook->name->toString(),
453-
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $nameScope),
454+
$this->exportPhpDocNode($docComment !== null ? $docComment->getText() : null, $namespaceUses),
454455
$hook->byRef,
455456
$hook->body === null,
456457
$hook->isFinal(),
457458
$hook->body instanceof Expr,
458-
$this->exportParameterNodes($hook->params, $nameScope),
459+
$this->exportParameterNodes($hook->params, $namespaceUses),
459460
$this->exportAttributeNodes($hook->attrGroups),
460461
);
461462
}

‎src/Dependency/ExportedNodeVisitor.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node;
77
use PhpParser\NodeVisitor;
88
use PhpParser\NodeVisitorAbstract;
9+
use PHPStan\Analyser\NamespaceUsesTracker;
910
use PHPStan\DependencyInjection\AutowiredService;
1011
use PHPStan\ShouldNotHappenException;
1112

@@ -19,15 +20,15 @@ final class ExportedNodeVisitor extends NodeVisitorAbstract
1920
/** @var RootExportedNode[] */
2021
private array $currentNodes = [];
2122

22-
private ExportedNameScopeTracker $nameScopeTracker;
23+
private NamespaceUsesTracker $nameScopeTracker;
2324

2425
/**
2526
* ExportedNodeVisitor constructor.
2627
*
2728
*/
2829
public function __construct(private ExportedNodeResolver $exportedNodeResolver)
2930
{
30-
$this->nameScopeTracker = new ExportedNameScopeTracker();
31+
$this->nameScopeTracker = new NamespaceUsesTracker();
3132
}
3233

3334
public function reset(string $fileName): void
@@ -52,7 +53,7 @@ public function enterNode(Node $node): ?int
5253
throw new ShouldNotHappenException();
5354
}
5455
$this->nameScopeTracker->enterNode($node);
55-
$exportedNode = $this->exportedNodeResolver->resolve($node, $this->nameScopeTracker->getNameScope());
56+
$exportedNode = $this->exportedNodeResolver->resolve($node, $this->nameScopeTracker->getNamespaceUses());
5657
if ($exportedNode !== null) {
5758
$this->currentNodes[] = $exportedNode;
5859
}

0 commit comments

Comments
 (0)