Skip to content
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
6 changes: 3 additions & 3 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3138,7 +3138,7 @@ private function enterFunctionLike(

$paramExprString = '$' . $parameter->getName();
if ($parameter->isVariadic()) {
if ($this->phpVersion->supportsNamedArguments() && $functionReflection->acceptsNamedArguments()->yes()) {
if (!$this->getPhpVersion()->supportsNamedArguments()->no() && $functionReflection->acceptsNamedArguments()->yes()) {
$parameterType = new ArrayType(new UnionType([new IntegerType(), new StringType()]), $parameterType);
} else {
$parameterType = TypeCombinator::intersect(new ArrayType(new IntegerType(), $parameterType), new AccessoryArrayListType());
Expand All @@ -3153,7 +3153,7 @@ private function enterFunctionLike(

$nativeParameterType = $parameter->getNativeType();
if ($parameter->isVariadic()) {
if ($this->phpVersion->supportsNamedArguments() && $functionReflection->acceptsNamedArguments()->yes()) {
if (!$this->getPhpVersion()->supportsNamedArguments()->no() && $functionReflection->acceptsNamedArguments()->yes()) {
$nativeParameterType = new ArrayType(new UnionType([new IntegerType(), new StringType()]), $nativeParameterType);
} else {
$nativeParameterType = TypeCombinator::intersect(new ArrayType(new IntegerType(), $nativeParameterType), new AccessoryArrayListType());
Expand Down Expand Up @@ -3628,7 +3628,7 @@ public function getFunctionType($type, bool $isNullable, bool $isVariadic): Type
);
}
if ($isVariadic) {
if ($this->phpVersion->supportsNamedArguments()) {
if (!$this->getPhpVersion()->supportsNamedArguments()->no()) {
return new ArrayType(new UnionType([new IntegerType(), new StringType()]), $this->getFunctionType(
$type,
false,
Expand Down
5 changes: 5 additions & 0 deletions src/Php/PhpVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public function producesWarningForFinalPrivateMethods(): TrinaryLogic
return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
}

public function supportsNamedArguments(): TrinaryLogic
{
return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
}

}
4 changes: 1 addition & 3 deletions src/Rules/FunctionCallParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PhpParser\Node\Expr;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ExtendedParameterReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptor;
Expand Down Expand Up @@ -42,7 +41,6 @@ final class FunctionCallParametersCheck
public function __construct(
private RuleLevelHelper $ruleLevelHelper,
private NullsafeCheck $nullsafeCheck,
private PhpVersion $phpVersion,
private UnresolvableTypeHelper $unresolvableTypeHelper,
private PropertyReflectionFinder $propertyReflectionFinder,
private bool $checkArgumentTypes,
Expand Down Expand Up @@ -201,7 +199,7 @@ public function check(
];
}

if ($hasNamedArguments && !$this->phpVersion->supportsNamedArguments() && !(bool) $funcCall->getAttribute('isAttribute', false)) {
if ($hasNamedArguments && !$scope->getPhpVersion()->supportsNamedArguments()->yes() && !(bool) $funcCall->getAttribute('isAttribute', false)) {
$errors[] = RuleErrorBuilder::message('Named arguments are supported only on PHP 8.0 and later.')
->identifier('argument.namedNotSupported')
->line($funcCall->getStartLine())
Expand Down
4 changes: 1 addition & 3 deletions tests/PHPStan/Analyser/Bug9307CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Analyser;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\FunctionCallParametersCheck;
use PHPStan\Rules\Methods\CallMethodsRule;
use PHPStan\Rules\Methods\MethodCallCheck;
Expand All @@ -12,7 +11,6 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;

/**
* @extends RuleTestCase<CallMethodsRule>
Expand All @@ -26,7 +24,7 @@ protected function getRule(): Rule
$ruleLevelHelper = new RuleLevelHelper($reflectionProvider, true, false, true, true, false, false);
return new CallMethodsRule(
new MethodCallCheck($reflectionProvider, $ruleLevelHelper, true, true),
new FunctionCallParametersCheck($ruleLevelHelper, new NullsafeCheck(), new PhpVersion(PHP_VERSION_ID), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true),
new FunctionCallParametersCheck($ruleLevelHelper, new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true),
);
}

Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-2600-php-version-scope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php // lint > 7.4

namespace Bug2600PhpVersionScope;

use function PHPStan\Testing\assertType;

if (PHP_VERSION_ID >= 80000) {
class Foo8 {
/**
* @param mixed $x
*/
public function doBaz(...$x) {
assertType('array<int|string, mixed>', $x);
}
}
} else {
class Foo9 {
/**
* @param mixed $x
*/
public function doBaz(...$x) {
assertType('list<mixed>', $x);
}
}

}
2 changes: 0 additions & 2 deletions tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Rules\Classes;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\AttributesCheck;
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\ClassForbiddenNameCheck;
Expand Down Expand Up @@ -35,7 +34,6 @@ protected function getRule(): Rule
new FunctionCallParametersCheck(
new RuleLevelHelper($reflectionProvider, true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false),
new NullsafeCheck(),
new PhpVersion(80000),
new UnresolvableTypeHelper(),
new PropertyReflectionFinder(),
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Rules\Classes;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\AttributesCheck;
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\ClassForbiddenNameCheck;
Expand Down Expand Up @@ -30,7 +29,6 @@ protected function getRule(): Rule
new FunctionCallParametersCheck(
new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false),
new NullsafeCheck(),
new PhpVersion(80000),
new UnresolvableTypeHelper(),
new PropertyReflectionFinder(),
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Rules\Classes;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\ClassForbiddenNameCheck;
use PHPStan\Rules\ClassNameCheck;
Expand All @@ -26,7 +25,7 @@ protected function getRule(): Rule
$reflectionProvider = $this->createReflectionProvider();
return new InstantiationRule(
$reflectionProvider,
new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false), new NullsafeCheck(), new PhpVersion(80000), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true),
new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true),
new ClassNameCheck(
new ClassCaseSensitivityCheck($reflectionProvider, true),
new ClassForbiddenNameCheck(self::getContainer()),
Expand Down
11 changes: 9 additions & 2 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Rules\Classes;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\ClassForbiddenNameCheck;
use PHPStan\Rules\ClassNameCheck;
Expand All @@ -26,7 +25,7 @@ protected function getRule(): Rule
$reflectionProvider = $this->createReflectionProvider();
return new InstantiationRule(
$reflectionProvider,
new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false), new NullsafeCheck(), new PhpVersion(80000), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true),
new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true),
new ClassNameCheck(
new ClassCaseSensitivityCheck($reflectionProvider, true),
new ClassForbiddenNameCheck(self::getContainer()),
Expand Down Expand Up @@ -290,6 +289,10 @@ public function testBug4056(): void

public function testNamedArguments(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0');
}

$this->analyse([__DIR__ . '/data/instantiation-named-arguments.php'], [
[
'Missing parameter $j (int) in call to InstantiationNamedArguments\Foo constructor.',
Expand Down Expand Up @@ -501,6 +504,10 @@ public function testBug10248(): void

public function testBug11815(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0');
}

$this->analyse([__DIR__ . '/data/bug-11815.php'], []);
}

Expand Down
2 changes: 0 additions & 2 deletions tests/PHPStan/Rules/EnumCases/EnumCaseAttributesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Rules\EnumCases;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\AttributesCheck;
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\ClassForbiddenNameCheck;
Expand Down Expand Up @@ -30,7 +29,6 @@ protected function getRule(): Rule
new FunctionCallParametersCheck(
new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false),
new NullsafeCheck(),
new PhpVersion(80100),
new UnresolvableTypeHelper(),
new PropertyReflectionFinder(),
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Rules\Functions;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\AttributesCheck;
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\ClassForbiddenNameCheck;
Expand Down Expand Up @@ -30,7 +29,6 @@ protected function getRule(): Rule
new FunctionCallParametersCheck(
new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false),
new NullsafeCheck(),
new PhpVersion(80000),
new UnresolvableTypeHelper(),
new PropertyReflectionFinder(),
true,
Expand Down
6 changes: 4 additions & 2 deletions tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Rules\Functions;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\FunctionCallParametersCheck;
use PHPStan\Rules\NullsafeCheck;
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
Expand All @@ -27,7 +26,6 @@ protected function getRule(): Rule
new FunctionCallParametersCheck(
$ruleLevelHelper,
new NullsafeCheck(),
new PhpVersion(80000),
new UnresolvableTypeHelper(),
new PropertyReflectionFinder(),
true,
Expand Down Expand Up @@ -158,6 +156,10 @@ public function testRule(): void

public function testNamedArguments(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0');
}

$this->analyse([__DIR__ . '/data/callables-named-arguments.php'], [
[
'Missing parameter $j (int) in call to closure.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Rules\Functions;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\FunctionCallParametersCheck;
use PHPStan\Rules\NullsafeCheck;
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
Expand All @@ -28,7 +27,7 @@ protected function getRule(): Rule
$broker = $this->createReflectionProvider();
return new CallToFunctionParametersRule(
$broker,
new FunctionCallParametersCheck(new RuleLevelHelper($broker, true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false), new NullsafeCheck(), new PhpVersion(80000), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true),
new FunctionCallParametersCheck(new RuleLevelHelper($broker, true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true),
);
}

Expand Down Expand Up @@ -477,6 +476,10 @@ public function testGenericFunction(): void

public function testNamedArguments(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0');
}

$errors = [
[
'Missing parameter $j (int) in call to function FunctionNamedArguments\foo.',
Expand All @@ -491,12 +494,6 @@ public function testNamedArguments(): void
14,
],
];
if (PHP_VERSION_ID < 80000) {
$errors[] = [
'Missing parameter $arr1 (array) in call to function array_merge.',
14,
];
}

require_once __DIR__ . '/data/named-arguments-define.php';
$this->analyse([__DIR__ . '/data/named-arguments.php'], $errors);
Expand Down
3 changes: 1 addition & 2 deletions tests/PHPStan/Rules/Functions/CallUserFuncRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Rules\Functions;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\FunctionCallParametersCheck;
use PHPStan\Rules\NullsafeCheck;
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
Expand All @@ -21,7 +20,7 @@ class CallUserFuncRuleTest extends RuleTestCase
protected function getRule(): Rule
{
$reflectionProvider = $this->createReflectionProvider();
return new CallUserFuncRule($reflectionProvider, new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, true, false, false), new NullsafeCheck(), new PhpVersion(80000), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true));
return new CallUserFuncRule($reflectionProvider, new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, true, false, false), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true));
}

public function testRule(): void
Expand Down
2 changes: 0 additions & 2 deletions tests/PHPStan/Rules/Functions/ClosureAttributesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Rules\Functions;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\AttributesCheck;
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\ClassForbiddenNameCheck;
Expand Down Expand Up @@ -30,7 +29,6 @@ protected function getRule(): Rule
new FunctionCallParametersCheck(
new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false),
new NullsafeCheck(),
new PhpVersion(80000),
new UnresolvableTypeHelper(),
new PropertyReflectionFinder(),
true,
Expand Down
2 changes: 0 additions & 2 deletions tests/PHPStan/Rules/Functions/FunctionAttributesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Rules\Functions;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\AttributesCheck;
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\ClassForbiddenNameCheck;
Expand Down Expand Up @@ -30,7 +29,6 @@ protected function getRule(): Rule
new FunctionCallParametersCheck(
new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false),
new NullsafeCheck(),
new PhpVersion(80000),
new UnresolvableTypeHelper(),
new PropertyReflectionFinder(),
true,
Expand Down
2 changes: 0 additions & 2 deletions tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Rules\Functions;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\AttributesCheck;
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\ClassForbiddenNameCheck;
Expand Down Expand Up @@ -30,7 +29,6 @@ protected function getRule(): Rule
new FunctionCallParametersCheck(
new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false),
new NullsafeCheck(),
new PhpVersion(80000),
new UnresolvableTypeHelper(),
new PropertyReflectionFinder(),
true,
Expand Down
Loading
Loading