/
Analyser.php
118 lines (103 loc) · 2.97 KB
/
Analyser.php
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
<?php declare(strict_types = 1);
namespace PHPStan\Analyser;
use Closure;
use PHPStan\Collectors\CollectedData;
use PHPStan\Collectors\Registry as CollectorRegistry;
use PHPStan\Rules\Registry as RuleRegistry;
use Throwable;
use function array_fill_keys;
use function array_merge;
use function count;
use function memory_get_peak_usage;
use function sprintf;
class Analyser
{
public function __construct(
private FileAnalyser $fileAnalyser,
private RuleRegistry $ruleRegistry,
private CollectorRegistry $collectorRegistry,
private NodeScopeResolver $nodeScopeResolver,
private int $internalErrorsCountLimit,
)
{
}
/**
* @param string[] $files
* @param Closure(string $file): void|null $preFileCallback
* @param Closure(int ): void|null $postFileCallback
* @param string[]|null $allAnalysedFiles
*/
public function analyse(
array $files,
?Closure $preFileCallback = null,
?Closure $postFileCallback = null,
bool $debug = false,
?array $allAnalysedFiles = null,
): AnalyserResult
{
if ($allAnalysedFiles === null) {
$allAnalysedFiles = $files;
}
$this->nodeScopeResolver->setAnalysedFiles($allAnalysedFiles);
$allAnalysedFiles = array_fill_keys($allAnalysedFiles, true);
/** @var list<Error> $errors */
$errors = [];
/** @var list<CollectedData> $collectedData */
$collectedData = [];
$internalErrorsCount = 0;
$reachedInternalErrorsCountLimit = false;
$dependencies = [];
$exportedNodes = [];
foreach ($files as $file) {
if ($preFileCallback !== null) {
$preFileCallback($file);
}
try {
$fileAnalyserResult = $this->fileAnalyser->analyseFile(
$file,
$allAnalysedFiles,
$this->ruleRegistry,
$this->collectorRegistry,
null,
);
$errors = array_merge($errors, $fileAnalyserResult->getErrors());
$collectedData = array_merge($collectedData, $fileAnalyserResult->getCollectedData());
$dependencies[$file] = $fileAnalyserResult->getDependencies();
$fileExportedNodes = $fileAnalyserResult->getExportedNodes();
if (count($fileExportedNodes) > 0) {
$exportedNodes[$file] = $fileExportedNodes;
}
} catch (Throwable $t) {
if ($debug) {
throw $t;
}
$internalErrorsCount++;
$internalErrorMessage = sprintf('Internal error: %s', $t->getMessage());
$internalErrorMessage .= sprintf(
'%sRun PHPStan with --debug option and post the stack trace to:%s%s',
"\n",
"\n",
'https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md',
);
$errors[] = new Error($internalErrorMessage, $file, null, $t);
if ($internalErrorsCount >= $this->internalErrorsCountLimit) {
$reachedInternalErrorsCountLimit = true;
break;
}
}
if ($postFileCallback === null) {
continue;
}
$postFileCallback(1);
}
return new AnalyserResult(
$errors,
[],
$collectedData,
$internalErrorsCount === 0 ? $dependencies : null,
$exportedNodes,
$reachedInternalErrorsCountLimit,
memory_get_peak_usage(true),
);
}
}