|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Command; |
| 4 | + |
| 5 | +use PHPStan\Diagnose\DiagnoseExtension; |
| 6 | +use PHPStan\Diagnose\PHPStanDiagnoseExtension; |
| 7 | +use PHPStan\ShouldNotHappenException; |
| 8 | +use Symfony\Component\Console\Command\Command; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Input\InputOption; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | +use function is_string; |
| 13 | + |
| 14 | +class DiagnoseCommand extends Command |
| 15 | +{ |
| 16 | + |
| 17 | + private const NAME = 'diagnose'; |
| 18 | + |
| 19 | + /** |
| 20 | + * @param string[] $composerAutoloaderProjectPaths |
| 21 | + */ |
| 22 | + public function __construct( |
| 23 | + private array $composerAutoloaderProjectPaths, |
| 24 | + ) |
| 25 | + { |
| 26 | + parent::__construct(); |
| 27 | + } |
| 28 | + |
| 29 | + protected function configure(): void |
| 30 | + { |
| 31 | + $this->setName(self::NAME) |
| 32 | + ->setDescription('Shows diagnose information about PHPStan and extensions') |
| 33 | + ->setDefinition([ |
| 34 | + new InputOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Path to project configuration file'), |
| 35 | + new InputOption(AnalyseCommand::OPTION_LEVEL, 'l', InputOption::VALUE_REQUIRED, 'Level of rule options - the higher the stricter'), |
| 36 | + new InputOption('autoload-file', 'a', InputOption::VALUE_REQUIRED, 'Project\'s additional autoload file path'), |
| 37 | + new InputOption('debug', null, InputOption::VALUE_NONE, 'Show debug information - do not catch internal errors'), |
| 38 | + new InputOption('memory-limit', null, InputOption::VALUE_REQUIRED, 'Memory limit for clearing result cache'), |
| 39 | + ]); |
| 40 | + } |
| 41 | + |
| 42 | + protected function initialize(InputInterface $input, OutputInterface $output): void |
| 43 | + { |
| 44 | + if ((bool) $input->getOption('debug')) { |
| 45 | + $application = $this->getApplication(); |
| 46 | + if ($application === null) { |
| 47 | + throw new ShouldNotHappenException(); |
| 48 | + } |
| 49 | + $application->setCatchExceptions(false); |
| 50 | + return; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 55 | + { |
| 56 | + $memoryLimit = $input->getOption('memory-limit'); |
| 57 | + $autoloadFile = $input->getOption('autoload-file'); |
| 58 | + $configuration = $input->getOption('configuration'); |
| 59 | + $level = $input->getOption(AnalyseCommand::OPTION_LEVEL); |
| 60 | + |
| 61 | + if ( |
| 62 | + (!is_string($memoryLimit) && $memoryLimit !== null) |
| 63 | + || (!is_string($autoloadFile) && $autoloadFile !== null) |
| 64 | + || (!is_string($configuration) && $configuration !== null) |
| 65 | + || (!is_string($level) && $level !== null) |
| 66 | + ) { |
| 67 | + throw new ShouldNotHappenException(); |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + $inceptionResult = CommandHelper::begin( |
| 72 | + $input, |
| 73 | + $output, |
| 74 | + [], |
| 75 | + $memoryLimit, |
| 76 | + $autoloadFile, |
| 77 | + $this->composerAutoloaderProjectPaths, |
| 78 | + $configuration, |
| 79 | + null, |
| 80 | + $level, |
| 81 | + false, |
| 82 | + ); |
| 83 | + } catch (InceptionNotSuccessfulException) { |
| 84 | + return 1; |
| 85 | + } |
| 86 | + |
| 87 | + $container = $inceptionResult->getContainer(); |
| 88 | + $output = $inceptionResult->getStdOutput(); |
| 89 | + |
| 90 | + /** @var PHPStanDiagnoseExtension $phpstanDiagnoseExtension */ |
| 91 | + $phpstanDiagnoseExtension = $container->getService('phpstanDiagnoseExtension'); |
| 92 | + |
| 93 | + // not using tag for this extension to make sure it's always first |
| 94 | + $phpstanDiagnoseExtension->print($output); |
| 95 | + |
| 96 | + /** @var DiagnoseExtension $extension */ |
| 97 | + foreach ($container->getServicesByTag(DiagnoseExtension::EXTENSION_TAG) as $extension) { |
| 98 | + $extension->print($output); |
| 99 | + } |
| 100 | + |
| 101 | + return 0; |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments