-
Notifications
You must be signed in to change notification settings - Fork 460
/
CommandHelper.php
583 lines (507 loc) · 23.2 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
<?php declare(strict_types = 1);
namespace PHPStan\Command;
use Composer\XdebugHandler\XdebugHandler;
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\Command\Symfony\SymfonyOutput;
use PHPStan\Command\Symfony\SymfonyStyle;
use PHPStan\DependencyInjection\Container;
use PHPStan\DependencyInjection\ContainerFactory;
use PHPStan\DependencyInjection\LoaderFactory;
use PHPStan\DependencyInjection\NeonAdapter;
use PHPStan\File\FileFinder;
use PHPStan\File\FileHelper;
use PHPStan\File\FileReader;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CommandHelper
{
public const DEFAULT_LEVEL = '0';
/**
* @param string[] $paths
* @param string[] $composerAutoloaderProjectPaths
*/
public static function begin(
InputInterface $input,
OutputInterface $output,
array $paths,
?string $pathsFile,
?string $memoryLimit,
?string $autoloadFile,
array $composerAutoloaderProjectPaths,
?string $projectConfigFile,
?string $generateBaselineFile,
?string $level,
bool $allowXdebug,
bool $manageMemoryLimitFile = true,
bool $debugEnabled = false,
?string $singleReflectionFile = null
): InceptionResult
{
if (!$allowXdebug) {
$xdebug = new XdebugHandler('phpstan', '--ansi');
$xdebug->check();
unset($xdebug);
}
$stdOutput = new SymfonyOutput($output, new SymfonyStyle(new ErrorsConsoleStyle($input, $output)));
/** @var \PHPStan\Command\Output $errorOutput */
$errorOutput = (static function () use ($input, $output): Output {
$symfonyErrorOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
return new SymfonyOutput($symfonyErrorOutput, new SymfonyStyle(new ErrorsConsoleStyle($input, $symfonyErrorOutput)));
})();
if ($memoryLimit !== null) {
if (\Nette\Utils\Strings::match($memoryLimit, '#^-?\d+[kMG]?$#i') === null) {
$errorOutput->writeLineFormatted(sprintf('Invalid memory limit format "%s".', $memoryLimit));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
if (ini_set('memory_limit', $memoryLimit) === false) {
$errorOutput->writeLineFormatted(sprintf('Memory limit "%s" cannot be set.', $memoryLimit));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
}
$currentWorkingDirectory = getcwd();
if ($currentWorkingDirectory === false) {
throw new \PHPStan\ShouldNotHappenException();
}
$currentWorkingDirectoryFileHelper = new FileHelper($currentWorkingDirectory);
$currentWorkingDirectory = $currentWorkingDirectoryFileHelper->getWorkingDirectory();
if ($autoloadFile !== null) {
$autoloadFile = $currentWorkingDirectoryFileHelper->absolutizePath($autoloadFile);
if (!is_file($autoloadFile)) {
$errorOutput->writeLineFormatted(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->writeLineFormatted(sprintf('Note: Using configuration file %s.', $projectConfigFile));
break;
}
}
} else {
$projectConfigFile = $currentWorkingDirectoryFileHelper->absolutizePath($projectConfigFile);
}
if ($generateBaselineFile !== null) {
$generateBaselineFile = $currentWorkingDirectoryFileHelper->normalizePath($currentWorkingDirectoryFileHelper->absolutizePath($generateBaselineFile));
}
$defaultLevelUsed = false;
if ($projectConfigFile === null && $level === null) {
$level = self::DEFAULT_LEVEL;
$defaultLevelUsed = true;
}
$paths = array_map(static function (string $path) use ($currentWorkingDirectoryFileHelper): string {
return $currentWorkingDirectoryFileHelper->normalizePath($currentWorkingDirectoryFileHelper->absolutizePath($path));
}, $paths);
if (count($paths) === 0 && $pathsFile !== null) {
$pathsFile = $currentWorkingDirectoryFileHelper->absolutizePath($pathsFile);
if (!file_exists($pathsFile)) {
$errorOutput->writeLineFormatted(sprintf('Paths file %s does not exist.', $pathsFile));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
try {
$pathsString = FileReader::read($pathsFile);
} catch (\PHPStan\File\CouldNotReadFileException $e) {
$errorOutput->writeLineFormatted($e->getMessage());
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$paths = array_values(array_filter(explode("\n", $pathsString), static function (string $path): bool {
return trim($path) !== '';
}));
$pathsFileFileHelper = new FileHelper(dirname($pathsFile));
$paths = array_map(static function (string $path) use ($pathsFileFileHelper): string {
return $pathsFileFileHelper->normalizePath($pathsFileFileHelper->absolutizePath($path));
}, $paths);
}
$analysedPathsFromConfig = [];
$containerFactory = new ContainerFactory($currentWorkingDirectory);
$projectConfig = null;
if ($projectConfigFile !== null) {
if (!is_file($projectConfigFile)) {
$errorOutput->writeLineFormatted(sprintf('Project config file at path %s does not exist.', $projectConfigFile));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$loader = (new LoaderFactory(
$currentWorkingDirectoryFileHelper,
$containerFactory->getRootDirectory(),
$containerFactory->getCurrentWorkingDirectory(),
$generateBaselineFile
))->createLoader();
try {
$projectConfig = $loader->load($projectConfigFile, null);
} catch (\Nette\InvalidStateException | \Nette\FileNotFoundException $e) {
$errorOutput->writeLineFormatted($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 = (string) $projectConfig['parameters']['level'];
}
if (count($paths) === 0 && isset($projectConfig['parameters']['paths'])) {
$analysedPathsFromConfig = Helpers::expand($projectConfig['parameters']['paths'], $defaultParameters);
$paths = $analysedPathsFromConfig;
}
}
$additionalConfigFiles = [];
if ($level !== null) {
$levelConfigFile = sprintf('%s/config.level%s.neon', $containerFactory->getConfigDirectory(), $level);
if (!is_file($levelConfigFile)) {
$errorOutput->writeLineFormatted(sprintf('Level config file %s was not found.', $levelConfigFile));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$additionalConfigFiles[] = $levelConfigFile;
}
if (class_exists('PHPStan\ExtensionInstaller\GeneratedConfig')) {
$generatedConfigReflection = new \ReflectionClass('PHPStan\ExtensionInstaller\GeneratedConfig');
$generatedConfigDirectory = dirname($generatedConfigReflection->getFileName());
foreach (\PHPStan\ExtensionInstaller\GeneratedConfig::EXTENSIONS as $name => $extensionConfig) {
foreach ($extensionConfig['extra']['includes'] ?? [] as $includedFile) {
if (!is_string($includedFile)) {
$errorOutput->writeLineFormatted(sprintf('Cannot include config from package %s, expecting string file path but got %s', $name, gettype($includedFile)));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$includedFilePath = null;
if (isset($extensionConfig['relative_install_path'])) {
$includedFilePath = sprintf('%s/%s/%s', $generatedConfigDirectory, $extensionConfig['relative_install_path'], $includedFile);
if (!file_exists($includedFilePath) || !is_readable($includedFilePath)) {
$includedFilePath = null;
}
}
if ($includedFilePath === null) {
$includedFilePath = sprintf('%s/%s', $extensionConfig['install_path'], $includedFile);
}
if (!file_exists($includedFilePath) || !is_readable($includedFilePath)) {
$errorOutput->writeLineFormatted(sprintf('Config file %s does not exist or isn\'t readable', $includedFilePath));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$additionalConfigFiles[] = $includedFilePath;
}
}
}
if ($projectConfigFile !== null) {
$additionalConfigFiles[] = $projectConfigFile;
}
$loaderParameters = [
'rootDir' => $containerFactory->getRootDirectory(),
'currentWorkingDirectory' => $containerFactory->getCurrentWorkingDirectory(),
];
self::detectDuplicateIncludedFiles(
$errorOutput,
$currentWorkingDirectoryFileHelper,
$additionalConfigFiles,
$loaderParameters
);
$createDir = static function (string $path) use ($errorOutput): void {
if (!@mkdir($path, 0777) && !is_dir($path)) {
$errorOutput->writeLineFormatted(sprintf('Cannot create a temp directory %s', $path));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
};
if (!isset($tmpDir)) {
$tmpDir = sys_get_temp_dir() . '/phpstan';
$createDir($tmpDir);
}
try {
$container = $containerFactory->create($tmpDir, $additionalConfigFiles, $paths, $composerAutoloaderProjectPaths, $analysedPathsFromConfig, $level ?? self::DEFAULT_LEVEL, $generateBaselineFile, $autoloadFile, $singleReflectionFile);
} catch (\Nette\DI\InvalidConfigurationException | \Nette\Utils\AssertionException $e) {
$errorOutput->writeLineFormatted('<error>Invalid configuration:</error>');
$errorOutput->writeLineFormatted($e->getMessage());
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$containerFactory->clearOldContainers($tmpDir);
if (count($paths) === 0) {
$errorOutput->writeLineFormatted('At least one path must be specified to analyse.');
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$memoryLimitFile = $container->getParameter('memoryLimitFile');
if ($manageMemoryLimitFile && file_exists($memoryLimitFile)) {
$memoryLimitFileContents = FileReader::read($memoryLimitFile);
$errorOutput->writeLineFormatted('PHPStan crashed in the previous run probably because of excessive memory consumption.');
$errorOutput->writeLineFormatted(sprintf('It consumed around %s of memory.', $memoryLimitFileContents));
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted('To avoid this issue, allow to use more memory with the --memory-limit option.');
@unlink($memoryLimitFile);
}
self::setUpSignalHandler($errorOutput, $manageMemoryLimitFile ? $memoryLimitFile : null);
if (!$container->hasParameter('customRulesetUsed')) {
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted('<comment>No rules detected</comment>');
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted('You have the following choices:');
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted('* while running the analyse option, use the <info>--level</info> option to adjust your rule level - the higher the stricter');
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted(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</>.', $currentWorkingDirectoryFileHelper->normalizePath(__DIR__ . '/../../conf')));
$errorOutput->writeLineFormatted(' * in this case, don\'t forget to define parameter <options=bold>customRulesetUsed</> in your config file.');
$errorOutput->writeLineFormatted('');
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->writeLineFormatted('<error>Invalid configuration:</error>');
$errorOutput->writeLineFormatted($message);
}
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$autoloadFiles = $container->getParameter('autoload_files');
if ($manageMemoryLimitFile && count($autoloadFiles) > 0) {
$errorOutput->writeLineFormatted('⚠️ You\'re using a deprecated config option <fg=cyan>autoload_files</>. ⚠️️');
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted('You might not need it anymore - try removing it from your');
$errorOutput->writeLineFormatted('configuration file and run PHPStan again.');
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted('If the analysis fails, there are now two distinct options');
$errorOutput->writeLineFormatted('to choose from to replace <fg=cyan>autoload_files</>:');
$errorOutput->writeLineFormatted('1) <fg=cyan>scanFiles</> - PHPStan will scan those for classes and functions');
$errorOutput->writeLineFormatted(' definitions. PHPStan will not execute those files.');
$errorOutput->writeLineFormatted('2) <fg=cyan>bootstrapFiles</> - PHPStan will execute these files to prepare');
$errorOutput->writeLineFormatted(' the PHP runtime environment for the analysis.');
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted('Read more about this in PHPStan\'s documentation:');
$errorOutput->writeLineFormatted('https://phpstan.org/user-guide/discovering-symbols');
$errorOutput->writeLineFormatted('');
}
$autoloadDirectories = $container->getParameter('autoload_directories');
if (count($autoloadDirectories) > 0 && $manageMemoryLimitFile) {
$errorOutput->writeLineFormatted('⚠️ You\'re using a deprecated config option <fg=cyan>autoload_directories</>. ⚠️️');
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted('You might not need it anymore - try removing it from your');
$errorOutput->writeLineFormatted('configuration file and run PHPStan again.');
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted('If the analysis fails, replace it with <fg=cyan>scanDirectories</>.');
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted('Read more about this in PHPStan\'s documentation:');
$errorOutput->writeLineFormatted('https://phpstan.org/user-guide/discovering-symbols');
$errorOutput->writeLineFormatted('');
}
foreach ($autoloadFiles as $parameterAutoloadFile) {
if (!file_exists($parameterAutoloadFile)) {
$errorOutput->writeLineFormatted(sprintf('Autoload file %s does not exist.', $parameterAutoloadFile));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
(static function (string $file) use ($container): void {
require_once $file;
})($parameterAutoloadFile);
}
$bootstrapFile = $container->getParameter('bootstrap');
if ($bootstrapFile !== null) {
if ($manageMemoryLimitFile) {
$errorOutput->writeLineFormatted('⚠️ You\'re using a deprecated config option <fg=cyan>bootstrap</>. ⚠️️');
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted('This option has been replaced with <fg=cyan>bootstrapFiles</> which accepts a list of files');
$errorOutput->writeLineFormatted('to execute before the analysis.');
$errorOutput->writeLineFormatted('');
}
self::executeBootstrapFile($bootstrapFile, $container, $errorOutput, $debugEnabled);
}
foreach ($container->getParameter('bootstrapFiles') as $bootstrapFileFromArray) {
self::executeBootstrapFile($bootstrapFileFromArray, $container, $errorOutput, $debugEnabled);
}
foreach ($container->getParameter('scanFiles') as $scannedFile) {
if (is_file($scannedFile)) {
continue;
}
$errorOutput->writeLineFormatted(sprintf('Scanned file %s does not exist.', $scannedFile));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
foreach ($container->getParameter('scanDirectories') as $scannedDirectory) {
if (is_dir($scannedDirectory)) {
continue;
}
$errorOutput->writeLineFormatted(sprintf('Scanned directory %s does not exist.', $scannedDirectory));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$alreadyAddedStubFiles = [];
foreach ($container->getParameter('stubFiles') as $stubFile) {
if (
$container->getParameter('featureToggles')['detectDuplicateStubFiles']
&& array_key_exists($stubFile, $alreadyAddedStubFiles)
) {
$errorOutput->writeLineFormatted(sprintf('Stub file %s is added multiple times.', $stubFile));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$alreadyAddedStubFiles[$stubFile] = true;
if (is_file($stubFile)) {
continue;
}
$errorOutput->writeLineFormatted(sprintf('Stub file %s does not exist.', $stubFile));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$excludesAnalyse = $container->getParameter('excludes_analyse');
$excludePaths = $container->getParameter('excludePaths');
if (count($excludesAnalyse) > 0 && $excludePaths !== null) {
$errorOutput->writeLineFormatted(sprintf('Configuration parameters <fg=cyan>excludes_analyse</> and <fg=cyan>excludePaths</> cannot be used at the same time.'));
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted(sprintf('Parameter <fg=cyan>excludes_analyse</> has been deprecated so use <fg=cyan>excludePaths</> only from now on.'));
$errorOutput->writeLineFormatted('');
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
$tempResultCachePath = $container->getParameter('tempResultCachePath');
$createDir($tempResultCachePath);
/** @var FileFinder $fileFinder */
$fileFinder = $container->getService('fileFinderAnalyse');
/** @var \Closure(): (array{string[], bool}) $filesCallback */
$filesCallback = static function () use ($fileFinder, $paths): array {
$fileFinderResult = $fileFinder->findFiles($paths);
return [$fileFinderResult->getFiles(), $fileFinderResult->isOnlyFiles()];
};
return new InceptionResult(
$filesCallback,
$stdOutput,
$errorOutput,
$container,
$defaultLevelUsed,
$memoryLimitFile,
$projectConfigFile,
$projectConfig,
$generateBaselineFile
);
}
private static function executeBootstrapFile(
string $file,
Container $container,
Output $errorOutput,
bool $debugEnabled
): void
{
if (!is_file($file)) {
$errorOutput->writeLineFormatted(sprintf('Bootstrap file %s does not exist.', $file));
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
try {
(static function (string $file) use ($container): void {
require_once $file;
})($file);
} catch (\Throwable $e) {
$errorOutput->writeLineFormatted(sprintf('%s thrown in %s on line %d while loading bootstrap file %s: %s', get_class($e), $e->getFile(), $e->getLine(), $file, $e->getMessage()));
if ($debugEnabled) {
$errorOutput->writeLineFormatted($e->getTraceAsString());
}
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}
}
private static function setUpSignalHandler(Output $output, ?string $memoryLimitFile): void
{
if (!function_exists('pcntl_signal')) {
return;
}
pcntl_async_signals(true);
pcntl_signal(SIGINT, static function () use ($output, $memoryLimitFile): void {
if ($memoryLimitFile !== null && file_exists($memoryLimitFile)) {
@unlink($memoryLimitFile);
}
$output->writeLineFormatted('');
exit(1);
});
}
/**
* @param \PHPStan\Command\Output $output
* @param \PHPStan\File\FileHelper $fileHelper
* @param string[] $configFiles
* @param array<string, string> $loaderParameters
* @throws \PHPStan\Command\InceptionNotSuccessfulException
*/
private static function detectDuplicateIncludedFiles(
Output $output,
FileHelper $fileHelper,
array $configFiles,
array $loaderParameters
): void
{
$neonAdapter = new NeonAdapter();
$phpAdapter = new PhpAdapter();
$allConfigFiles = [];
foreach ($configFiles as $configFile) {
$allConfigFiles = array_merge($allConfigFiles, self::getConfigFiles($fileHelper, $neonAdapter, $phpAdapter, $configFile, $loaderParameters, null));
}
$normalized = array_map(static function (string $file) use ($fileHelper): string {
return $fileHelper->normalizePath($file);
}, $allConfigFiles);
$deduplicated = array_unique($normalized);
if (count($normalized) <= count($deduplicated)) {
return;
}
$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->writeLineFormatted(sprintf($format, implode("\n- ", $duplicateFiles)));
if (class_exists('PHPStan\ExtensionInstaller\GeneratedConfig')) {
$output->writeLineFormatted('');
$output->writeLineFormatted('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();
}
/**
* @param \PHPStan\DependencyInjection\NeonAdapter $neonAdapter
* @param \Nette\DI\Config\Adapters\PhpAdapter $phpAdapter
* @param string $configFile
* @param array<string, string> $loaderParameters
* @param string|null $generateBaselineFile
* @return string[]
*/
private static function getConfigFiles(
FileHelper $fileHelper,
NeonAdapter $neonAdapter,
PhpAdapter $phpAdapter,
string $configFile,
array $loaderParameters,
?string $generateBaselineFile
): array
{
if ($generateBaselineFile === $fileHelper->normalizePath($configFile)) {
return [];
}
if (!is_file($configFile) || !is_readable($configFile)) {
return [];
}
if (Strings::endsWith($configFile, '.php')) {
$data = $phpAdapter->load($configFile);
} else {
$data = $neonAdapter->load($configFile);
}
$allConfigFiles = [$configFile];
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 = array_merge($allConfigFiles, self::getConfigFiles($fileHelper, $neonAdapter, $phpAdapter, $include, $loaderParameters, $generateBaselineFile));
}
}
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;
}
}