From 39ee4c3022e6105afc92a3158ccca4086af77649 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 09:33:25 +0200 Subject: [PATCH 1/6] Added regression test --- .github/workflows/e2e-tests.yml | 3 +++ e2e/composer-version-named-args/test.php | 34 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 e2e/composer-version-named-args/test.php diff --git a/.github/workflows/e2e-tests.yml b/.github/workflows/e2e-tests.yml index b143c3e446..faa4cb81a1 100644 --- a/.github/workflows/e2e-tests.yml +++ b/.github/workflows/e2e-tests.yml @@ -391,6 +391,9 @@ jobs: cd e2e/composer-version-config composer install ../../bin/phpstan analyze test.php --level=0 + - script: | + cd e2e/composer-version-named-args + ../../bin/phpstan analyze test.php --level=0 steps: - name: "Checkout" diff --git a/e2e/composer-version-named-args/test.php b/e2e/composer-version-named-args/test.php new file mode 100644 index 0000000000..97bae38991 --- /dev/null +++ b/e2e/composer-version-named-args/test.php @@ -0,0 +1,34 @@ += 80400) { + } else { + } + return [ + new Exception(previous: new Exception()), + ]; + } +} + +class HelloWorld2 +{ + /** @return mixed[] */ + public function sayHello(): array|null + { + return [ + PHP_VERSION_ID >= 80400 ? 1 : 0, + new Exception(previous: new Exception()), + ]; + } +} From 58348d3f598481fcc0d0b252a14c037fbefa0f8a Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 09:35:54 +0200 Subject: [PATCH 2/6] Update ignore-by-php-version.neon.php --- build/ignore-by-php-version.neon.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build/ignore-by-php-version.neon.php b/build/ignore-by-php-version.neon.php index c250ea9eec..3ca5aad13a 100644 --- a/build/ignore-by-php-version.neon.php +++ b/build/ignore-by-php-version.neon.php @@ -36,7 +36,7 @@ $config = []; $config['includes'] = $includes; -// overrides config.platform.php in composer.json -$config['parameters']['phpVersion'] = PHP_VERSION_ID; +// undo config.platform.php from composer.json +unset($config['parameters']['phpVersion']); return $config; From 43edf6e3c2d7cd0a5649675dfbb170ed9477bfff Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 09:44:59 +0200 Subject: [PATCH 3/6] Discard changes to build/ignore-by-php-version.neon.php --- build/ignore-by-php-version.neon.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build/ignore-by-php-version.neon.php b/build/ignore-by-php-version.neon.php index 3ca5aad13a..c250ea9eec 100644 --- a/build/ignore-by-php-version.neon.php +++ b/build/ignore-by-php-version.neon.php @@ -36,7 +36,7 @@ $config = []; $config['includes'] = $includes; -// undo config.platform.php from composer.json -unset($config['parameters']['phpVersion']); +// overrides config.platform.php in composer.json +$config['parameters']['phpVersion'] = PHP_VERSION_ID; return $config; From 5f4bcfeb12b38b95049b212f56f7d1161d047f79 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 10:38:48 +0200 Subject: [PATCH 4/6] fix --- src/Analyser/ConstantResolver.php | 4 +++- src/Analyser/MutatingScope.php | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Analyser/ConstantResolver.php b/src/Analyser/ConstantResolver.php index 846188b985..124e764367 100644 --- a/src/Analyser/ConstantResolver.php +++ b/src/Analyser/ConstantResolver.php @@ -34,6 +34,8 @@ final class ConstantResolver { + public const int PHP_MIN_VERSION_ID = 50207; + /** @var array */ private array $currentlyResolving = []; @@ -141,7 +143,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type return $this->createInteger($minRelease, $maxRelease); } if ($resolvedConstantName === 'PHP_VERSION_ID') { - $minVersion = 50207; + $minVersion = self::PHP_MIN_VERSION_ID; $maxVersion = null; if ($minPhpVersion !== null) { $minVersion = max($minVersion, $minPhpVersion->getVersionId()); diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index cdb919da1f..29e5a0b1d1 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -6235,8 +6235,10 @@ public function getIterableValueType(Type $iteratee): Type public function getPhpVersion(): PhpVersions { + $minPhpVersion = IntegerRangeType::fromInterval(ConstantResolver::PHP_MIN_VERSION_ID, null); + $constType = $this->getGlobalConstantType(new Name('PHP_VERSION_ID')); - if ($constType !== null) { + if ($constType !== null && !$minPhpVersion->isSuperTypeOf($constType)->yes()) { return new PhpVersions($constType); } From 9c2fd64969e1f930afde6a7d6548b7259121af63 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 10:41:14 +0200 Subject: [PATCH 5/6] fix --- src/Analyser/ConstantResolver.php | 2 +- src/Analyser/MutatingScope.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Analyser/ConstantResolver.php b/src/Analyser/ConstantResolver.php index 124e764367..68d948d634 100644 --- a/src/Analyser/ConstantResolver.php +++ b/src/Analyser/ConstantResolver.php @@ -34,7 +34,7 @@ final class ConstantResolver { - public const int PHP_MIN_VERSION_ID = 50207; + public const PHP_MIN_VERSION_ID = 50207; /** @var array */ private array $currentlyResolving = []; diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 29e5a0b1d1..2e952c1585 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -6238,7 +6238,7 @@ public function getPhpVersion(): PhpVersions $minPhpVersion = IntegerRangeType::fromInterval(ConstantResolver::PHP_MIN_VERSION_ID, null); $constType = $this->getGlobalConstantType(new Name('PHP_VERSION_ID')); - if ($constType !== null && !$minPhpVersion->isSuperTypeOf($constType)->yes()) { + if ($constType !== null && !$constType->isSuperTypeOf($minPhpVersion)->yes()) { return new PhpVersions($constType); } From 0cb4788ce8f3d7fe84736318862d1a7ec6e7622c Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 10:49:49 +0200 Subject: [PATCH 6/6] fix --- src/Analyser/ConstantResolver.php | 4 ++-- src/Analyser/MutatingScope.php | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Analyser/ConstantResolver.php b/src/Analyser/ConstantResolver.php index 68d948d634..52dec8da3b 100644 --- a/src/Analyser/ConstantResolver.php +++ b/src/Analyser/ConstantResolver.php @@ -34,7 +34,7 @@ final class ConstantResolver { - public const PHP_MIN_VERSION_ID = 50207; + public const PHP_MIN_ANALYZABLE_VERSION_ID = 50207; /** @var array */ private array $currentlyResolving = []; @@ -143,7 +143,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type return $this->createInteger($minRelease, $maxRelease); } if ($resolvedConstantName === 'PHP_VERSION_ID') { - $minVersion = self::PHP_MIN_VERSION_ID; + $minVersion = self::PHP_MIN_ANALYZABLE_VERSION_ID; $maxVersion = null; if ($minPhpVersion !== null) { $minVersion = max($minVersion, $minPhpVersion->getVersionId()); diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 2e952c1585..ea86730f30 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -52,6 +52,7 @@ use PHPStan\Parser\NewAssignedToPropertyVisitor; use PHPStan\Parser\Parser; use PHPStan\Php\PhpVersion; +use PHPStan\Php\PhpVersionFactory; use PHPStan\Php\PhpVersions; use PHPStan\PhpDoc\Tag\TemplateTag; use PHPStan\Reflection\Assertions; @@ -6235,10 +6236,10 @@ public function getIterableValueType(Type $iteratee): Type public function getPhpVersion(): PhpVersions { - $minPhpVersion = IntegerRangeType::fromInterval(ConstantResolver::PHP_MIN_VERSION_ID, null); + $overallPhpVersionRange = IntegerRangeType::fromInterval(ConstantResolver::PHP_MIN_ANALYZABLE_VERSION_ID, PhpVersionFactory::MAX_PHP_VERSION); $constType = $this->getGlobalConstantType(new Name('PHP_VERSION_ID')); - if ($constType !== null && !$constType->isSuperTypeOf($minPhpVersion)->yes()) { + if ($constType !== null && !$constType->equals($overallPhpVersionRange)) { return new PhpVersions($constType); }