Skip to content

Commit 5a63154

Browse files
committed
PHP version for analysis in PHPStanDiagnoseExtension
1 parent 979055f commit 5a63154

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/Diagnose/PHPStanDiagnoseExtension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@
1111
class PHPStanDiagnoseExtension implements DiagnoseExtension
1212
{
1313

14+
public function __construct(private PhpVersion $phpVersion)
15+
{
16+
}
17+
1418
public function print(Output $errorOutput): void
1519
{
1620
$phpRuntimeVersion = new PhpVersion(PHP_VERSION_ID);
1721
$errorOutput->writeLineFormatted(sprintf(
1822
'<info>PHP runtime version:</info> %s',
1923
$phpRuntimeVersion->getVersionString(),
2024
));
25+
$errorOutput->writeLineFormatted(sprintf(
26+
'<info>PHP version for analysis:</info> %s (from %s)',
27+
$this->phpVersion->getVersionString(),
28+
$this->phpVersion->getSourceLabel(),
29+
));
2130
$errorOutput->writeLineFormatted(sprintf(
2231
'<info>PHPStan version:</info> %s',
2332
ComposerHelper::getPhpStanVersion(),

src/Php/PhpVersion.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,30 @@
88
class PhpVersion
99
{
1010

11-
public function __construct(private int $versionId)
11+
public const SOURCE_RUNTIME = 1;
12+
public const SOURCE_CONFIG = 2;
13+
public const SOURCE_COMPOSER_PLATFORM_PHP = 3;
14+
public const SOURCE_UNKNOWN = 4;
15+
16+
/**
17+
* @param self::SOURCE_* $source
18+
*/
19+
public function __construct(private int $versionId, private int $source = self::SOURCE_UNKNOWN)
20+
{
21+
}
22+
23+
public function getSourceLabel(): string
1224
{
25+
switch ($this->source) {
26+
case self::SOURCE_RUNTIME:
27+
return 'runtime';
28+
case self::SOURCE_CONFIG:
29+
return 'config';
30+
case self::SOURCE_COMPOSER_PLATFORM_PHP:
31+
return 'config.platform.php in composer.json';
32+
}
33+
34+
return 'unknown';
1335
}
1436

1537
public function getVersionId(): int

src/Php/PhpVersionFactory.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,20 @@ public function __construct(
2020
public function create(): PhpVersion
2121
{
2222
$versionId = $this->versionId;
23-
if ($versionId === null && $this->composerPhpVersion !== null) {
23+
if ($versionId !== null) {
24+
$source = PhpVersion::SOURCE_CONFIG;
25+
} elseif ($this->composerPhpVersion !== null) {
2426
$parts = explode('.', $this->composerPhpVersion);
2527
$tmp = (int) $parts[0] * 10000 + (int) ($parts[1] ?? 0) * 100 + (int) ($parts[2] ?? 0);
2628
$tmp = max($tmp, 70100);
2729
$versionId = min($tmp, 80399);
28-
}
29-
30-
if ($versionId === null) {
30+
$source = PhpVersion::SOURCE_COMPOSER_PLATFORM_PHP;
31+
} else {
3132
$versionId = PHP_VERSION_ID;
33+
$source = PhpVersion::SOURCE_RUNTIME;
3234
}
3335

34-
return new PhpVersion($versionId);
36+
return new PhpVersion($versionId, $source);
3537
}
3638

3739
}

0 commit comments

Comments
 (0)