Skip to content

Improved fix "Named arguments are supported only on PHP 8.0 and later" false positive" #4033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Analyser/ConstantResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
final class ConstantResolver
{

public const PHP_MIN_ANALYZABLE_VERSION_ID = 50207;

/** @var array<string, true> */
private array $currentlyResolving = [];

Expand Down Expand Up @@ -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());
Expand Down
13 changes: 12 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -6236,7 +6237,17 @@ public function getIterableValueType(Type $iteratee): Type
public function getPhpVersion(): PhpVersions
{
$constType = $this->getGlobalConstantType(new Name('PHP_VERSION_ID'));
if ($constType !== null) {

$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);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'], []);
}

}
57 changes: 57 additions & 0 deletions tests/PHPStan/Rules/Classes/data/named-arguments-phpversion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace NamedArgumentsPhpversion;

use Exception;

class HelloWorld
{
/** @return mixed[] */
public function sayHello(): array|null
{
if(PHP_VERSION_ID >= 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()),
];
}
}

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()),
];
}
}
Loading