-
-
Notifications
You must be signed in to change notification settings - Fork 862
/
CommandHelper.php
404 lines (357 loc) · 14.8 KB
/
CommandHelper.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php declare(strict_types = 1);
namespace PHPStan\Command;
use Nette\DI\Config\Adapters\PhpAdapter;
use Nette\DI\Helpers;
use Nette\Schema\Context as SchemaContext;
use Nette\Schema\Processor;
use Nette\Utils\Strings;
use Nette\Utils\Validators;
use PHPStan\DependencyInjection\ContainerFactory;
use PHPStan\DependencyInjection\LoaderFactory;
use PHPStan\DependencyInjection\NeonAdapter;
use PHPStan\File\FileFinder;
use PHPStan\File\FileHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\OutputStyle;
class CommandHelper
{
public const DEFAULT_LEVEL = 0;
public static function begin(
InputInterface $input,
OutputInterface $output,
array $paths,
?string $pathsFile,
?string $memoryLimit,
?string $autoloadFile,
?string $projectConfigFile,
?string $level
): InceptionResult
{
$errorOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
$consoleStyle = new ErrorsConsoleStyle($input, $output);
if ($memoryLimit !== null) {
if (\Nette\Utils\Strings::match($memoryLimit, '#^-?\d+[kMG]?$#i') === null) {
$errorOutput->writeln(sprintf('Invalid memory limit format "%s".', $memoryLimit));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
if (ini_set('memory_limit', $memoryLimit) === false) {
$errorOutput->writeln(sprintf('Memory limit "%s" cannot be set.', $memoryLimit));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
}
$currentWorkingDirectory = getcwd();
if ($currentWorkingDirectory === false) {
throw new \PHPStan\ShouldNotHappenException();
}
$fileHelper = new FileHelper($currentWorkingDirectory);
if ($autoloadFile !== null) {
if (!is_file($autoloadFile)) {
$errorOutput->writeln(sprintf('Autoload file "%s" not found.', $autoloadFile));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
(static function (string $file): void {
require_once $file;
})($autoloadFile);
}
if ($projectConfigFile === null) {
foreach (['phpstan.neon', 'phpstan.neon.dist'] as $discoverableConfigName) {
$discoverableConfigFile = $currentWorkingDirectory . DIRECTORY_SEPARATOR . $discoverableConfigName;
if (is_file($discoverableConfigFile)) {
$projectConfigFile = $discoverableConfigFile;
$errorOutput->writeln(sprintf('Note: Using configuration file %s.', $projectConfigFile));
break;
}
}
}
$defaultLevelUsed = false;
if ($projectConfigFile === null && $level === null) {
$level = self::DEFAULT_LEVEL;
$defaultLevelUsed = true;
}
if (count($paths) === 0 && $pathsFile !== null) {
if (!file_exists($pathsFile)) {
$errorOutput->writeln(sprintf('Paths file %s does not exist.', $pathsFile));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$pathsString = file_get_contents($pathsFile);
if ($pathsString === false) {
throw new \PHPStan\ShouldNotHappenException();
}
$paths = array_values(array_filter(explode("\n", $pathsString), static function (string $path): bool {
return trim($path) !== '';
}));
}
$containerFactory = new ContainerFactory($currentWorkingDirectory);
if ($projectConfigFile !== null) {
if (!is_file($projectConfigFile)) {
$errorOutput->writeln(sprintf('Project config file at path %s does not exist.', $projectConfigFile));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$loader = (new LoaderFactory(
$containerFactory->getRootDirectory(),
$containerFactory->getCurrentWorkingDirectory()
))->createLoader();
try {
$projectConfig = $loader->load($projectConfigFile, null);
} catch (\Nette\InvalidStateException | \Nette\FileNotFoundException $e) {
$errorOutput->writeln($e->getMessage());
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$defaultParameters = [
'rootDir' => $containerFactory->getRootDirectory(),
'currentWorkingDirectory' => $containerFactory->getCurrentWorkingDirectory(),
];
if (isset($projectConfig['parameters']['tmpDir'])) {
$tmpDir = Helpers::expand($projectConfig['parameters']['tmpDir'], $defaultParameters);
}
if ($level === null && isset($projectConfig['parameters']['level'])) {
$level = $projectConfig['parameters']['level'];
}
if (count($paths) === 0 && isset($projectConfig['parameters']['paths'])) {
$paths = Helpers::expand($projectConfig['parameters']['paths'], $defaultParameters);
}
}
$additionalConfigFiles = [];
if ($level !== null) {
$levelConfigFile = sprintf('%s/config.level%s.neon', $containerFactory->getConfigDirectory(), $level);
if (!is_file($levelConfigFile)) {
$errorOutput->writeln(sprintf('Level config file %s was not found.', $levelConfigFile));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$additionalConfigFiles[] = $levelConfigFile;
}
if (class_exists('PHPStan\ExtensionInstaller\GeneratedConfig')) {
foreach (\PHPStan\ExtensionInstaller\GeneratedConfig::EXTENSIONS as $name => $extensionConfig) {
foreach ($extensionConfig['extra']['includes'] ?? [] as $includedFile) {
if (!is_string($includedFile)) {
$errorOutput->writeln(sprintf('Cannot include config from package %s, expecting string file path but got %s', $name, gettype($includedFile)));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$includedFilePath = sprintf('%s/%s', $extensionConfig['install_path'], $includedFile);
if (!file_exists($includedFilePath) || !is_readable($includedFilePath)) {
$errorOutput->writeln(sprintf('Config file %s does not exists or isn\'t readable', $includedFilePath));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$additionalConfigFiles[] = $includedFilePath;
}
}
}
if ($projectConfigFile !== null) {
$additionalConfigFiles[] = $fileHelper->absolutizePath($projectConfigFile);
}
self::detectDuplicateIncludedFiles(
$errorOutput,
$fileHelper,
$additionalConfigFiles,
[
'rootDir' => $containerFactory->getRootDirectory(),
'currentWorkingDirectory' => $containerFactory->getCurrentWorkingDirectory(),
]
);
if (!isset($tmpDir)) {
$tmpDir = sys_get_temp_dir() . '/phpstan';
if (!@mkdir($tmpDir, 0777, true) && !is_dir($tmpDir)) {
$errorOutput->writeln(sprintf('Cannot create a temp directory %s', $tmpDir));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
}
$paths = array_map(static function (string $path) use ($fileHelper): string {
return $fileHelper->absolutizePath($path);
}, $paths);
try {
$container = $containerFactory->create($tmpDir, $additionalConfigFiles, $paths);
} catch (\Nette\DI\InvalidConfigurationException | \Nette\Utils\AssertionException $e) {
$errorOutput->writeln('<error>Invalid configuration:</error>');
$errorOutput->writeln($e->getMessage());
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
if (count($paths) === 0) {
$errorOutput->writeln('At least one path must be specified to analyse.');
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$memoryLimitFile = $container->getParameter('memoryLimitFile');
if (file_exists($memoryLimitFile)) {
$memoryLimitFileContents = file_get_contents($memoryLimitFile);
if ($memoryLimitFileContents === false) {
throw new \PHPStan\ShouldNotHappenException();
}
$errorOutput->writeln('PHPStan crashed in the previous run probably because of excessive memory consumption.');
$errorOutput->writeln(sprintf('It consumed around %s of memory.', $memoryLimitFileContents));
$errorOutput->writeln('');
$errorOutput->writeln('');
$errorOutput->writeln('To avoid this issue, allow to use more memory with the --memory-limit option.');
@unlink($memoryLimitFile);
}
self::setUpSignalHandler($consoleStyle, $memoryLimitFile);
if ($container->getParameter('customRulesetUsed') === null) {
$errorOutput->writeln('');
$errorOutput->writeln('<comment>No rules detected</comment>');
$errorOutput->writeln('');
$errorOutput->writeln('You have the following choices:');
$errorOutput->writeln('');
$errorOutput->writeln('* while running the analyse option, use the <info>--level</info> option to adjust your rule level - the higher the stricter');
$errorOutput->writeln('');
$errorOutput->writeln(sprintf('* create your own <info>custom ruleset</info> by selecting which rules you want to check by copying the service definitions from the built-in config level files in <options=bold>%s</>.', $fileHelper->normalizePath(__DIR__ . '/../../conf')));
$errorOutput->writeln(' * in this case, don\'t forget to define parameter <options=bold>customRulesetUsed</> in your config file.');
$errorOutput->writeln('');
throw new \PHPStan\Command\InceptionNotSuccessfulException();
} elseif ((bool) $container->getParameter('customRulesetUsed')) {
$defaultLevelUsed = false;
}
$schema = $container->getParameter('__parametersSchema');
$processor = new Processor();
$processor->onNewContext[] = static function (SchemaContext $context): void {
$context->path = ['parameters'];
};
try {
$processor->process($schema, $container->getParameters());
} catch (\Nette\Schema\ValidationException $e) {
foreach ($e->getMessages() as $message) {
$errorOutput->writeln('<error>Invalid configuration:</error>');
$errorOutput->writeln($message);
}
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
foreach ($container->getParameter('autoload_files') as $parameterAutoloadFile) {
if (!file_exists($parameterAutoloadFile)) {
$errorOutput->writeln(sprintf('Autoload file %s does not exist.', $parameterAutoloadFile));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
(static function (string $file) use ($container): void {
require_once $file;
})($parameterAutoloadFile);
}
$autoloadDirectories = $container->getParameter('autoload_directories');
if (count($autoloadDirectories) > 0) {
$robotLoader = new \Nette\Loaders\RobotLoader();
$robotLoader->acceptFiles = array_map(static function (string $extension): string {
return sprintf('*.%s', $extension);
}, $container->getParameter('fileExtensions'));
$robotLoader->setTempDirectory($tmpDir);
foreach ($autoloadDirectories as $directory) {
if (!file_exists($directory)) {
$errorOutput->writeln(sprintf('Autoload directory %s does not exist.', $directory));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$robotLoader->addDirectory($directory);
}
foreach ($container->getParameter('excludes_analyse') as $directory) {
$robotLoader->excludeDirectory($directory);
}
$robotLoader->register();
}
$bootstrapFile = $container->getParameter('bootstrap');
if ($bootstrapFile !== null) {
if (!is_file($bootstrapFile)) {
$errorOutput->writeln(sprintf('Bootstrap file %s does not exist.', $bootstrapFile));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
try {
(static function (string $file) use ($container): void {
require_once $file;
})($bootstrapFile);
} catch (\Throwable $e) {
$errorOutput->writeln($e->getMessage());
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
}
/** @var FileFinder $fileFinder */
$fileFinder = $container->getByType(FileFinder::class);
try {
$fileFinderResult = $fileFinder->findFiles($paths);
} catch (\PHPStan\File\PathNotFoundException $e) {
$errorOutput->writeln(sprintf('<error>%s</error>', $e->getMessage()));
throw new \PHPStan\Command\InceptionNotSuccessfulException($e->getMessage(), 0, $e);
}
return new InceptionResult(
$fileFinderResult->getFiles(),
$fileFinderResult->isOnlyFiles(),
$consoleStyle,
$errorOutput,
$container,
$defaultLevelUsed,
$memoryLimitFile,
$projectConfigFile
);
}
private static function setUpSignalHandler(OutputStyle $consoleStyle, string $memoryLimitFile): void
{
if (!function_exists('pcntl_signal')) {
return;
}
pcntl_async_signals(true);
pcntl_signal(SIGINT, static function () use ($consoleStyle, $memoryLimitFile): void {
if (file_exists($memoryLimitFile)) {
@unlink($memoryLimitFile);
}
$consoleStyle->newLine();
exit(1);
});
}
private static function detectDuplicateIncludedFiles(
OutputInterface $output,
FileHelper $fileHelper,
array $configFiles,
array $loaderParameters
): void
{
$neonAdapter = new NeonAdapter();
$phpAdapter = new PhpAdapter();
$allConfigFiles = $configFiles;
foreach ($configFiles as $configFile) {
$allConfigFiles = array_merge($allConfigFiles, self::getConfigFiles($neonAdapter, $phpAdapter, $configFile, $loaderParameters));
}
$normalized = array_map(static function (string $file) use ($fileHelper): string {
return $fileHelper->absolutizePath($fileHelper->normalizePath($file));
}, $allConfigFiles);
$deduplicated = array_unique($normalized);
if (count($normalized) > count($deduplicated)) {
$duplicateFiles = array_unique(array_diff_key($normalized, $deduplicated));
$format = "<error>These files are included multiple times:</error>\n- %s";
if (count($duplicateFiles) === 1) {
$format = "<error>This file is included multiple times:</error>\n- %s";
}
$output->writeln(sprintf($format, implode("\n- ", $duplicateFiles)));
if (class_exists('PHPStan\ExtensionInstaller\GeneratedConfig')) {
$output->writeln('');
$output->writeln('It can lead to unexpected results. If you\'re using phpstan/extension-installer, make sure you have removed corresponding neon files from your project config file.');
}
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
}
private static function getConfigFiles(
NeonAdapter $neonAdapter,
PhpAdapter $phpAdapter,
string $configFile,
array $loaderParameters
): array
{
if (!is_file($configFile) || !is_readable($configFile)) {
return [];
}
if (Strings::endsWith($configFile, '.php')) {
$data = $phpAdapter->load($configFile);
} else {
$data = $neonAdapter->load($configFile);
}
$allConfigFiles = [];
if (isset($data['includes'])) {
Validators::assert($data['includes'], 'list', sprintf("section 'includes' in file '%s'", $configFile));
$includes = Helpers::expand($data['includes'], $loaderParameters);
foreach ($includes as $include) {
$include = self::expandIncludedFile($include, $configFile);
$allConfigFiles[] = $include;
$allConfigFiles = array_merge($allConfigFiles, self::getConfigFiles($neonAdapter, $phpAdapter, $include, $loaderParameters));
}
}
return $allConfigFiles;
}
private static function expandIncludedFile(string $includedFile, string $mainFile): string
{
return Strings::match($includedFile, '#([a-z]+:)?[/\\\\]#Ai') !== null // is absolute
? $includedFile
: dirname($mainFile) . '/' . $includedFile;
}
}