forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathRoutingParser.php
More file actions
84 lines (70 loc) · 2.37 KB
/
Copy pathPathRoutingParser.php
File metadata and controls
84 lines (70 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php declare(strict_types = 1);
namespace PHPStan\Parser;
use PHPStan\File\FileHelper;
use function array_fill_keys;
use function array_slice;
use function count;
use function explode;
use function implode;
use function is_link;
use function realpath;
use function str_contains;
use const DIRECTORY_SEPARATOR;
final class PathRoutingParser implements Parser
{
private ?string $singleReflectionFile;
/** @var array<string, true> filePath(string) => bool(true) */
private array $analysedFiles = [];
public function __construct(
private FileHelper $fileHelper,
private Parser $currentPhpVersionRichParser,
private Parser $currentPhpVersionSimpleParser,
private Parser $php8Parser,
?string $singleReflectionFile,
)
{
$this->singleReflectionFile = $singleReflectionFile !== null ? $fileHelper->normalizePath($singleReflectionFile) : null;
}
/**
* @param string[] $files
*/
public function setAnalysedFiles(array $files): void
{
$this->analysedFiles = array_fill_keys($files, true);
}
public function parseFile(string $file): array
{
$normalizedPath = $this->fileHelper->normalizePath($file, '/');
if (str_contains($normalizedPath, 'vendor/jetbrains/phpstorm-stubs')) {
return $this->php8Parser->parseFile($file);
}
if (str_contains($normalizedPath, 'vendor/phpstan/php-8-stubs/stubs')) {
return $this->php8Parser->parseFile($file);
}
$file = $this->fileHelper->normalizePath($file);
if (!isset($this->analysedFiles[$file]) && $file !== $this->singleReflectionFile) {
// check symlinked file that still might be in analysedFiles
$pathParts = explode(DIRECTORY_SEPARATOR, $file);
for ($i = count($pathParts); $i > 1; $i--) {
$joinedPartOfPath = implode(DIRECTORY_SEPARATOR, array_slice($pathParts, 0, $i));
if (!@is_link($joinedPartOfPath)) {
continue;
}
$realFilePath = realpath($file);
if ($realFilePath !== false) {
$normalizedRealFilePath = $this->fileHelper->normalizePath($realFilePath);
if (isset($this->analysedFiles[$normalizedRealFilePath])) {
return $this->currentPhpVersionRichParser->parseFile($file);
}
}
break;
}
return $this->currentPhpVersionSimpleParser->parseFile($file);
}
return $this->currentPhpVersionRichParser->parseFile($file);
}
public function parseString(string $sourceCode): array
{
return $this->currentPhpVersionSimpleParser->parseString($sourceCode);
}
}