forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagnoseCommand.php
More file actions
112 lines (96 loc) · 3.23 KB
/
DiagnoseCommand.php
File metadata and controls
112 lines (96 loc) · 3.23 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
<?php declare(strict_types = 1);
namespace PHPStan\Command;
use Override;
use PHPStan\Diagnose\DiagnoseExtension;
use PHPStan\Diagnose\PHPStanDiagnoseExtension;
use PHPStan\ShouldNotHappenException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function is_string;
final class DiagnoseCommand extends Command
{
private const NAME = 'diagnose';
/**
* @param string[] $composerAutoloaderProjectPaths
*/
public function __construct(
private array $composerAutoloaderProjectPaths,
)
{
parent::__construct();
}
#[Override]
protected function configure(): void
{
$this->setName(self::NAME)
->setDescription('Shows diagnose information about PHPStan and extensions')
->setDefinition([
new InputOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Path to project configuration file'),
new InputOption(AnalyseCommand::OPTION_LEVEL, 'l', InputOption::VALUE_REQUIRED, 'Level of rule options - the higher the stricter'),
new InputOption('autoload-file', 'a', InputOption::VALUE_REQUIRED, 'Project\'s additional autoload file path'),
new InputOption('debug', mode: InputOption::VALUE_NONE, description: 'Show debug information - do not catch internal errors'),
new InputOption('memory-limit', mode: InputOption::VALUE_REQUIRED, description: 'Memory limit for clearing result cache'),
]);
}
#[Override]
protected function initialize(InputInterface $input, OutputInterface $output): void
{
if ((bool) $input->getOption('debug')) {
$application = $this->getApplication();
if ($application === null) {
throw new ShouldNotHappenException();
}
$application->setCatchExceptions(false);
return;
}
}
#[Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
$memoryLimit = $input->getOption('memory-limit');
$autoloadFile = $input->getOption('autoload-file');
$configuration = $input->getOption('configuration');
$level = $input->getOption(AnalyseCommand::OPTION_LEVEL);
if (
(!is_string($memoryLimit) && $memoryLimit !== null)
|| (!is_string($autoloadFile) && $autoloadFile !== null)
|| (!is_string($configuration) && $configuration !== null)
|| (!is_string($level) && $level !== null)
) {
throw new ShouldNotHappenException();
}
try {
$inceptionResult = CommandHelper::begin(
$input,
$output,
[],
$memoryLimit,
$autoloadFile,
$this->composerAutoloaderProjectPaths,
$configuration,
null,
$level,
false,
false,
null,
null,
false,
);
} catch (InceptionNotSuccessfulException) {
return 1;
}
$container = $inceptionResult->getContainer();
$output = $inceptionResult->getStdOutput();
/** @var PHPStanDiagnoseExtension $phpstanDiagnoseExtension */
$phpstanDiagnoseExtension = $container->getService('phpstanDiagnoseExtension');
// not using tag for this extension to make sure it's always first
$phpstanDiagnoseExtension->print($output, []);
/** @var DiagnoseExtension $extension */
foreach ($container->getServicesByTag(DiagnoseExtension::EXTENSION_TAG) as $extension) {
$extension->print($output);
}
return 0;
}
}