From e6d83e4142c7aa1b2c6cced9aebb25e2700d89f9 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 11:37:30 +0200 Subject: [PATCH 1/9] Reapply "Fix "Named arguments are supported only on PHP 8.0 and later." false positive" This reverts commit 1c565b54a216b4b3b15a40bb9e385eed2a042de1. --- .github/workflows/e2e-tests.yml | 3 +++ e2e/composer-version-named-args/test.php | 34 ++++++++++++++++++++++++ src/Analyser/ConstantResolver.php | 4 ++- src/Analyser/MutatingScope.php | 5 +++- 4 files changed, 44 insertions(+), 2 deletions(-) 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()), + ]; + } +} diff --git a/src/Analyser/ConstantResolver.php b/src/Analyser/ConstantResolver.php index 846188b985..52dec8da3b 100644 --- a/src/Analyser/ConstantResolver.php +++ b/src/Analyser/ConstantResolver.php @@ -34,6 +34,8 @@ final class ConstantResolver { + public const PHP_MIN_ANALYZABLE_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_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 cdb919da1f..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,8 +6236,10 @@ public function getIterableValueType(Type $iteratee): Type public function getPhpVersion(): PhpVersions { + $overallPhpVersionRange = IntegerRangeType::fromInterval(ConstantResolver::PHP_MIN_ANALYZABLE_VERSION_ID, PhpVersionFactory::MAX_PHP_VERSION); + $constType = $this->getGlobalConstantType(new Name('PHP_VERSION_ID')); - if ($constType !== null) { + if ($constType !== null && !$constType->equals($overallPhpVersionRange)) { return new PhpVersions($constType); } From b7c631889a58d99f29c59293037a2725d595ee03 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 11:37:57 +0200 Subject: [PATCH 2/9] new fix --- src/Analyser/MutatingScope.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index ea86730f30..051d15d960 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -6236,10 +6236,18 @@ public function getIterableValueType(Type $iteratee): Type public function getPhpVersion(): PhpVersions { - $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->equals($overallPhpVersionRange)) { + + $isOverallPhpVersionRange = false; + if ( + $constType instanceof IntegerRangeType + && $constType->getMin() === ConstantResolver::PHP_MIN_ANALYZABLE_VERSION_ID + && ($constType->getMax() === null || $constType->getMax() === PhpVersionFactory::MAX_PHP_VERSION) + ) { + $isOverallPhpVersionRange = true; + } + + if ($constType !== null && !$isOverallPhpVersionRange) { return new PhpVersions($constType); } From 43abe4bcd27549070e71e4bcf8c13a6751f08fa5 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 11:42:58 +0200 Subject: [PATCH 3/9] disable fix to verify regression test works --- src/Analyser/MutatingScope.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 051d15d960..4374599f4d 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -6244,7 +6244,7 @@ public function getPhpVersion(): PhpVersions && $constType->getMin() === ConstantResolver::PHP_MIN_ANALYZABLE_VERSION_ID && ($constType->getMax() === null || $constType->getMax() === PhpVersionFactory::MAX_PHP_VERSION) ) { - $isOverallPhpVersionRange = true; + //$isOverallPhpVersionRange = true; } if ($constType !== null && !$isOverallPhpVersionRange) { From b33397020bd5548b787b7f571a562070d54975d3 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 11:47:50 +0200 Subject: [PATCH 4/9] better test --- .../Rules/Classes/InstantiationRuleTest.php | 9 ++++++ .../data/named-arguments-phpversion.php | 31 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/PHPStan/Rules/Classes/data/named-arguments-phpversion.php diff --git a/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php b/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php index 1a84e5e170..0c361880e7 100644 --- a/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php +++ b/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php @@ -586,4 +586,13 @@ public function testBug12951(): void ]); } + public function testNamedArgumentsPhpversion(): void + { + if (PHP_VERSION_ID < 80000) { + self::markTestSkipped('Test requires PHP 8.0'); + } + + $this->analyse([__DIR__ . '/data/named-arguments-phpversion.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Classes/data/named-arguments-phpversion.php b/tests/PHPStan/Rules/Classes/data/named-arguments-phpversion.php new file mode 100644 index 0000000000..24f4d2c85c --- /dev/null +++ b/tests/PHPStan/Rules/Classes/data/named-arguments-phpversion.php @@ -0,0 +1,31 @@ += 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 42e11a647d873194940e50f50dfd8ca4810f9235 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 11:47:56 +0200 Subject: [PATCH 5/9] Revert "disable fix to verify regression test works" This reverts commit 43abe4bcd27549070e71e4bcf8c13a6751f08fa5. --- src/Analyser/MutatingScope.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 4374599f4d..051d15d960 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -6244,7 +6244,7 @@ public function getPhpVersion(): PhpVersions && $constType->getMin() === ConstantResolver::PHP_MIN_ANALYZABLE_VERSION_ID && ($constType->getMax() === null || $constType->getMax() === PhpVersionFactory::MAX_PHP_VERSION) ) { - //$isOverallPhpVersionRange = true; + $isOverallPhpVersionRange = true; } if ($constType !== null && !$isOverallPhpVersionRange) { From 7abe804ff8a1ebec36dd2ae7c5a5b5ffe99b8036 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 11:48:26 +0200 Subject: [PATCH 6/9] Discard changes to .github/workflows/e2e-tests.yml --- .github/workflows/e2e-tests.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/e2e-tests.yml b/.github/workflows/e2e-tests.yml index faa4cb81a1..b143c3e446 100644 --- a/.github/workflows/e2e-tests.yml +++ b/.github/workflows/e2e-tests.yml @@ -391,9 +391,6 @@ 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" From 41621c28adb43e1282d9742deab5a313d9a0cbb5 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 11:48:37 +0200 Subject: [PATCH 7/9] Delete e2e/composer-version-named-args/test.php --- e2e/composer-version-named-args/test.php | 34 ------------------------ 1 file changed, 34 deletions(-) delete mode 100644 e2e/composer-version-named-args/test.php diff --git a/e2e/composer-version-named-args/test.php b/e2e/composer-version-named-args/test.php deleted file mode 100644 index 97bae38991..0000000000 --- a/e2e/composer-version-named-args/test.php +++ /dev/null @@ -1,34 +0,0 @@ -= 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 a769ebe2840df6a6cd832663845292017ca27cd4 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 11:51:03 +0200 Subject: [PATCH 8/9] fix build --- .../PHPStan/Rules/Classes/data/named-arguments-phpversion.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/PHPStan/Rules/Classes/data/named-arguments-phpversion.php b/tests/PHPStan/Rules/Classes/data/named-arguments-phpversion.php index 24f4d2c85c..4dc1f86a9a 100644 --- a/tests/PHPStan/Rules/Classes/data/named-arguments-phpversion.php +++ b/tests/PHPStan/Rules/Classes/data/named-arguments-phpversion.php @@ -1,4 +1,6 @@ -= 8.0 + +declare(strict_types = 1); namespace NamedArgumentsPhpversion; From 41019bad343fd3bec8fe70ea4eac64f7a443dc34 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 28 May 2025 12:27:00 +0200 Subject: [PATCH 9/9] more tests --- .../data/named-arguments-phpversion.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/PHPStan/Rules/Classes/data/named-arguments-phpversion.php b/tests/PHPStan/Rules/Classes/data/named-arguments-phpversion.php index 4dc1f86a9a..dbba869398 100644 --- a/tests/PHPStan/Rules/Classes/data/named-arguments-phpversion.php +++ b/tests/PHPStan/Rules/Classes/data/named-arguments-phpversion.php @@ -31,3 +31,27 @@ public function sayHello(): array|null ]; } } + +class HelloWorld3 +{ + /** @return mixed[] */ + public function sayHello(): array|null + { + return [ + PHP_VERSION_ID >= 70400 ? 1 : 0, + new Exception(previous: new Exception()), + ]; + } +} + +class HelloWorld4 +{ + /** @return mixed[] */ + public function sayHello(): array|null + { + return [ + PHP_VERSION_ID < 80000 ? 1 : 0, + new Exception(previous: new Exception()), + ]; + } +}