4
4
5
5
use PHPStan \File \FileHelper ;
6
6
use function array_fill_keys ;
7
+ use function array_map ;
7
8
use function array_slice ;
8
9
use function count ;
9
10
use function explode ;
10
11
use function implode ;
11
12
use function is_link ;
12
13
use function realpath ;
13
14
use function str_contains ;
15
+ use function strtolower ;
14
16
use const DIRECTORY_SEPARATOR ;
17
+ use const PHP_OS_FAMILY ;
15
18
16
19
final class PathRoutingParser implements Parser
17
20
{
18
21
19
22
/** @var bool[] filePath(string) => bool(true) */
20
23
private array $ analysedFiles = [];
21
24
25
+ private bool $ caseInsensitiveFilesystem ;
26
+
22
27
public function __construct (
23
28
private FileHelper $ fileHelper ,
24
29
private Parser $ currentPhpVersionRichParser ,
25
30
private Parser $ currentPhpVersionSimpleParser ,
26
31
private Parser $ php8Parser ,
27
32
)
28
33
{
34
+ $ this ->caseInsensitiveFilesystem = PHP_OS_FAMILY === 'Darwin ' ;
29
35
}
30
36
31
37
/**
32
38
* @param string[] $files
33
39
*/
34
40
public function setAnalysedFiles (array $ files ): void
35
41
{
42
+ if ($ this ->caseInsensitiveFilesystem ) {
43
+ $ files = array_map (static fn (string $ file ): string => strtolower ($ file ), $ files );
44
+ }
36
45
$ this ->analysedFiles = array_fill_keys ($ files , true );
37
46
}
38
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 ]);
55
+ }
56
+
39
57
public function parseFile (string $ file ): array
40
58
{
41
59
$ normalizedPath = $ this ->fileHelper ->normalizePath ($ file , '/ ' );
@@ -47,7 +65,7 @@ public function parseFile(string $file): array
47
65
}
48
66
49
67
$ file = $ this ->fileHelper ->normalizePath ($ file );
50
- if (!isset ( $ this ->analysedFiles [ $ file] )) {
68
+ if (!$ this ->isInAnalyzedFiles ( $ file )) {
51
69
// check symlinked file that still might be in analysedFiles
52
70
$ pathParts = explode (DIRECTORY_SEPARATOR , $ file );
53
71
for ($ i = count ($ pathParts ); $ i > 1 ; $ i --) {
@@ -59,7 +77,7 @@ public function parseFile(string $file): array
59
77
$ realFilePath = realpath ($ file );
60
78
if ($ realFilePath !== false ) {
61
79
$ normalizedRealFilePath = $ this ->fileHelper ->normalizePath ($ realFilePath );
62
- if (isset ( $ this ->analysedFiles [ $ normalizedRealFilePath] )) {
80
+ if ($ this ->isInAnalyzedFiles ( $ normalizedRealFilePath )) {
63
81
return $ this ->currentPhpVersionRichParser ->parseFile ($ file );
64
82
}
65
83
}
0 commit comments