Repository navigation
Expand file tree
/
Copy pathRunCommand.php
More file actions
297 lines (259 loc) · 9.63 KB
/
Copy pathRunCommand.php
File metadata and controls
297 lines (259 loc) · 9.63 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
<?php declare(strict_types = 1);
namespace PHPStan\IssueBot\Console;
use Exception;
use Fidry\CpuCoreCounter\CpuCoreCounter as FidryCpuCoreCounter;
use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound;
use Nette\Neon\Neon;
use Nette\Utils\Json;
use PHPStan\IssueBot\Playground\PlaygroundCache;
use PHPStan\IssueBot\Playground\PlaygroundError;
use PHPStan\IssueBot\Playground\PlaygroundResult;
use PHPStan\IssueBot\Process\ProcessPromise;
use React\EventLoop\LoopInterface;
use React\EventLoop\StreamSelectLoop;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
use function array_key_exists;
use function array_values;
use function count;
use function escapeshellarg;
use function explode;
use function file_get_contents;
use function file_put_contents;
use function implode;
use function is_file;
use function ksort;
use function microtime;
use function mkdir;
use function React\Promise\set_rejection_handler;
use function serialize;
use function sha1;
use function sprintf;
use function str_replace;
use function strpos;
use function sys_get_temp_dir;
use function unserialize;
class RunCommand extends Command
{
public function __construct(private string $playgroundCachePath, private string $tmpDir)
{
parent::__construct();
}
protected function configure(): void
{
$this->setName('run');
$this->addArgument('phpVersion', InputArgument::REQUIRED);
$this->addArgument('playgroundHashes', InputArgument::REQUIRED);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$phpVersion = (int) $input->getArgument('phpVersion');
$commaSeparatedPlaygroundHashes = $input->getArgument('playgroundHashes');
$playgroundHashes = explode(',', $commaSeparatedPlaygroundHashes);
$playgroundCache = $this->loadPlaygroundCache();
try {
$cpuCount = (new FidryCpuCoreCounter())->getCount();
} catch (NumberOfCpuCoreNotFound) {
$cpuCount = 1;
}
$loop = new StreamSelectLoop();
$jobs = [];
foreach ($playgroundHashes as $hash) {
if (!array_key_exists($hash, $playgroundCache->getResults())) {
throw new Exception(sprintf('Hash %s must exist', $hash));
}
$jobs[] = [$phpVersion, $hash, $playgroundCache->getResults()[$hash]];
}
$allErrors = [];
set_rejection_handler(static function (Throwable $t): void {
throw $t;
});
$this->runPool($jobs, $cpuCount, function (array $job) use ($output, $loop): PromiseInterface {
[$phpVersion, $hash, $result] = $job;
return $this->analyseHash($loop, $output, $phpVersion, $result)->then(
static fn (array $errors) => [$hash, $errors],
);
})->then(static function (array $results) use (&$allErrors): void {
foreach ($results as [$hash, $errors]) {
$allErrors[$hash] = $errors;
}
});
$loop->run();
$data = ['phpVersion' => $phpVersion, 'errors' => $allErrors];
$writeSuccess = file_put_contents(
sprintf($this->tmpDir . '/results-%d-%s.tmp', $phpVersion, sha1($commaSeparatedPlaygroundHashes)),
serialize($data),
);
if ($writeSuccess === false) {
throw new Exception('Result write unsuccessful');
}
return 0;
}
/**
* @param array<array{int, string, PlaygroundResult}> $jobs
* @param callable(array{int, string, PlaygroundResult}): PromiseInterface<array{string, list<PlaygroundError>}> $jobRunner
* @return PromiseInterface<list<array{string, list<PlaygroundError>}>>
*/
private function runPool(array $jobs, int $concurrency, callable $jobRunner): PromiseInterface
{
$deferred = new Deferred();
$results = [];
$pending = 0;
$index = 0;
$total = count($jobs);
$rejected = false;
if ($total === 0) {
$deferred->resolve([]);
return $deferred->promise();
}
$runNext = static function () use (&$runNext, &$jobs, &$results, &$pending, &$index, &$rejected, $total, $concurrency, $jobRunner, $deferred): void {
if ($rejected) {
return;
}
while ($pending < $concurrency && $index < $total) {
$currentIndex = $index++;
$pending++;
$jobRunner($jobs[$currentIndex])->then(
static function ($result) use (&$results, &$pending, $currentIndex, $total, $runNext, $deferred): void {
$results[$currentIndex] = $result;
$pending--;
if (count($results) === $total) {
ksort($results);
$deferred->resolve(array_values($results));
} else {
$runNext();
}
},
static function ($error) use (&$rejected, $deferred): void {
if ($rejected) {
return;
}
$rejected = true;
$deferred->reject($error);
},
);
}
};
$runNext();
return $deferred->promise();
}
/**
* @return PromiseInterface<list<PlaygroundError>>
*/
private function analyseHash(LoopInterface $loop, OutputInterface $output, int $phpVersion, PlaygroundResult $result): PromiseInterface
{
$configFiles = [
__DIR__ . '/../../playground.neon',
__DIR__ . '/../../vendor/phpstan/phpstan-deprecation-rules/rules.neon',
];
if ($result->isBleedingEdge()) {
$configFiles[] = __DIR__ . '/../../../conf/bleedingEdge.neon';
}
if ($result->isStrictRules()) {
$configFiles[] = __DIR__ . '/../../vendor/phpstan/phpstan-strict-rules/rules.neon';
}
$tmpDir = sys_get_temp_dir() . '/phpstan-issue-bot-' . $result->getHash();
@mkdir($tmpDir, 0777, true);
$options = $result->getOptions();
$parameters = [
'level' => $result->getLevel(),
'inferPrivatePropertyTypeFromConstructor' => $options['inferPrivatePropertyTypeFromConstructor'] ?? true,
'treatPhpDocTypesAsCertain' => $result->isTreatPhpDocTypesAsCertain(),
'phpVersion' => $phpVersion,
'tmpDir' => $tmpDir,
'rememberPossiblyImpureFunctionValues' => $options['rememberPossiblyImpureFunctionValues'] ?? true,
'checkBenevolentUnionTypes' => $options['checkBenevolentUnionTypes'] ?? false,
'checkTooWideReturnTypesInProtectedAndPublicMethods' => $options['checkTooWideTypesInProtectedAndPublicMethods'] ?? false,
'checkTooWideParameterOutInProtectedAndPublicMethods' => $options['checkTooWideTypesInProtectedAndPublicMethods'] ?? false,
'checkTooWideThrowTypesInProtectedAndPublicMethods' => $options['checkTooWideTypesInProtectedAndPublicMethods'] ?? false,
'reportUnsafeArrayStringKeyCasting' => $options['reportUnsafeArrayStringKeyCasting'] ?? null,
];
$parameters['exceptions'] = [
'implicitThrows' => $options['implicitThrows'] ?? true,
'reportUncheckedExceptionDeadCatch' => $options['reportUncheckedExceptionDeadCatch'] ?? true,
'uncheckedExceptionClasses' => $options['uncheckedExceptionClasses'] ?? [],
'checkedExceptionClasses' => $options['checkedExceptionClasses'] ?? [],
'check' => [
'missingCheckedExceptionInThrows' => $options['missingCheckedExceptionInThrows'] ?? false,
'tooWideImplicitThrowType' => $options['tooWideImplicitThrowType'] ?? false,
],
];
$neon = Neon::encode([
'includes' => $configFiles,
'parameters' => $parameters,
]);
$hash = $result->getHash();
$neonPath = sprintf($this->tmpDir . '/%s.neon', $hash);
$codePath = sprintf($this->tmpDir . '/%s.php', $hash);
file_put_contents($neonPath, $neon);
file_put_contents($codePath, $result->getCode());
$commandArray = [
escapeshellarg(__DIR__ . '/../../../bin/phpstan'),
'analyse',
'--error-format',
'json',
'--no-progress',
'-c',
escapeshellarg($neonPath),
escapeshellarg($codePath),
];
$output->writeln(sprintf('Starting analysis of %s', $hash));
$process = new ProcessPromise($loop, implode(' ', $commandArray));
$startTime = microtime(true);
return $process->run()->then(static function (string $stdout) use ($hash, $output, $startTime) {
try {
$json = Json::decode($stdout, forceArrays: true);
} catch (Throwable $e) {
$output->writeln($stdout);
throw new Exception(sprintf('Failed to decode JSON for %s: %s', $hash, $e->getMessage()));
}
$errors = self::extractErrors($json, $hash);
$elapsedTime = microtime(true) - $startTime;
$output->writeln(sprintf('Analysis of %s took %.2f s', $hash, $elapsedTime));
return $errors;
});
}
/**
* @param mixed[] $json
* @return list<PlaygroundError>
*/
public static function extractErrors(array $json, string $hash): array
{
$errors = [];
foreach ($json['files'] ?? [] as $file) {
foreach ($file['messages'] as $message) {
$messageText = str_replace(sprintf('/%s.php', $hash), '/tmp.php', $message['message']);
if (strpos($messageText, 'Internal error') !== false) {
throw new Exception(sprintf('While analysing %s: %s', $hash, $messageText));
}
$errors[] = new PlaygroundError($message['line'] ?? -1, $messageText, $message['identifier'] ?? null, $message['fixable'] ?? false);
}
}
// Top-level (non-file) errors: a parallel worker crashing (e.g. out of
// memory), a bootstrap failure, etc. PHPStan reports these in $json['errors']
// with file_errors === 0, so without recording them here the analysis looks
// like it produced zero errors and any expected output silently disappears
// from the diff. Record them so they are visible.
foreach ($json['errors'] ?? [] as $genericError) {
$messageText = str_replace(sprintf('/%s.php', $hash), '/tmp.php', (string) $genericError);
$errors[] = new PlaygroundError(-1, $messageText, null);
}
return $errors;
}
private function loadPlaygroundCache(): PlaygroundCache
{
if (!is_file($this->playgroundCachePath)) {
throw new Exception('Playground cache must exist');
}
$contents = file_get_contents($this->playgroundCachePath);
if ($contents === false) {
throw new Exception('Read unsuccessful');
}
return unserialize($contents);
}
}