Skip to content

Commit

Permalink
DiagnoseExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 5, 2024
1 parent 25658f6 commit d55933f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
4 changes: 4 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,10 @@ services:
php8Parser: @php8Parser
autowired: false

phpstanDiagnoseExtension:
class: PHPStan\Diagnose\PHPStanDiagnoseExtension
autowired: false

# Error formatters

-
Expand Down
30 changes: 30 additions & 0 deletions src/Command/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use PHPStan\Command\Symfony\SymfonyOutput;
use PHPStan\Command\Symfony\SymfonyStyle;
use PHPStan\DependencyInjection\Container;
use PHPStan\Diagnose\DiagnoseExtension;
use PHPStan\Diagnose\PHPStanDiagnoseExtension;
use PHPStan\File\CouldNotWriteFileException;
use PHPStan\File\FileReader;
use PHPStan\File\FileWriter;
Expand Down Expand Up @@ -225,14 +227,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
try {
[$files, $onlyFiles] = $inceptionResult->getFiles();
} catch (PathNotFoundException $e) {
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput());
$inceptionResult->getErrorOutput()->writeLineFormatted(sprintf('<error>%s</error>', $e->getMessage()));
return 1;
} catch (InceptionNotSuccessfulException) {
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput());
return 1;
}

if (count($files) === 0) {
$bleedingEdge = (bool) $container->getParameter('featureToggles')['zeroFiles'];

$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput());

if (!$bleedingEdge) {
$inceptionResult->getErrorOutput()->getStyle()->note('No files found to analyse.');
$inceptionResult->getErrorOutput()->getStyle()->warning('This will cause a non-zero exit code in PHPStan 2.0.');
Expand Down Expand Up @@ -422,6 +429,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if ($generateBaselineFile !== null) {
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput());
if (count($internalErrorsTuples) > 0) {
foreach ($internalErrorsTuples as [$internalError]) {
$inceptionResult->getStdOutput()->writeLineFormatted($internalError->getMessage());
Expand Down Expand Up @@ -459,6 +467,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$exitCode = $errorFormatter->formatErrors($analysisResult, $inceptionResult->getStdOutput());

$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput());

$errorOutput->writeLineFormatted('⚠️ Result is incomplete because of severe errors. ⚠️');
$errorOutput->writeLineFormatted(' Fix these errors first and then re-run PHPStan');
$errorOutput->writeLineFormatted(' to get all reported errors.');
Expand Down Expand Up @@ -525,6 +535,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput());

return $inceptionResult->handleReturn(
$exitCode,
$analysisResult->getPeakMemoryUsageBytes(),
Expand Down Expand Up @@ -647,4 +659,22 @@ private function runFixer(InceptionResult $inceptionResult, Container $container
);
}

private function runDiagnoseExtensions(Container $container, Output $errorOutput): void
{
if (!$errorOutput->isDebug()) {
return;
}

/** @var PHPStanDiagnoseExtension $phpstanDiagnoseExtension */
$phpstanDiagnoseExtension = $container->getService('phpstanDiagnoseExtension');

// not using tag for this extension to make sure it's always first
$phpstanDiagnoseExtension->print($errorOutput);

/** @var DiagnoseExtension $extension */
foreach ($container->getServicesByTag(DiagnoseExtension::EXTENSION_TAG) as $extension) {
$extension->print($errorOutput);
}
}

}
2 changes: 2 additions & 0 deletions src/DependencyInjection/ConditionalTagsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\DependencyInjection\Type\LazyDynamicThrowTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\LazyParameterClosureTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\LazyParameterOutTypeExtensionProvider;
use PHPStan\Diagnose\DiagnoseExtension;
use PHPStan\Parser\RichParser;
use PHPStan\PhpDoc\StubFilesExtension;
use PHPStan\PhpDoc\TypeNodeResolverExtension;
Expand Down Expand Up @@ -57,6 +58,7 @@ public function getConfigSchema(): Nette\Schema\Schema
LazyParameterOutTypeExtensionProvider::FUNCTION_TAG => $bool,
LazyParameterOutTypeExtensionProvider::METHOD_TAG => $bool,
LazyParameterOutTypeExtensionProvider::STATIC_METHOD_TAG => $bool,
DiagnoseExtension::EXTENSION_TAG => $bool,
])->min(1));
}

Expand Down
31 changes: 31 additions & 0 deletions src/Diagnose/DiagnoseExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace PHPStan\Diagnose;

use PHPStan\Command\Output;

/**
* DiagnoseExtension can output any diagnostic information to stderr after analysis.
*
* PHPStan displays this information when running the "analyse" command with "-vvv" CLI option.
*
* To register it in the configuration file use the `phpstan.diagnoseExtension` service tag:
*
* ```
* services:
* -
* class: App\PHPStan\MyExtension
* tags:
* - phpstan.diagnoseExtension
* ```
*
* @api
*/
interface DiagnoseExtension
{

public const EXTENSION_TAG = 'phpstan.diagnoseExtension';

public function print(Output $errorOutput): void;

}
28 changes: 28 additions & 0 deletions src/Diagnose/PHPStanDiagnoseExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace PHPStan\Diagnose;

use PHPStan\Command\Output;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Php\PhpVersion;
use function sprintf;
use const PHP_VERSION_ID;

class PHPStanDiagnoseExtension implements DiagnoseExtension
{

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

}

0 comments on commit d55933f

Please sign in to comment.