This repository was archived by the owner on May 20, 2025. It is now read-only.
forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetaFileParser.php
More file actions
127 lines (104 loc) · 3.28 KB
/
MetaFileParser.php
File metadata and controls
127 lines (104 loc) · 3.28 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php declare(strict_types = 1);
namespace PHPStan\PhpStormMeta;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PHPStan\File\FileHelper;
use PHPStan\Parser\CachedParser;
use PHPStan\PhpStormMeta\TypeMapping\CallReturnOverrideCollection;
use PHPStan\PhpStormMeta\TypeMapping\FunctionCallTypeOverride;
use PHPStan\PhpStormMeta\TypeMapping\MethodCallTypeOverride;
use SplFileInfo;
use Symfony\Component\Finder\Finder;
use function array_key_exists;
use function count;
use function file_exists;
use function is_dir;
class MetaFileParser
{
private readonly CallReturnOverrideCollection $parsedMeta;
/** @var array<string, string> */
private array $parsedMetaPaths = [];
private bool $metaParsed;
/**
* @param list<string> $metaPaths
*/
public function __construct(
private readonly CachedParser $parser,
private readonly OverrideParser $overrideParser,
private readonly FileHelper $fileHelper,
private readonly array $metaPaths,
)
{
$this->parsedMeta = new CallReturnOverrideCollection();
$this->metaParsed = $this->metaPaths === [];
}
public function getMeta(): CallReturnOverrideCollection
{
if (!$this->metaParsed) {
$this->parseMeta($this->parsedMeta, ...$this->metaPaths);
$this->metaParsed = true;
}
return $this->parsedMeta;
}
private function parseMeta(
CallReturnOverrideCollection $resultCollector,
string ...$metaPaths,
): void
{
$metaFiles = [];
/** @var array<string, string> $newlyParsedMetaPaths */
$newlyParsedMetaPaths = [];
foreach ($metaPaths as $relativePath) {
if (array_key_exists($relativePath, $this->parsedMetaPaths)) {
continue;
}
$path = $this->fileHelper->absolutizePath($relativePath);
if (is_dir($path)) {
$finder = (new Finder())->in($path)->files()->ignoreDotFiles(false);
foreach ($finder as $fileInfo) {
$metaFiles [] = $fileInfo->getPathname();
}
} elseif (file_exists($path)) {
$singleFile = new SplFileInfo($path);
$metaFiles[] = $singleFile->getPathname();
}
$newlyParsedMetaPaths[$relativePath] = $path;
}
foreach ($metaFiles as $metaFile) {
$stmts = $this->parser->parseFile($metaFile);
foreach ($stmts as $topStmt) {
if (!($topStmt instanceof Stmt\Namespace_)) {
continue;
}
if ($topStmt->name === null || $topStmt->name->toString() !== 'PHPSTORM_META') {
continue;
}
foreach ($topStmt->stmts as $metaStmt) {
if (!($metaStmt instanceof Expression)
|| !($metaStmt->expr instanceof FuncCall)
|| !($metaStmt->expr->name instanceof Name)
|| $metaStmt->expr->name->toString() !== 'override'
) {
continue;
}
$args = $metaStmt->expr->getArgs();
if (count($args) < 2) {
continue;
}
[$callableArg, $overrideArg] = $args;
$parsedOverride = $this->overrideParser->parseOverride($callableArg, $overrideArg);
if ($parsedOverride instanceof MethodCallTypeOverride) {
$resultCollector->addMethodCallOverride($parsedOverride);
} elseif ($parsedOverride instanceof FunctionCallTypeOverride) {
$resultCollector->addFunctionCallOverride($parsedOverride);
}
}
}
foreach ($newlyParsedMetaPaths as $relativePath => $path) {
$this->parsedMetaPaths[$relativePath] = $path;
}
}
}
}