Skip to content

Commit 682b2b5

Browse files
authored
Improved fix "Named arguments are supported only on PHP 8.0 and later" false positive
1 parent d30498a commit 682b2b5

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

src/Analyser/ConstantResolver.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
final class ConstantResolver
3535
{
3636

37+
public const PHP_MIN_ANALYZABLE_VERSION_ID = 50207;
38+
3739
/** @var array<string, true> */
3840
private array $currentlyResolving = [];
3941

@@ -141,7 +143,7 @@ public function resolvePredefinedConstant(string $resolvedConstantName): ?Type
141143
return $this->createInteger($minRelease, $maxRelease);
142144
}
143145
if ($resolvedConstantName === 'PHP_VERSION_ID') {
144-
$minVersion = 50207;
146+
$minVersion = self::PHP_MIN_ANALYZABLE_VERSION_ID;
145147
$maxVersion = null;
146148
if ($minPhpVersion !== null) {
147149
$minVersion = max($minVersion, $minPhpVersion->getVersionId());

src/Analyser/MutatingScope.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use PHPStan\Parser\NewAssignedToPropertyVisitor;
5353
use PHPStan\Parser\Parser;
5454
use PHPStan\Php\PhpVersion;
55+
use PHPStan\Php\PhpVersionFactory;
5556
use PHPStan\Php\PhpVersions;
5657
use PHPStan\PhpDoc\Tag\TemplateTag;
5758
use PHPStan\Reflection\Assertions;
@@ -6236,7 +6237,17 @@ public function getIterableValueType(Type $iteratee): Type
62366237
public function getPhpVersion(): PhpVersions
62376238
{
62386239
$constType = $this->getGlobalConstantType(new Name('PHP_VERSION_ID'));
6239-
if ($constType !== null) {
6240+
6241+
$isOverallPhpVersionRange = false;
6242+
if (
6243+
$constType instanceof IntegerRangeType
6244+
&& $constType->getMin() === ConstantResolver::PHP_MIN_ANALYZABLE_VERSION_ID
6245+
&& ($constType->getMax() === null || $constType->getMax() === PhpVersionFactory::MAX_PHP_VERSION)
6246+
) {
6247+
$isOverallPhpVersionRange = true;
6248+
}
6249+
6250+
if ($constType !== null && !$isOverallPhpVersionRange) {
62406251
return new PhpVersions($constType);
62416252
}
62426253

tests/PHPStan/Rules/Classes/InstantiationRuleTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,4 +586,13 @@ public function testBug12951(): void
586586
]);
587587
}
588588

589+
public function testNamedArgumentsPhpversion(): void
590+
{
591+
if (PHP_VERSION_ID < 80000) {
592+
self::markTestSkipped('Test requires PHP 8.0');
593+
}
594+
595+
$this->analyse([__DIR__ . '/data/named-arguments-phpversion.php'], []);
596+
}
597+
589598
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace NamedArgumentsPhpversion;
6+
7+
use Exception;
8+
9+
class HelloWorld
10+
{
11+
/** @return mixed[] */
12+
public function sayHello(): array|null
13+
{
14+
if(PHP_VERSION_ID >= 80400) {
15+
} else {
16+
}
17+
return [
18+
new Exception(previous: new Exception()),
19+
];
20+
}
21+
}
22+
23+
class HelloWorld2
24+
{
25+
/** @return mixed[] */
26+
public function sayHello(): array|null
27+
{
28+
return [
29+
PHP_VERSION_ID >= 80400 ? 1 : 0,
30+
new Exception(previous: new Exception()),
31+
];
32+
}
33+
}
34+
35+
class HelloWorld3
36+
{
37+
/** @return mixed[] */
38+
public function sayHello(): array|null
39+
{
40+
return [
41+
PHP_VERSION_ID >= 70400 ? 1 : 0,
42+
new Exception(previous: new Exception()),
43+
];
44+
}
45+
}
46+
47+
class HelloWorld4
48+
{
49+
/** @return mixed[] */
50+
public function sayHello(): array|null
51+
{
52+
return [
53+
PHP_VERSION_ID < 80000 ? 1 : 0,
54+
new Exception(previous: new Exception()),
55+
];
56+
}
57+
}

0 commit comments

Comments
 (0)