Skip to content

Commit 3d26eef

Browse files
committed
fix: apply dynamic parameter type extensions in FunctionCallParametersCheck
1 parent 0b5944b commit 3d26eef

28 files changed

Lines changed: 61 additions & 10 deletions

‎e2e/parameter-type-extension/phpstan-baseline.neon‎

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎e2e/parameter-type-extension/phpstan.neon.dist‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
includes:
2-
- phpstan-baseline.neon
31
parameters:
42
level: 9
53
paths:

‎src/Rules/FunctionCallParametersCheck.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use PHPStan\DependencyInjection\AutowiredService;
1313
use PHPStan\Reflection\ConstantReflection;
1414
use PHPStan\Reflection\ExtendedParameterReflection;
15+
use PHPStan\Reflection\FunctionReflection;
16+
use PHPStan\Reflection\MethodReflection;
1517
use PHPStan\Reflection\ParameterReflection;
1618
use PHPStan\Reflection\ParametersAcceptor;
1719
use PHPStan\Reflection\ReflectionProvider;
@@ -23,6 +25,7 @@
2325
use PHPStan\TrinaryLogic;
2426
use PHPStan\Type\ConditionalType;
2527
use PHPStan\Type\Constant\ConstantIntegerType;
28+
use PHPStan\Type\DynamicParameterTypeResolver;
2629
use PHPStan\Type\ErrorType;
2730
use PHPStan\Type\Generic\TemplateType;
2831
use PHPStan\Type\IntegerRangeType;
@@ -55,6 +58,7 @@ public function __construct(
5558
private UnresolvableTypeHelper $unresolvableTypeHelper,
5659
private PropertyReflectionFinder $propertyReflectionFinder,
5760
private ReflectionProvider $reflectionProvider,
61+
private DynamicParameterTypeResolver $dynamicParameterTypeResolver,
5862
#[AutowiredParameter(ref: '%checkFunctionArgumentTypes%')]
5963
private bool $checkArgumentTypes,
6064
#[AutowiredParameter]
@@ -98,6 +102,7 @@ public function check(
98102
string $exclusiveConstantsMessage,
99103
string $bitmaskNotAllowedMessage,
100104
?array $renamedNamedArgumentParameterData,
105+
MethodReflection|FunctionReflection|null $calleeReflection = null,
101106
): array
102107
{
103108
if ($funcCall instanceof Node\Expr\MethodCall || $funcCall instanceof Node\Expr\StaticCall || $funcCall instanceof Node\Expr\FuncCall) {
@@ -391,6 +396,15 @@ public function check(
391396
if ($this->checkArgumentTypes) {
392397
$parameterType = TypeUtils::resolveLateResolvableTypes($parameter->getType());
393398

399+
// judge the argument against the type the extensions announced for
400+
// the parameter, which is what NodeScopeResolver walked it with
401+
if (!$funcCall instanceof Node\Expr\New_) {
402+
$overriddenType = $this->dynamicParameterTypeResolver->resolve($funcCall, $calleeReflection, $parameter, $scope);
403+
if ($overriddenType !== null) {
404+
$parameterType = $overriddenType;
405+
}
406+
}
407+
394408
if (
395409
!$parameter->passedByReference()->createsNewVariable()
396410
|| (!$isBuiltin && !$argumentValueType instanceof ErrorType)

‎src/Rules/Functions/CallToFunctionParametersRule.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE
7474
'Constants %s cannot be combined for %s of function ' . $functionName . '.',
7575
'Combining constants with | is not allowed for %s of function ' . $functionName . '.',
7676
null,
77+
$function,
7778
);
7879
}
7980

‎src/Rules/Methods/CallMethodsRule.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ private function processSingleMethodCall(Scope&NodeCallbackInvoker&CollectedData
108108
$declaringClass->getName(),
109109
$methodReflection->getName(),
110110
] : null,
111+
$methodReflection,
111112
));
112113
}
113114

‎src/Rules/Methods/CallStaticMethodsRule.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ private function processSingleMethodCall(Scope&NodeCallbackInvoker&CollectedData
114114
'Constants %s cannot be combined for %s of ' . $lowercasedMethodName . '.',
115115
'Combining constants with | is not allowed for %s of ' . $lowercasedMethodName . '.',
116116
null,
117+
$method,
117118
));
118119

119120
return $errors;

‎tests/PHPStan/Analyser/Bug9307CallMethodsRuleTest.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Rules\Rule;
1212
use PHPStan\Rules\RuleLevelHelper;
1313
use PHPStan\Testing\RuleTestCase;
14+
use PHPStan\Type\DynamicParameterTypeResolver;
1415

1516
/**
1617
* @extends RuleTestCase<CallMethodsRule>
@@ -44,6 +45,7 @@ protected function getRule(): Rule
4445
new UnresolvableTypeHelper(),
4546
new PropertyReflectionFinder(),
4647
$reflectionProvider,
48+
self::getContainer()->getByType(DynamicParameterTypeResolver::class),
4749
checkArgumentTypes: true,
4850
checkArgumentsPassedByReference: true,
4951
checkExtraArguments: true,

‎tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPStan\Rules\Rule;
1616
use PHPStan\Rules\RuleLevelHelper;
1717
use PHPStan\Testing\RuleTestCase;
18+
use PHPStan\Type\DynamicParameterTypeResolver;
1819
use PHPUnit\Framework\Attributes\RequiresPhp;
1920

2021
/**
@@ -49,6 +50,7 @@ protected function getRule(): Rule
4950
new UnresolvableTypeHelper(),
5051
new PropertyReflectionFinder(),
5152
$reflectionProvider,
53+
self::getContainer()->getByType(DynamicParameterTypeResolver::class),
5254
checkArgumentTypes: true,
5355
checkArgumentsPassedByReference: true,
5456
checkExtraArguments: true,

‎tests/PHPStan/Rules/Classes/ClassConstantAttributesRuleTest.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPStan\Rules\Rule;
1616
use PHPStan\Rules\RuleLevelHelper;
1717
use PHPStan\Testing\RuleTestCase;
18+
use PHPStan\Type\DynamicParameterTypeResolver;
1819

1920
/**
2021
* @extends RuleTestCase<ClassConstantAttributesRule>
@@ -44,6 +45,7 @@ protected function getRule(): Rule
4445
new UnresolvableTypeHelper(),
4546
new PropertyReflectionFinder(),
4647
$reflectionProvider,
48+
self::getContainer()->getByType(DynamicParameterTypeResolver::class),
4749
checkArgumentTypes: true,
4850
checkArgumentsPassedByReference: true,
4951
checkExtraArguments: true,

‎tests/PHPStan/Rules/Classes/ForbiddenNameCheckExtensionRuleTest.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPStan\Rules\Rule;
1616
use PHPStan\Rules\RuleLevelHelper;
1717
use PHPStan\Testing\RuleTestCase;
18+
use PHPStan\Type\DynamicParameterTypeResolver;
1819
use function array_merge;
1920

2021
/**
@@ -46,6 +47,7 @@ protected function getRule(): Rule
4647
new UnresolvableTypeHelper(),
4748
new PropertyReflectionFinder(),
4849
$reflectionProvider,
50+
self::getContainer()->getByType(DynamicParameterTypeResolver::class),
4951
checkArgumentTypes: true,
5052
checkArgumentsPassedByReference: true,
5153
checkExtraArguments: true,

0 commit comments

Comments
 (0)