forked from rectorphp/rector-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkerRunner.php
More file actions
182 lines (151 loc) · 6.69 KB
/
WorkerRunner.php
File metadata and controls
182 lines (151 loc) · 6.69 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
<?php
declare(strict_types=1);
namespace Rector\Parallel;
use Clue\React\NDJson\Decoder;
use Clue\React\NDJson\Encoder;
use Nette\Utils\FileSystem;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Core\Application\ApplicationFileProcessor;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesProcessor;
use Rector\Core\Console\Style\RectorConsoleOutputStyle;
use Rector\Core\Contract\Processor\FileProcessorInterface;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\StaticReflection\DynamicSourceLocatorDecorator;
use Rector\Core\Util\ArrayParametersMerger;
use Rector\Core\ValueObject\Application\File;
use Rector\Core\ValueObject\Configuration;
use Rector\Core\ValueObject\Error\SystemError;
use Rector\Core\ValueObject\Reporting\FileDiff;
use Rector\Parallel\ValueObject\Bridge;
use Symplify\EasyParallel\Enum\Action;
use Symplify\EasyParallel\Enum\ReactCommand;
use Symplify\EasyParallel\Enum\ReactEvent;
use Throwable;
final class WorkerRunner
{
/**
* @var string
*/
private const RESULT = 'result';
/**
* @param FileProcessorInterface[] $fileProcessors
*/
public function __construct(
private readonly ArrayParametersMerger $arrayParametersMerger,
private readonly CurrentFileProvider $currentFileProvider,
private readonly DynamicSourceLocatorDecorator $dynamicSourceLocatorDecorator,
private readonly RectorConsoleOutputStyle $rectorConsoleOutputStyle,
private readonly RemovedAndAddedFilesProcessor $removedAndAddedFilesProcessor,
private readonly ApplicationFileProcessor $applicationFileProcessor,
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly array $fileProcessors = [],
) {
}
public function run(Encoder $encoder, Decoder $decoder, Configuration $configuration): void
{
$this->dynamicSourceLocatorDecorator->addPaths($configuration->getPaths());
// 1. handle system error
$handleErrorCallback = static function (Throwable $throwable) use ($encoder): void {
$systemErrors = new SystemError($throwable->getMessage(), $throwable->getFile(), $throwable->getLine());
$encoder->write([
ReactCommand::ACTION => Action::RESULT,
self::RESULT => [
Bridge::SYSTEM_ERRORS => [$systemErrors],
Bridge::FILES_COUNT => 0,
Bridge::SYSTEM_ERRORS_COUNT => 1,
],
]);
$encoder->end();
};
$encoder->on(ReactEvent::ERROR, $handleErrorCallback);
// 2. collect diffs + errors from file processor
$decoder->on(ReactEvent::DATA, function (array $json) use ($encoder, $configuration): void {
$action = $json[ReactCommand::ACTION];
if ($action !== Action::MAIN) {
return;
}
$systemErrorsCount = 0;
/** @var string[] $filePaths */
$filePaths = $json[Bridge::FILES] ?? [];
$errorAndFileDiffs = [];
$systemErrors = [];
// 1. allow PHPStan to work with static reflection on provided files
$this->applicationFileProcessor->configurePHPStanNodeScopeResolver($filePaths, $configuration);
foreach ($filePaths as $filePath) {
$file = null;
try {
$file = new File($filePath, FileSystem::read($filePath));
$this->currentFileProvider->setFile($file);
$errorAndFileDiffs = $this->processFile($file, $configuration, $errorAndFileDiffs);
if ($errorAndFileDiffs[Bridge::SYSTEM_ERRORS] !== []) {
$this->invalidateFile($file);
} else {
$this->changedFilesDetector->cacheFileWithDependencies($file->getFilePath());
}
} catch (Throwable $throwable) {
++$systemErrorsCount;
$systemErrors = $this->collectSystemErrors($systemErrors, $throwable, $filePath);
$this->invalidateFile($file);
}
}
$this->removedAndAddedFilesProcessor->run($configuration);
/**
* this invokes all listeners listening $decoder->on(...) @see \Symplify\EasyParallel\Enum\ReactEvent::DATA
*/
$encoder->write([
ReactCommand::ACTION => Action::RESULT,
self::RESULT => [
Bridge::FILE_DIFFS => $errorAndFileDiffs[Bridge::FILE_DIFFS] ?? [],
Bridge::FILES_COUNT => count($filePaths),
Bridge::SYSTEM_ERRORS => $systemErrors,
Bridge::SYSTEM_ERRORS_COUNT => $systemErrorsCount,
],
]);
});
$decoder->on(ReactEvent::ERROR, $handleErrorCallback);
}
/**
* @param array{system_errors: SystemError[], file_diffs: FileDiff[]}|mixed[] $errorAndFileDiffs
* @return array{system_errors: SystemError[], file_diffs: FileDiff[]}
*/
private function processFile(File $file, Configuration $configuration, array $errorAndFileDiffs): array
{
foreach ($this->fileProcessors as $fileProcessor) {
if (! $fileProcessor->supports($file, $configuration)) {
continue;
}
$currentErrorsAndFileDiffs = $fileProcessor->process($file, $configuration);
$errorAndFileDiffs = $this->arrayParametersMerger->merge(
$errorAndFileDiffs,
$currentErrorsAndFileDiffs
);
}
return $errorAndFileDiffs;
}
/**
* @param SystemError[] $systemErrors
* @return SystemError[]
*/
private function collectSystemErrors(array $systemErrors, Throwable $throwable, string $filePath): array
{
$errorMessage = sprintf('System error: "%s"', $throwable->getMessage()) . PHP_EOL;
if ($this->rectorConsoleOutputStyle->isDebug()) {
$systemErrors[] = new SystemError(
$errorMessage . PHP_EOL . 'Stack trace:' . PHP_EOL . $throwable->getTraceAsString(),
$filePath,
$throwable->getLine()
);
return $systemErrors;
}
$errorMessage .= 'Run Rector with "--debug" option and post the report here: https://github.com/rectorphp/rector/issues/new';
$systemErrors[] = new SystemError($errorMessage, $filePath, $throwable->getLine());
return $systemErrors;
}
private function invalidateFile(?File $file): void
{
if (! $file instanceof File) {
return;
}
$this->changedFilesDetector->invalidateFile($file->getFilePath());
}
}