-
Notifications
You must be signed in to change notification settings - Fork 571
Expand file tree
/
Copy pathRunCommand.php
More file actions
152 lines (131 loc) · 4.62 KB
/
RunCommand.php
File metadata and controls
152 lines (131 loc) · 4.62 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
<?php declare(strict_types = 1);
namespace PHPStan\IssueBot\Console;
use Exception;
use Nette\Neon\Neon;
use Nette\Utils\Json;
use PHPStan\IssueBot\Playground\PlaygroundCache;
use PHPStan\IssueBot\Playground\PlaygroundError;
use PHPStan\IssueBot\Playground\PlaygroundResult;
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 function array_key_exists;
use function exec;
use function explode;
use function file_get_contents;
use function file_put_contents;
use function implode;
use function is_file;
use function microtime;
use function serialize;
use function sha1;
use function sprintf;
use function str_replace;
use function strpos;
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();
$errors = [];
foreach ($playgroundHashes as $hash) {
if (!array_key_exists($hash, $playgroundCache->getResults())) {
throw new Exception(sprintf('Hash %s must exist', $hash));
}
$errors[$hash] = $this->analyseHash($output, $phpVersion, $playgroundCache->getResults()[$hash]);
}
$data = ['phpVersion' => $phpVersion, 'errors' => $errors];
$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;
}
/**
* @return list<PlaygroundError>
*/
private function analyseHash(OutputInterface $output, int $phpVersion, PlaygroundResult $result): array
{
$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';
}
$neon = Neon::encode([
'includes' => $configFiles,
'parameters' => [
'level' => $result->getLevel(),
'inferPrivatePropertyTypeFromConstructor' => true,
'treatPhpDocTypesAsCertain' => $result->isTreatPhpDocTypesAsCertain(),
'phpVersion' => $phpVersion,
],
]);
$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 = [
__DIR__ . '/../../../bin/phpstan',
'analyse',
'--error-format',
'json',
'--no-progress',
'-c',
$neonPath,
$codePath,
];
$output->writeln(sprintf('Starting analysis of %s', $hash));
$startTime = microtime(true);
putenv("PHPSTAN_FNSR=1");
exec(implode(' ', $commandArray), $outputLines, $exitCode);
$elapsedTime = microtime(true) - $startTime;
$output->writeln(sprintf('Analysis of %s took %.2f s', $hash, $elapsedTime));
if ($exitCode !== 0 && $exitCode !== 1) {
throw new Exception(sprintf('PHPStan exited with code %d during analysis of %s', $exitCode, $hash));
}
$json = Json::decode(implode("\n", $outputLines), Json::FORCE_ARRAY);
$errors = [];
foreach ($json['files'] as ['messages' => $messages]) {
foreach ($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);
}
}
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);
}
}