diff --git a/conf/config.neon b/conf/config.neon
index 739e8247dc..3f4414b606 100644
--- a/conf/config.neon
+++ b/conf/config.neon
@@ -2108,6 +2108,7 @@ services:
arguments:
composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths%
allConfigFiles: %allConfigFiles%
+ configPhpVersion: %phpVersion%
autowired: false
# Error formatters
diff --git a/src/Diagnose/PHPStanDiagnoseExtension.php b/src/Diagnose/PHPStanDiagnoseExtension.php
index 7c3057190c..35cb6a862e 100644
--- a/src/Diagnose/PHPStanDiagnoseExtension.php
+++ b/src/Diagnose/PHPStanDiagnoseExtension.php
@@ -7,6 +7,7 @@
use PHPStan\ExtensionInstaller\GeneratedConfig;
use PHPStan\File\FileHelper;
use PHPStan\Internal\ComposerHelper;
+use PHPStan\Php\ComposerPhpVersionFactory;
use PHPStan\Php\PhpVersion;
use ReflectionClass;
use function array_key_exists;
@@ -17,6 +18,7 @@
use function explode;
use function implode;
use function in_array;
+use function is_array;
use function is_file;
use function is_readable;
use function sprintf;
@@ -29,14 +31,17 @@ final class PHPStanDiagnoseExtension implements DiagnoseExtension
{
/**
+ * @param int|array{min: int, max: int}|null $configPhpVersion
* @param string[] $composerAutoloaderProjectPaths
* @param string [] $allConfigFiles
*/
public function __construct(
private PhpVersion $phpVersion,
+ private int|array|null $configPhpVersion,
private FileHelper $fileHelper,
private array $composerAutoloaderProjectPaths,
private array $allConfigFiles,
+ private ComposerPhpVersionFactory $composerPhpVersionFactory,
)
{
}
@@ -48,11 +53,45 @@ public function print(Output $output): void
'PHP runtime version: %s',
$phpRuntimeVersion->getVersionString(),
));
- $output->writeLineFormatted(sprintf(
- 'PHP version for analysis: %s (from %s)',
- $this->phpVersion->getVersionString(),
- $this->phpVersion->getSourceLabel(),
- ));
+
+ if (
+ $this->phpVersion->getSource() === PhpVersion::SOURCE_CONFIG
+ && is_array($this->configPhpVersion)
+ ) {
+ $minVersion = new PhpVersion($this->configPhpVersion['min']);
+ $maxVersion = new PhpVersion($this->configPhpVersion['max']);
+
+ $output->writeLineFormatted(sprintf(
+ 'PHP version for analysis: %s-%s (from %s)',
+ $minVersion->getVersionString(),
+ $maxVersion->getVersionString(),
+ $this->phpVersion->getSourceLabel(),
+ ));
+
+ } else {
+ $minComposerPhpVersion = $this->composerPhpVersionFactory->getMinVersion();
+ $maxComposerPhpVersion = $this->composerPhpVersionFactory->getMaxVersion();
+ if ($minComposerPhpVersion !== null && $maxComposerPhpVersion !== null) {
+ if ($minComposerPhpVersion->getVersionId() !== $maxComposerPhpVersion->getVersionId()) {
+ $output->writeLineFormatted(sprintf(
+ 'PHP composer.json required version: %s-%s',
+ $minComposerPhpVersion->getVersionString(),
+ $maxComposerPhpVersion->getVersionString(),
+ ));
+ } else {
+ $output->writeLineFormatted(sprintf(
+ 'PHP composer.json required version: %s',
+ $minComposerPhpVersion->getVersionString(),
+ ));
+ }
+ }
+
+ $output->writeLineFormatted(sprintf(
+ 'PHP version for analysis: %s (from %s)',
+ $this->phpVersion->getVersionString(),
+ $this->phpVersion->getSourceLabel(),
+ ));
+ }
$output->writeLineFormatted('');
$output->writeLineFormatted(sprintf(
diff --git a/src/Php/PhpVersion.php b/src/Php/PhpVersion.php
index 83ca1245b5..8520f6488d 100644
--- a/src/Php/PhpVersion.php
+++ b/src/Php/PhpVersion.php
@@ -22,6 +22,14 @@ public function __construct(private int $versionId, private int $source = self::
{
}
+ /**
+ * @return self::SOURCE_*
+ */
+ public function getSource(): int
+ {
+ return $this->source;
+ }
+
public function getSourceLabel(): string
{
switch ($this->source) {