Skip to content

Commit d55933f

Browse files
committed
DiagnoseExtension
1 parent 25658f6 commit d55933f

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

conf/config.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,10 @@ services:
20982098
php8Parser: @php8Parser
20992099
autowired: false
21002100

2101+
phpstanDiagnoseExtension:
2102+
class: PHPStan\Diagnose\PHPStanDiagnoseExtension
2103+
autowired: false
2104+
21012105
# Error formatters
21022106

21032107
-

src/Command/AnalyseCommand.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use PHPStan\Command\Symfony\SymfonyOutput;
1111
use PHPStan\Command\Symfony\SymfonyStyle;
1212
use PHPStan\DependencyInjection\Container;
13+
use PHPStan\Diagnose\DiagnoseExtension;
14+
use PHPStan\Diagnose\PHPStanDiagnoseExtension;
1315
use PHPStan\File\CouldNotWriteFileException;
1416
use PHPStan\File\FileReader;
1517
use PHPStan\File\FileWriter;
@@ -225,14 +227,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
225227
try {
226228
[$files, $onlyFiles] = $inceptionResult->getFiles();
227229
} catch (PathNotFoundException $e) {
230+
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput());
228231
$inceptionResult->getErrorOutput()->writeLineFormatted(sprintf('<error>%s</error>', $e->getMessage()));
229232
return 1;
230233
} catch (InceptionNotSuccessfulException) {
234+
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput());
231235
return 1;
232236
}
233237

234238
if (count($files) === 0) {
235239
$bleedingEdge = (bool) $container->getParameter('featureToggles')['zeroFiles'];
240+
241+
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput());
242+
236243
if (!$bleedingEdge) {
237244
$inceptionResult->getErrorOutput()->getStyle()->note('No files found to analyse.');
238245
$inceptionResult->getErrorOutput()->getStyle()->warning('This will cause a non-zero exit code in PHPStan 2.0.');
@@ -422,6 +429,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
422429
}
423430

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

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

470+
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput());
471+
462472
$errorOutput->writeLineFormatted('⚠️ Result is incomplete because of severe errors. ⚠️');
463473
$errorOutput->writeLineFormatted(' Fix these errors first and then re-run PHPStan');
464474
$errorOutput->writeLineFormatted(' to get all reported errors.');
@@ -525,6 +535,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
525535
}
526536
}
527537

538+
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput());
539+
528540
return $inceptionResult->handleReturn(
529541
$exitCode,
530542
$analysisResult->getPeakMemoryUsageBytes(),
@@ -647,4 +659,22 @@ private function runFixer(InceptionResult $inceptionResult, Container $container
647659
);
648660
}
649661

662+
private function runDiagnoseExtensions(Container $container, Output $errorOutput): void
663+
{
664+
if (!$errorOutput->isDebug()) {
665+
return;
666+
}
667+
668+
/** @var PHPStanDiagnoseExtension $phpstanDiagnoseExtension */
669+
$phpstanDiagnoseExtension = $container->getService('phpstanDiagnoseExtension');
670+
671+
// not using tag for this extension to make sure it's always first
672+
$phpstanDiagnoseExtension->print($errorOutput);
673+
674+
/** @var DiagnoseExtension $extension */
675+
foreach ($container->getServicesByTag(DiagnoseExtension::EXTENSION_TAG) as $extension) {
676+
$extension->print($errorOutput);
677+
}
678+
}
679+
650680
}

src/DependencyInjection/ConditionalTagsExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\DependencyInjection\Type\LazyDynamicThrowTypeExtensionProvider;
1212
use PHPStan\DependencyInjection\Type\LazyParameterClosureTypeExtensionProvider;
1313
use PHPStan\DependencyInjection\Type\LazyParameterOutTypeExtensionProvider;
14+
use PHPStan\Diagnose\DiagnoseExtension;
1415
use PHPStan\Parser\RichParser;
1516
use PHPStan\PhpDoc\StubFilesExtension;
1617
use PHPStan\PhpDoc\TypeNodeResolverExtension;
@@ -57,6 +58,7 @@ public function getConfigSchema(): Nette\Schema\Schema
5758
LazyParameterOutTypeExtensionProvider::FUNCTION_TAG => $bool,
5859
LazyParameterOutTypeExtensionProvider::METHOD_TAG => $bool,
5960
LazyParameterOutTypeExtensionProvider::STATIC_METHOD_TAG => $bool,
61+
DiagnoseExtension::EXTENSION_TAG => $bool,
6062
])->min(1));
6163
}
6264

src/Diagnose/DiagnoseExtension.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Diagnose;
4+
5+
use PHPStan\Command\Output;
6+
7+
/**
8+
* DiagnoseExtension can output any diagnostic information to stderr after analysis.
9+
*
10+
* PHPStan displays this information when running the "analyse" command with "-vvv" CLI option.
11+
*
12+
* To register it in the configuration file use the `phpstan.diagnoseExtension` service tag:
13+
*
14+
* ```
15+
* services:
16+
* -
17+
* class: App\PHPStan\MyExtension
18+
* tags:
19+
* - phpstan.diagnoseExtension
20+
* ```
21+
*
22+
* @api
23+
*/
24+
interface DiagnoseExtension
25+
{
26+
27+
public const EXTENSION_TAG = 'phpstan.diagnoseExtension';
28+
29+
public function print(Output $errorOutput): void;
30+
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Diagnose;
4+
5+
use PHPStan\Command\Output;
6+
use PHPStan\Internal\ComposerHelper;
7+
use PHPStan\Php\PhpVersion;
8+
use function sprintf;
9+
use const PHP_VERSION_ID;
10+
11+
class PHPStanDiagnoseExtension implements DiagnoseExtension
12+
{
13+
14+
public function print(Output $errorOutput): void
15+
{
16+
$phpRuntimeVersion = new PhpVersion(PHP_VERSION_ID);
17+
$errorOutput->writeLineFormatted(sprintf(
18+
'<info>PHP runtime version:</info> %s',
19+
$phpRuntimeVersion->getVersionString(),
20+
));
21+
$errorOutput->writeLineFormatted(sprintf(
22+
'<info>PHPStan version:</info> %s',
23+
ComposerHelper::getPhpStanVersion(),
24+
));
25+
$errorOutput->writeLineFormatted('');
26+
}
27+
28+
}

0 commit comments

Comments
 (0)