Repository navigation
Expand file tree
/
Copy pathFixerWorkerRunner.php
More file actions
379 lines (341 loc) · 12.6 KB
/
Copy pathFixerWorkerRunner.php
File metadata and controls
379 lines (341 loc) · 12.6 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php declare(strict_types = 1);
namespace PHPStan\Command;
use Clue\React\NDJson\Encoder;
use PHPStan\Analyser\AnalyserResult;
use PHPStan\Analyser\AnalyserResultFinalizer;
use PHPStan\Analyser\Error;
use PHPStan\Analyser\Ignore\IgnoredErrorHelper;
use PHPStan\Analyser\Ignore\IgnoredErrorHelperResult;
use PHPStan\Analyser\InternalError;
use PHPStan\Analyser\ResultCache\ResultCacheManager;
use PHPStan\Analyser\ResultCache\ResultCacheManagerFactory;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Parallel\ParallelAnalyser;
use PHPStan\Parallel\Scheduler;
use PHPStan\Process\CpuCoreCounter;
use PHPStan\ShouldNotHappenException;
use React\EventLoop\LoopInterface;
use React\EventLoop\StreamSelectLoop;
use React\Promise\PromiseInterface;
use React\Socket\ConnectionInterface;
use React\Socket\TcpConnector;
use Symfony\Component\Console\Input\InputInterface;
use function array_diff;
use function array_key_exists;
use function count;
use function filemtime;
use function filesize;
use function in_array;
use function is_file;
use function memory_get_peak_usage;
use function React\Promise\resolve;
use function sprintf;
use function usort;
use const JSON_INVALID_UTF8_IGNORE;
/**
* The PHPStan Pro worker logic that runs *after* the application boot.
*
* Extracted from FixerWorkerCommand so it can be reused without re-booting: a
* pcntl_fork()-ed child (see ForkedProcessPromise) inherits the already-booted
* DI container and calls run() directly, while FixerWorkerCommand still calls
* it after the expensive CommandHelper::begin() boot of a freshly spawned
* process.
*
* It connects back to FixerApplication's TCP server, restores the result
* cache, analyses the changed files and streams the results.
*/
#[AutowiredService]
final class FixerWorkerRunner
{
public function __construct(
private IgnoredErrorHelper $ignoredErrorHelper,
private ResultCacheManagerFactory $resultCacheManagerFactory,
private AnalyserResultFinalizer $analyserResultFinalizer,
private ParallelAnalyser $parallelAnalyser,
private Scheduler $scheduler,
private CpuCoreCounter $cpuCoreCounter,
)
{
}
/**
* @param string[] $inceptionFiles
* @param mixed[]|null $projectConfigArray
*/
public function run(
Output $errorOutput,
array $inceptionFiles,
bool $isOnlyFiles,
?array $projectConfigArray,
?string $configuration,
int $serverPort,
InputInterface $input,
): int
{
$ignoredErrorHelperResult = $this->ignoredErrorHelper->initialize();
if (count($ignoredErrorHelperResult->getErrors()) > 0) {
throw new ShouldNotHappenException();
}
// Always a fresh event loop: in a forked child the parent's inherited
// loop must never be touched.
$loop = new StreamSelectLoop();
$tcpConnector = new TcpConnector($loop);
$tcpConnector->connect(sprintf('127.0.0.1:%d', $serverPort))->then(function (ConnectionInterface $connection) use ($errorOutput, $inceptionFiles, $isOnlyFiles, $projectConfigArray, $configuration, $input, $ignoredErrorHelperResult, $loop): void {
// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
$jsonInvalidUtf8Ignore = defined('JSON_INVALID_UTF8_IGNORE') ? JSON_INVALID_UTF8_IGNORE : 0;
// phpcs:enable
$out = new Encoder($connection, $jsonInvalidUtf8Ignore);
//$in = new Decoder($connection, true, 512, $jsonInvalidUtf8Ignore, 128 * 1024 * 1024);
/** @var ResultCacheManager $resultCacheManager */
$resultCacheManager = $this->resultCacheManagerFactory->create([]);
$out->write([
'action' => 'analysisStart',
'result' => [
'analysedFiles' => $inceptionFiles,
],
]);
$resultCache = $resultCacheManager->restore($inceptionFiles, false, false, $projectConfigArray, $errorOutput);
$errorsFromResultCacheTmp = $resultCache->getErrors();
$locallyIgnoredErrorsFromResultCacheTmp = $resultCache->getLocallyIgnoredErrors();
foreach ($resultCache->getFilesToAnalyse() as $fileToAnalyse) {
unset($errorsFromResultCacheTmp[$fileToAnalyse]);
unset($locallyIgnoredErrorsFromResultCacheTmp[$fileToAnalyse]);
}
$errorsFromResultCache = [];
foreach ($errorsFromResultCacheTmp as $errorsByFile) {
foreach ($errorsByFile as $error) {
$errorsFromResultCache[] = $error;
}
}
[$errorsFromResultCache, $ignoredErrorsFromResultCache] = $this->filterErrors($errorsFromResultCache, $ignoredErrorHelperResult, $isOnlyFiles, $inceptionFiles, false);
foreach ($locallyIgnoredErrorsFromResultCacheTmp as $locallyIgnoredErrors) {
foreach ($locallyIgnoredErrors as $locallyIgnoredError) {
$ignoredErrorsFromResultCache[] = [$locallyIgnoredError, null];
}
}
$out->write([
'action' => 'analysisStream',
'result' => [
'errors' => $errorsFromResultCache,
'ignoredErrors' => $ignoredErrorsFromResultCache,
'analysedFiles' => array_diff($inceptionFiles, $resultCache->getFilesToAnalyse()),
],
]);
$filesToAnalyse = $resultCache->getFilesToAnalyse();
usort($filesToAnalyse, static function (string $a, string $b): int {
$aTime = @filemtime($a);
if ($aTime === false) {
return 1;
}
$bTime = @filemtime($b);
if ($bTime === false) {
return -1;
}
// files are sorted from the oldest
// because ParallelAnalyser reverses the scheduler jobs to do the smallest
// jobs first
return $aTime <=> $bTime;
});
$this->runAnalyser(
$loop,
$filesToAnalyse,
$inceptionFiles,
$configuration,
$input,
function (array $errors, array $locallyIgnoredErrors, array $analysedFiles) use ($out, $ignoredErrorHelperResult, $isOnlyFiles, $inceptionFiles): void {
$internalErrors = [];
foreach ($errors as $fileSpecificError) {
if (!$fileSpecificError->hasNonIgnorableException()) {
continue;
}
$internalErrors[] = $this->transformErrorIntoInternalError($fileSpecificError);
}
if (count($internalErrors) > 0) {
$out->write(['action' => 'analysisCrash', 'data' => [
'internalErrors' => $internalErrors,
]]);
return;
}
[$errors, $ignoredErrors] = $this->filterErrors($errors, $ignoredErrorHelperResult, $isOnlyFiles, $inceptionFiles, false);
foreach ($locallyIgnoredErrors as $locallyIgnoredError) {
$ignoredErrors[] = [$locallyIgnoredError, null];
}
$out->write([
'action' => 'analysisStream',
'result' => [
'errors' => $errors,
'ignoredErrors' => $ignoredErrors,
'analysedFiles' => $analysedFiles,
],
]);
},
)->then(function (AnalyserResult $intermediateAnalyserResult) use ($resultCacheManager, $resultCache, $errorOutput, $isOnlyFiles, $ignoredErrorHelperResult, $inceptionFiles, $out): void {
$resultCacheResult = $resultCacheManager->process(
$intermediateAnalyserResult,
$resultCache,
$errorOutput,
false,
true,
);
$finalizerResult = $this->analyserResultFinalizer->finalize($resultCacheResult->getAnalyserResult(), $isOnlyFiles, false, null, null, $resultCache->getCollectedDataRuleResults());
$resultCacheResult->save($finalizerResult);
$internalErrors = [];
foreach ($finalizerResult->getAnalyserResult()->getInternalErrors() as $internalError) {
$internalErrors[] = new InternalError(
$internalError->getTraceAsString() !== null ? sprintf('Internal error: %s', $internalError->getMessage()) : $internalError->getMessage(),
$internalError->getContextDescription(),
$internalError->getTrace(),
$internalError->getTraceAsString(),
$internalError->shouldReportBug(),
);
}
foreach ($finalizerResult->getAnalyserResult()->getUnorderedErrors() as $fileSpecificError) {
if (!$fileSpecificError->hasNonIgnorableException()) {
continue;
}
$internalErrors[] = $this->transformErrorIntoInternalError($fileSpecificError);
}
$hasInternalErrors = count($internalErrors) > 0 || $finalizerResult->getAnalyserResult()->hasReachedInternalErrorsCountLimit();
if ($hasInternalErrors) {
$out->write(['action' => 'analysisCrash', 'data' => [
'internalErrors' => count($internalErrors) > 0 ? $internalErrors : [
new InternalError(
'Internal error occurred',
'running analyser in PHPStan Pro worker',
trace: [],
traceAsString: null,
shouldReportBug: false,
),
],
]]);
}
[$collectorErrors, $ignoredCollectorErrors] = $this->filterErrors($finalizerResult->getCollectorErrors(), $ignoredErrorHelperResult, $isOnlyFiles, $inceptionFiles, $hasInternalErrors);
foreach ($finalizerResult->getLocallyIgnoredCollectorErrors() as $locallyIgnoredCollectorError) {
$ignoredCollectorErrors[] = [$locallyIgnoredCollectorError, null];
}
$out->write([
'action' => 'analysisStream',
'result' => [
'errors' => $collectorErrors,
'ignoredErrors' => $ignoredCollectorErrors,
'analysedFiles' => [],
],
]);
$ignoredErrorHelperProcessedResult = $ignoredErrorHelperResult->process(
$finalizerResult->getErrors(),
$isOnlyFiles,
$inceptionFiles,
$hasInternalErrors,
);
$ignoreFileErrors = [];
foreach ($ignoredErrorHelperProcessedResult->getNotIgnoredErrors() as $error) {
if ($error->getIdentifier() === null) {
continue;
}
if (!in_array($error->getIdentifier(), ['ignore.count', 'ignore.unmatched', 'ignore.unmatchedLine', 'ignore.unmatchedIdentifier', 'ignore.noComment'], true)) {
continue;
}
$ignoreFileErrors[] = $error;
}
$out->end([
'action' => 'analysisEnd',
'result' => [
'ignoreFileErrors' => $ignoreFileErrors,
'ignoreNotFileErrors' => $ignoredErrorHelperProcessedResult->getOtherIgnoreMessages(),
],
]);
});
});
$loop->run();
return 0;
}
private function transformErrorIntoInternalError(Error $error): InternalError
{
$message = $error->getMessage();
$metadata = $error->getMetadata();
if (
$error->getIdentifier() === 'phpstan.internal'
&& array_key_exists(InternalError::STACK_TRACE_AS_STRING_METADATA_KEY, $metadata)
) {
$message = sprintf('Internal error: %s', $message);
}
return new InternalError(
$message,
sprintf('analysing file %s', $error->getTraitFilePath() ?? $error->getFilePath()),
$metadata[InternalError::STACK_TRACE_METADATA_KEY] ?? [],
$metadata[InternalError::STACK_TRACE_AS_STRING_METADATA_KEY] ?? null,
shouldReportBug: true,
);
}
/**
* @param string[] $inceptionFiles
* @param list<Error> $errors
* @return array{list<Error>, list<array{Error, mixed[]|string}>}
*/
private function filterErrors(array $errors, IgnoredErrorHelperResult $ignoredErrorHelperResult, bool $onlyFiles, array $inceptionFiles, bool $hasInternalErrors): array
{
$ignoredErrorHelperProcessedResult = $ignoredErrorHelperResult->process($errors, $onlyFiles, $inceptionFiles, $hasInternalErrors);
$finalErrors = [];
foreach ($ignoredErrorHelperProcessedResult->getNotIgnoredErrors() as $error) {
if ($error->getIdentifier() === null) {
$finalErrors[] = $error;
continue;
}
if (in_array($error->getIdentifier(), ['ignore.count', 'ignore.unmatched'], true)) {
continue;
}
$finalErrors[] = $error;
}
return [
$finalErrors,
$ignoredErrorHelperProcessedResult->getIgnoredErrors(),
];
}
/**
* @param string[] $files
* @param string[] $allAnalysedFiles
* @param callable(list<Error>, list<Error>, string[]): void $onFileAnalysisHandler
* @return PromiseInterface<AnalyserResult>
*/
private function runAnalyser(LoopInterface $loop, array $files, array $allAnalysedFiles, ?string $configuration, InputInterface $input, callable $onFileAnalysisHandler): PromiseInterface
{
$filesCount = count($files);
if ($filesCount === 0) {
return resolve(new AnalyserResult(
unorderedErrors: [],
filteredPhpErrors: [],
allPhpErrors: [],
locallyIgnoredErrors: [],
linesToIgnore: [],
unmatchedLineIgnores: [],
internalErrors: [],
collectedData: [],
dependencies: [],
usedTraitDependencies: [],
valueDependencies: ['values' => [], 'dependents' => []],
packageDependencies: [],
exportedNodes: [],
reachedInternalErrorsCountLimit: false,
peakMemoryUsageBytes: memory_get_peak_usage(true),
processedFiles: [],
));
}
$schedule = $this->scheduler->scheduleWork($this->cpuCoreCounter->getNumberOfCpuCores(), $files, static fn (string $file): int => (int) @filesize($file));
$mainScript = null;
if (isset($_SERVER['argv'][0]) && is_file($_SERVER['argv'][0])) {
$mainScript = $_SERVER['argv'][0];
}
return $this->parallelAnalyser->analyse(
$loop,
$schedule,
$allAnalysedFiles,
$mainScript,
null,
$configuration,
null,
null,
$input,
$onFileAnalysisHandler,
null,
);
}
}