Skip to content

Commit

Permalink
Add diagnose command to run DiagnoseExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 10, 2024
1 parent 66a7578 commit 22c84d2
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 6 deletions.
2 changes: 2 additions & 0 deletions bin/phpstan
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use PHPStan\Command\AnalyseCommand;
use PHPStan\Command\ClearResultCacheCommand;
use PHPStan\Command\DiagnoseCommand;
use PHPStan\Command\DumpParametersCommand;
use PHPStan\Command\FixerWorkerCommand;
use PHPStan\Command\WorkerCommand;
Expand Down Expand Up @@ -166,5 +167,6 @@ use Symfony\Component\Console\Helper\ProgressBar;
$application->add(new ClearResultCacheCommand($reversedComposerAutoloaderProjectPaths));
$application->add(new FixerWorkerCommand($reversedComposerAutoloaderProjectPaths));
$application->add(new DumpParametersCommand($reversedComposerAutoloaderProjectPaths));
$application->add(new DiagnoseCommand($reversedComposerAutoloaderProjectPaths));
$application->run();
})();
104 changes: 104 additions & 0 deletions src/Command/DiagnoseCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php declare(strict_types = 1);

namespace PHPStan\Command;

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;

class DiagnoseCommand extends Command
{

private const NAME = 'diagnose';

/**
* @param string[] $composerAutoloaderProjectPaths
*/
public function __construct(
private array $composerAutoloaderProjectPaths,
)
{
parent::__construct();
}

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', null, InputOption::VALUE_NONE, 'Show debug information - do not catch internal errors'),
new InputOption('memory-limit', null, InputOption::VALUE_REQUIRED, 'Memory limit for clearing result cache'),
]);
}

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;
}
}

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,
);
} 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;
}

}
2 changes: 1 addition & 1 deletion src/Diagnose/DiagnoseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ interface DiagnoseExtension

public const EXTENSION_TAG = 'phpstan.diagnoseExtension';

public function print(Output $errorOutput): void;
public function print(Output $output): void;

}
10 changes: 5 additions & 5 deletions src/Diagnose/PHPStanDiagnoseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ public function __construct(private PhpVersion $phpVersion)
{
}

public function print(Output $errorOutput): void
public function print(Output $output): void
{
$phpRuntimeVersion = new PhpVersion(PHP_VERSION_ID);
$errorOutput->writeLineFormatted(sprintf(
$output->writeLineFormatted(sprintf(
'<info>PHP runtime version:</info> %s',
$phpRuntimeVersion->getVersionString(),
));
$errorOutput->writeLineFormatted(sprintf(
$output->writeLineFormatted(sprintf(
'<info>PHP version for analysis:</info> %s (from %s)',
$this->phpVersion->getVersionString(),
$this->phpVersion->getSourceLabel(),
));
$errorOutput->writeLineFormatted(sprintf(
$output->writeLineFormatted(sprintf(
'<info>PHPStan version:</info> %s',
ComposerHelper::getPhpStanVersion(),
));
$errorOutput->writeLineFormatted('');
$output->writeLineFormatted('');
}

}

0 comments on commit 22c84d2

Please sign in to comment.