Skip to content

Commit 1db5a62

Browse files
committed
extract and re-use AnalysedFilesResolver
1 parent 86f0c28 commit 1db5a62

File tree

3 files changed

+54
-29
lines changed

3 files changed

+54
-29
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use function array_fill_keys;
6+
use function array_map;
7+
use function strtolower;
8+
use const PHP_OS_FAMILY;
9+
10+
final class AnalysedFilesResolver
11+
{
12+
13+
/** @var bool[] filePath(string) => bool(true) */
14+
private array $analysedFiles = [];
15+
16+
private bool $caseInsensitiveFilesystem;
17+
18+
public function __construct()
19+
{
20+
$this->caseInsensitiveFilesystem = PHP_OS_FAMILY === 'Darwin';
21+
}
22+
23+
/**
24+
* @param string[] $files
25+
*/
26+
public function setAnalysedFiles(array $files): void
27+
{
28+
if ($this->caseInsensitiveFilesystem) {
29+
$files = array_map(static fn (string $file): string => strtolower($file), $files);
30+
}
31+
$this->analysedFiles = array_fill_keys($files, true);
32+
}
33+
34+
public function isInAnalyzedFiles(string $file): bool
35+
{
36+
if ($this->caseInsensitiveFilesystem) {
37+
$file = strtolower($file);
38+
}
39+
40+
return isset($this->analysedFiles[$file]);
41+
}
42+
43+
}

src/Analyser/NodeScopeResolver.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ final class NodeScopeResolver
229229
private const LOOP_SCOPE_ITERATIONS = 3;
230230
private const GENERALIZE_AFTER_ITERATION = 1;
231231

232-
/** @var bool[] filePath(string) => bool(true) */
233-
private array $analysedFiles = [];
232+
private AnalysedFilesResolver $analysedFilesResolver;
234233

235234
/** @var array<string, true> */
236235
private array $earlyTerminatingMethodNames;
@@ -284,6 +283,7 @@ public function __construct(
284283
}
285284
}
286285
$this->earlyTerminatingMethodNames = $earlyTerminatingMethodNames;
286+
$this->analysedFilesResolver = new AnalysedFilesResolver();
287287
}
288288

289289
/**
@@ -292,7 +292,7 @@ public function __construct(
292292
*/
293293
public function setAnalysedFiles(array $files): void
294294
{
295-
$this->analysedFiles = array_fill_keys($files, true);
295+
$this->analysedFilesResolver->setAnalysedFiles($files);
296296
}
297297

298298
/**
@@ -6275,7 +6275,7 @@ private function processTraitUse(Node\Stmt\TraitUse $node, MutatingScope $classS
62756275
continue; // trait from eval or from PHP itself
62766276
}
62776277
$fileName = $this->fileHelper->normalizePath($traitFileName);
6278-
if (!isset($this->analysedFiles[$fileName])) {
6278+
if (!$this->analysedFilesResolver->isInAnalyzedFiles($fileName)) {
62796279
continue;
62806280
}
62816281
$adaptations = [];
@@ -6394,7 +6394,7 @@ private function processCalledMethod(MethodReflection $methodReflection): ?Mutat
63946394
$this->calledMethodStack[$stackName] = true;
63956395

63966396
$fileName = $this->fileHelper->normalizePath($declaringClass->getFileName());
6397-
if (!isset($this->analysedFiles[$fileName])) {
6397+
if (!$this->analysedFilesResolver->isInAnalyzedFiles($fileName)) {
63986398
return null;
63996399
}
64006400
$parserNodes = $this->parser->parseFile($fileName);

src/Parser/PathRoutingParser.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,21 @@
22

33
namespace PHPStan\Parser;
44

5+
use PHPStan\Analyser\AnalysedFilesResolver;
56
use PHPStan\File\FileHelper;
6-
use function array_fill_keys;
7-
use function array_map;
87
use function array_slice;
98
use function count;
109
use function explode;
1110
use function implode;
1211
use function is_link;
1312
use function realpath;
1413
use function str_contains;
15-
use function strtolower;
1614
use const DIRECTORY_SEPARATOR;
17-
use const PHP_OS_FAMILY;
1815

1916
final class PathRoutingParser implements Parser
2017
{
2118

22-
/** @var bool[] filePath(string) => bool(true) */
23-
private array $analysedFiles = [];
24-
25-
private bool $caseInsensitiveFilesystem;
19+
private AnalysedFilesResolver $analysedFilesResolver;
2620

2721
public function __construct(
2822
private FileHelper $fileHelper,
@@ -31,27 +25,15 @@ public function __construct(
3125
private Parser $php8Parser,
3226
)
3327
{
34-
$this->caseInsensitiveFilesystem = PHP_OS_FAMILY === 'Darwin';
28+
$this->analysedFilesResolver = new AnalysedFilesResolver();
3529
}
3630

3731
/**
3832
* @param string[] $files
3933
*/
4034
public function setAnalysedFiles(array $files): void
4135
{
42-
if ($this->caseInsensitiveFilesystem) {
43-
$files = array_map(static fn (string $file): string => strtolower($file), $files);
44-
}
45-
$this->analysedFiles = array_fill_keys($files, true);
46-
}
47-
48-
private function isInAnalyzedFiles(string $file): bool
49-
{
50-
if ($this->caseInsensitiveFilesystem) {
51-
$file = strtolower($file);
52-
}
53-
54-
return isset($this->analysedFiles[$file]);
36+
$this->analysedFilesResolver->setAnalysedFiles($files);
5537
}
5638

5739
public function parseFile(string $file): array
@@ -65,7 +47,7 @@ public function parseFile(string $file): array
6547
}
6648

6749
$file = $this->fileHelper->normalizePath($file);
68-
if (!$this->isInAnalyzedFiles($file)) {
50+
if (!$this->analysedFilesResolver->isInAnalyzedFiles($file)) {
6951
// check symlinked file that still might be in analysedFiles
7052
$pathParts = explode(DIRECTORY_SEPARATOR, $file);
7153
for ($i = count($pathParts); $i > 1; $i--) {
@@ -77,7 +59,7 @@ public function parseFile(string $file): array
7759
$realFilePath = realpath($file);
7860
if ($realFilePath !== false) {
7961
$normalizedRealFilePath = $this->fileHelper->normalizePath($realFilePath);
80-
if ($this->isInAnalyzedFiles($normalizedRealFilePath)) {
62+
if ($this->analysedFilesResolver->isInAnalyzedFiles($normalizedRealFilePath)) {
8163
return $this->currentPhpVersionRichParser->parseFile($file);
8264
}
8365
}

0 commit comments

Comments
 (0)