Skip to content

Commit 2f6f978

Browse files
committed
extract and re-use AnalysedFilesResolver
1 parent 23a7403 commit 2f6f978

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
@@ -230,8 +230,7 @@ final class NodeScopeResolver
230230
private const LOOP_SCOPE_ITERATIONS = 3;
231231
private const GENERALIZE_AFTER_ITERATION = 1;
232232

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

236235
/** @var array<string, true> */
237236
private array $earlyTerminatingMethodNames;
@@ -285,6 +284,7 @@ public function __construct(
285284
}
286285
}
287286
$this->earlyTerminatingMethodNames = $earlyTerminatingMethodNames;
287+
$this->analysedFilesResolver = new AnalysedFilesResolver();
288288
}
289289

290290
/**
@@ -293,7 +293,7 @@ public function __construct(
293293
*/
294294
public function setAnalysedFiles(array $files): void
295295
{
296-
$this->analysedFiles = array_fill_keys($files, true);
296+
$this->analysedFilesResolver->setAnalysedFiles($files);
297297
}
298298

299299
/**
@@ -6283,7 +6283,7 @@ private function processTraitUse(Node\Stmt\TraitUse $node, MutatingScope $classS
62836283
continue; // trait from eval or from PHP itself
62846284
}
62856285
$fileName = $this->fileHelper->normalizePath($traitFileName);
6286-
if (!isset($this->analysedFiles[$fileName])) {
6286+
if (!$this->analysedFilesResolver->isInAnalyzedFiles($fileName)) {
62876287
continue;
62886288
}
62896289
$adaptations = [];
@@ -6402,7 +6402,7 @@ private function processCalledMethod(MethodReflection $methodReflection): ?Mutat
64026402
$this->calledMethodStack[$stackName] = true;
64036403

64046404
$fileName = $this->fileHelper->normalizePath($declaringClass->getFileName());
6405-
if (!isset($this->analysedFiles[$fileName])) {
6405+
if (!$this->analysedFilesResolver->isInAnalyzedFiles($fileName)) {
64066406
return null;
64076407
}
64086408
$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)