Skip to content
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

Named argument detection is scope php-version dependent #3662

Open
wants to merge 2 commits into
base: 2.0.x
Choose a base branch
from
Open
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 @@ -23,4 +23,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 @@ -41,7 +40,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 @@ -194,7 +192,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
3 changes: 1 addition & 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php // lint >= 8.0

namespace InstantiationNamedArguments;

if (PHP_VERSION_ID < 80000) return;
class Foo
{

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
2 changes: 0 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
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
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace CallablesNamedArguments;

if (PHP_VERSION_ID < 80000) return;
class Foo
{

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Functions/data/named-arguments.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace FunctionNamedArguments;

if (PHP_VERSION_ID < 80000) return;
function bar(): void
{
foo(i: 1);
Expand Down
21 changes: 15 additions & 6 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Rules\Methods;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\FunctionCallParametersCheck;
use PHPStan\Rules\NullsafeCheck;
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
Expand All @@ -28,15 +27,13 @@ class CallMethodsRuleTest extends RuleTestCase

private bool $checkImplicitMixed = false;

private int $phpVersion = PHP_VERSION_ID;

protected function getRule(): Rule
{
$reflectionProvider = $this->createReflectionProvider();
$ruleLevelHelper = new RuleLevelHelper($reflectionProvider, $this->checkNullables, $this->checkThisOnly, $this->checkUnionTypes, $this->checkExplicitMixed, $this->checkImplicitMixed, false);
return new CallMethodsRule(
new MethodCallCheck($reflectionProvider, $ruleLevelHelper, true, true),
new FunctionCallParametersCheck($ruleLevelHelper, new NullsafeCheck(), new PhpVersion($this->phpVersion), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true),
new FunctionCallParametersCheck($ruleLevelHelper, new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true),
);
}

Expand Down Expand Up @@ -1822,12 +1819,25 @@ public function testDisallowNamedArguments(): void
]);
}

public function testDisallowNamedArgumentsInPhpVersionScope(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;

$this->analyse([__DIR__ . '/data/disallow-named-arguments-php-version-scope.php'], [
[
'Named arguments are supported only on PHP 8.0 and later.',
26,
],
]);
}

public function testNamedArguments(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->phpVersion = 80000;

$this->analyse([__DIR__ . '/data/named-arguments.php'], [
[
Expand Down Expand Up @@ -2104,7 +2114,6 @@ public function testBug4800(): void
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->phpVersion = 80000;
$this->analyse([__DIR__ . '/data/bug-4800.php'], [
[
'Missing parameter $bar (string) in call to method Bug4800\HelloWorld2::a().',
Expand Down
Loading
Loading