Skip to content

Commit 0b76399

Browse files
committed
Merge branch 2.2.x into 2.3.x
2 parents 39c862b + d72de75 commit 0b76399

19 files changed

Lines changed: 660 additions & 110 deletions

‎src/Analyser/ResultCache/ResultCacheManager.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2145,7 +2145,7 @@ private function getScannedFiles(array $allAnalysedFiles): array
21452145
}
21462146

21472147
$directories = array_unique(array_merge($analysedDirectories, $this->scanDirectories));
2148-
foreach ($this->scanFileFinder->findFiles($directories)->getFiles() as $file) {
2148+
foreach ($this->scanFileFinder->findFilesCached($directories)->getFiles() as $file) {
21492149
$scannedFiles[] = $file;
21502150
}
21512151

‎src/Command/CommandHelper.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ public static function begin(
612612
$errorOutput->writeLineFormatted('At least one path must be specified to analyse.');
613613
throw new InceptionNotSuccessfulException();
614614
}
615-
$fileFinderResult = $fileFinder->findFiles($paths);
615+
$fileFinderResult = $fileFinder->findFilesCached($paths);
616616
$files = $fileFinderResult->getFiles();
617617

618618
$pathRoutingParser->setAnalysedFiles($files);

‎src/Dependency/DependencyResolver.php‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ final class DependencyResolver
4747
/** @var array<string, list<ClassReflection|FunctionReflection|ConstantReflection>> */
4848
private array $classDependencies = [];
4949

50+
private ExportedNameScopeTracker $nameScopeTracker;
51+
52+
private ?string $nameScopeFile = null;
53+
5054
public function __construct(
5155
private FileHelper $fileHelper,
5256
private IncludedFilePathResolver $includedFilePathResolver,
@@ -55,10 +59,21 @@ public function __construct(
5559
private FileTypeMapper $fileTypeMapper,
5660
)
5761
{
62+
$this->nameScopeTracker = new ExportedNameScopeTracker();
5863
}
5964

6065
public function resolveDependencies(Node $node, Scope $scope): NodeDependencies
6166
{
67+
// The exported nodes written to the result cache have to record the same PHPDoc scope the
68+
// restore computes when it re-reads the file, so both go through the tracker. The nodes
69+
// arrive here in document order, one file at a time.
70+
$file = $scope->getFile();
71+
if ($file !== $this->nameScopeFile) {
72+
$this->nameScopeFile = $file;
73+
$this->nameScopeTracker->reset();
74+
}
75+
$this->nameScopeTracker->enterNode($node);
76+
6277
$dependenciesReflections = [];
6378
$dependenciesFilePaths = [];
6479

@@ -552,7 +567,7 @@ public function resolveDependencies(Node $node, Scope $scope): NodeDependencies
552567
}
553568
}
554569

555-
return new NodeDependencies($this->fileHelper, $dependenciesReflections, $this->exportedNodeResolver->resolve($scope->getFile(), $node), $dependenciesFilePaths);
570+
return new NodeDependencies($this->fileHelper, $dependenciesReflections, $this->exportedNodeResolver->resolve($node, $this->nameScopeTracker->getNameScope()), $dependenciesFilePaths);
556571
}
557572

558573
public function resolveUsedTraitDependencies(InClassNode $inClassNode): NodeDependencies
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Dependency;
4+
5+
/**
6+
* The namespace and the use statements in effect at a point in a file.
7+
*
8+
* This is everything an ExportedPhpDocNode records about the scope a PHPDoc was written in, and
9+
* all of it is read straight off the AST by ExportedNodeVisitor. Asking FileTypeMapper for a
10+
* NameScope instead would build the name scope map of the whole file, which resolves every PHPDoc
11+
* in it - far more than the exported nodes need, and it happens in the main process during a
12+
* result cache restore, in front of the analysis.
13+
*/
14+
final class ExportedNameScope
15+
{
16+
17+
/**
18+
* @param non-empty-string|null $namespace
19+
* @param array<string, string> $uses alias(string) => fullName(string)
20+
* @param array<string, string> $constUses alias(string) => fullName(string)
21+
*/
22+
public function __construct(
23+
private ?string $namespace,
24+
private array $uses,
25+
private array $constUses,
26+
)
27+
{
28+
}
29+
30+
/**
31+
* @return non-empty-string|null
32+
*/
33+
public function getNamespace(): ?string
34+
{
35+
return $this->namespace;
36+
}
37+
38+
/**
39+
* @return array<string, string>
40+
*/
41+
public function getUses(): array
42+
{
43+
return $this->uses;
44+
}
45+
46+
/**
47+
* @return array<string, string>
48+
*/
49+
public function getConstUses(): array
50+
{
51+
return $this->constUses;
52+
}
53+
54+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Dependency;
4+
5+
use PhpParser\Node;
6+
use function sprintf;
7+
use function strtolower;
8+
9+
/**
10+
* Keeps the namespace and the use statements seen so far while walking a file's statements.
11+
*
12+
* Both places that build exported nodes drive one of these - ExportedNodeVisitor for a result
13+
* cache restore, DependencyResolver during the analysis - so that the PHPDoc scope recorded in
14+
* the cache and the one computed when checking the cache are produced by the same code.
15+
*
16+
* It mirrors what FileTypeMapper does while building a name scope map, except that the uses are
17+
* cleared when a namespace is entered rather than when it is left. Namespace blocks cover every
18+
* statement of a file, so nothing can observe the difference.
19+
*/
20+
final class ExportedNameScopeTracker
21+
{
22+
23+
/** @var non-empty-string|null */
24+
private ?string $namespace = null;
25+
26+
/** @var array<string, string> */
27+
private array $uses = [];
28+
29+
/** @var array<string, string> */
30+
private array $constUses = [];
31+
32+
private ?ExportedNameScope $nameScope = null;
33+
34+
public function reset(): void
35+
{
36+
$this->namespace = null;
37+
$this->uses = [];
38+
$this->constUses = [];
39+
$this->nameScope = null;
40+
}
41+
42+
public function enterNode(Node $node): void
43+
{
44+
if ($node instanceof Node\Stmt\Namespace_) {
45+
$this->namespace = $node->name !== null ? $node->name->toString() : null;
46+
$this->uses = [];
47+
$this->constUses = [];
48+
} elseif ($node instanceof Node\Stmt\Use_) {
49+
if ($node->type === Node\Stmt\Use_::TYPE_NORMAL) {
50+
foreach ($node->uses as $use) {
51+
$this->uses[strtolower($use->getAlias()->name)] = $use->name->toString();
52+
}
53+
} elseif ($node->type === Node\Stmt\Use_::TYPE_CONSTANT) {
54+
foreach ($node->uses as $use) {
55+
$this->constUses[strtolower($use->getAlias()->name)] = $use->name->toString();
56+
}
57+
} else {
58+
return;
59+
}
60+
} elseif ($node instanceof Node\Stmt\GroupUse) {
61+
$prefix = $node->prefix->toString();
62+
foreach ($node->uses as $use) {
63+
if ($node->type === Node\Stmt\Use_::TYPE_NORMAL || $use->type === Node\Stmt\Use_::TYPE_NORMAL) {
64+
$this->uses[strtolower($use->getAlias()->name)] = sprintf('%s\\%s', $prefix, $use->name->toString());
65+
} elseif ($node->type === Node\Stmt\Use_::TYPE_CONSTANT || $use->type === Node\Stmt\Use_::TYPE_CONSTANT) {
66+
$this->constUses[strtolower($use->getAlias()->name)] = sprintf('%s\\%s', $prefix, $use->name->toString());
67+
}
68+
}
69+
} else {
70+
return;
71+
}
72+
73+
$this->nameScope = null;
74+
}
75+
76+
public function getNameScope(): ExportedNameScope
77+
{
78+
return $this->nameScope ??= new ExportedNameScope($this->namespace, $this->uses, $this->constUses);
79+
}
80+
81+
}

0 commit comments

Comments
 (0)