Skip to content

Fix "Named arguments are supported only on PHP 8.0 and later." false positive #4032

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 6 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
3 changes: 3 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
34 changes: 34 additions & 0 deletions e2e/composer-version-named-args/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace NamedAttributesPhpversion;

use Exception;
use function PHPStan\debugScope;
use function PHPStan\dumpType;
use function PHPStan\Testing\assertType;

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()),
];
}
}
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;
Copy link
Contributor Author

@staabm staabm May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this constant might need a better name. couldn't come up with something better.


/** @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
5 changes: 4 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 @@ -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);
}

Expand Down
Loading