Skip to content

Commit 6f5c6ba

Browse files
committed
Accept closures matching the parameter closure type extension in argument checks
The closure body is analysed with the parameter type from Function/Method/StaticMethodParameterClosureTypeExtension (ArgumentsHandler), but FunctionCallParametersCheck still checked the closure only against the declared parameter type. A closure typed for what the extension says it receives, like fn (PostBuilder $q) for Closure(Builder<Post>): mixed, was reported as argument.type. When the declared parameter type does not accept a closure or arrow function argument, ask the extension and accept the argument if the extension's type does. Errors keep describing the declared parameter type. Amp-Thread-ID: https://ampcode.com/threads/T-01a196a1-2352-73e4-b226-d1df672638a1
1 parent 9c8ed05 commit 6f5c6ba

38 files changed

Lines changed: 620 additions & 1 deletion

‎src/Rules/AttributesCheck.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public function check(
163163
'Constants %s cannot be combined for %s of attribute class ' . $attributeClassName . ' constructor.',
164164
'Combining constants with | is not allowed for %s of attribute class ' . $attributeClassName . ' constructor.',
165165
null,
166+
null,
166167
);
167168

168169
foreach ($parameterErrors as $error) {

‎src/Rules/Classes/InstantiationRule.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ private function checkClassName(string $class, bool $isName, Node $node, Scope&N
338338
'Constants %s cannot be combined for %s of class ' . $classDisplayName . ' constructor.',
339339
'Combining constants with | is not allowed for %s of class ' . $classDisplayName . ' constructor.',
340340
null,
341+
$calledConstructorReflection,
341342
));
342343
}
343344

‎src/Rules/FunctionCallParametersCheck.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
use PhpParser\Node;
66
use PhpParser\Node\Expr;
77
use PHPStan\Analyser\CollectedDataEmitter;
8+
use PHPStan\Analyser\DependencyTracker;
89
use PHPStan\Analyser\MutatingScope;
910
use PHPStan\Analyser\NodeCallbackInvoker;
1011
use PHPStan\Analyser\Scope;
1112
use PHPStan\DependencyInjection\AutowiredParameter;
1213
use PHPStan\DependencyInjection\AutowiredService;
1314
use PHPStan\Reflection\ConstantReflection;
1415
use PHPStan\Reflection\ExtendedParameterReflection;
16+
use PHPStan\Reflection\FunctionReflection;
17+
use PHPStan\Reflection\MethodReflection;
1518
use PHPStan\Reflection\ParameterReflection;
1619
use PHPStan\Reflection\ParametersAcceptor;
1720
use PHPStan\Reflection\ReflectionProvider;
@@ -56,6 +59,7 @@ public function __construct(
5659
private UnresolvableTypeHelper $unresolvableTypeHelper,
5760
private PropertyReflectionFinder $propertyReflectionFinder,
5861
private ReflectionProvider $reflectionProvider,
62+
private ParameterClosureTypeHelper $parameterClosureTypeHelper,
5963
#[AutowiredParameter(ref: '%checkFunctionArgumentTypes%')]
6064
private bool $checkArgumentTypes,
6165
#[AutowiredParameter]
@@ -99,6 +103,7 @@ public function check(
99103
string $exclusiveConstantsMessage,
100104
string $bitmaskNotAllowedMessage,
101105
?array $renamedNamedArgumentParameterData,
106+
FunctionReflection|MethodReflection|null $calleeReflection,
102107
): array
103108
{
104109
if ($funcCall instanceof Node\Expr\MethodCall || $funcCall instanceof Node\Expr\StaticCall || $funcCall instanceof Node\Expr\FuncCall) {
@@ -402,6 +407,21 @@ public function check(
402407
$isStrictTypes = $scope->isDeclareStrictTypes()
403408
&& (!$isBuiltin || !$parameterType->isCallable()->yes());
404409
$accepts = $this->ruleLevelHelper->accepts($parameterType, $argumentValueType, $isStrictTypes);
410+
if (
411+
!$accepts->result
412+
&& $calleeReflection !== null
413+
&& ($argumentValue instanceof Expr\Closure || $argumentValue instanceof Expr\ArrowFunction)
414+
&& $scope instanceof DependencyTracker
415+
) {
416+
// the closure body was analysed with the type from a parameter closure type extension, see ArgumentsHandler
417+
$closureParameterType = $this->parameterClosureTypeHelper->getParameterType($funcCall, $calleeReflection, $parameter, $scope);
418+
if ($closureParameterType !== null) {
419+
$closureAccepts = $this->ruleLevelHelper->accepts($closureParameterType, $argumentValueType, $isStrictTypes);
420+
if ($closureAccepts->result) {
421+
$accepts = $closureAccepts;
422+
}
423+
}
424+
}
405425

406426
if (!$accepts->result) {
407427
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $argumentValueType);

‎src/Rules/Functions/CallCallablesRule.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public function processNode(
145145
'Constants %s cannot be combined for %s of ' . $callableDescription . '.',
146146
'Combining constants with | is not allowed for %s of ' . $callableDescription . '.',
147147
null,
148+
null,
148149
),
149150
);
150151
}

‎src/Rules/Functions/CallToFunctionParametersRule.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE
7878
'Constants %s cannot be combined for %s of function ' . $functionName . '.',
7979
'Combining constants with | is not allowed for %s of function ' . $functionName . '.',
8080
null,
81+
$function,
8182
);
8283
}
8384

‎src/Rules/Functions/CallUserFuncRule.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE
9898
'Constants %s cannot be combined for %s of ' . $callableDescription . '.',
9999
'Combining constants with | is not allowed for %s of ' . $callableDescription . '.',
100100
null,
101+
null,
101102
);
102103
}
103104

‎src/Rules/Methods/CallMethodsRule.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ private function processSingleMethodCall(Scope&NodeCallbackInvoker&CollectedData
118118
$declaringClass->getName(),
119119
$methodReflection->getName(),
120120
] : null,
121+
$methodReflection,
121122
));
122123
}
123124

‎src/Rules/Methods/CallStaticMethodsRule.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ private function processSingleMethodCall(Scope&NodeCallbackInvoker&CollectedData
124124
'Constants %s cannot be combined for %s of ' . $lowercasedMethodName . '.',
125125
'Combining constants with | is not allowed for %s of ' . $lowercasedMethodName . '.',
126126
null,
127+
$method,
127128
));
128129

129130
return $errors;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules;
4+
5+
use PhpParser\Node\Expr\FuncCall;
6+
use PhpParser\Node\Expr\MethodCall;
7+
use PhpParser\Node\Expr\New_;
8+
use PhpParser\Node\Expr\StaticCall;
9+
use PhpParser\Node\Identifier;
10+
use PhpParser\Node\Name;
11+
use PHPStan\Analyser\DependencyTracker;
12+
use PHPStan\Analyser\Scope;
13+
use PHPStan\DependencyInjection\AutowiredExtensions;
14+
use PHPStan\DependencyInjection\AutowiredService;
15+
use PHPStan\DependencyInjection\ExtensionsCollection;
16+
use PHPStan\Reflection\FunctionReflection;
17+
use PHPStan\Reflection\MethodReflection;
18+
use PHPStan\Reflection\ParameterReflection;
19+
use PHPStan\Type\FunctionParameterClosureTypeExtension;
20+
use PHPStan\Type\MethodParameterClosureTypeExtension;
21+
use PHPStan\Type\StaticMethodParameterClosureTypeExtension;
22+
use PHPStan\Type\Type;
23+
24+
/**
25+
* Asks the parameter closure type extensions for the type of a closure passed to a parameter,
26+
* the same way ArgumentsHandler does when it analyses the closure body.
27+
*/
28+
#[AutowiredService]
29+
final class ParameterClosureTypeHelper
30+
{
31+
32+
/**
33+
* @param ExtensionsCollection<FunctionParameterClosureTypeExtension> $functionParameterClosureTypeExtensions
34+
* @param ExtensionsCollection<MethodParameterClosureTypeExtension> $methodParameterClosureTypeExtensions
35+
* @param ExtensionsCollection<StaticMethodParameterClosureTypeExtension> $staticMethodParameterClosureTypeExtensions
36+
*/
37+
public function __construct(
38+
#[AutowiredExtensions(of: FunctionParameterClosureTypeExtension::class)]
39+
private ExtensionsCollection $functionParameterClosureTypeExtensions,
40+
#[AutowiredExtensions(of: MethodParameterClosureTypeExtension::class)]
41+
private ExtensionsCollection $methodParameterClosureTypeExtensions,
42+
#[AutowiredExtensions(of: StaticMethodParameterClosureTypeExtension::class)]
43+
private ExtensionsCollection $staticMethodParameterClosureTypeExtensions,
44+
)
45+
{
46+
}
47+
48+
public function getParameterType(
49+
FuncCall|MethodCall|StaticCall|New_ $callLike,
50+
FunctionReflection|MethodReflection $calleeReflection,
51+
ParameterReflection $parameter,
52+
Scope&DependencyTracker $scope,
53+
): ?Type
54+
{
55+
if ($callLike instanceof FuncCall && $calleeReflection instanceof FunctionReflection) {
56+
foreach ($this->functionParameterClosureTypeExtensions->getAll() as $extension) {
57+
if ($extension->isFunctionSupported($calleeReflection, $parameter)) {
58+
return $extension->getTypeFromFunctionCall($calleeReflection, $callLike, $parameter, $scope);
59+
}
60+
}
61+
} elseif ($calleeReflection instanceof MethodReflection) {
62+
if ($callLike instanceof StaticCall) {
63+
foreach ($this->staticMethodParameterClosureTypeExtensions->getAll() as $extension) {
64+
if ($extension->isStaticMethodSupported($calleeReflection, $parameter)) {
65+
return $extension->getTypeFromStaticMethodCall($calleeReflection, $callLike, $parameter, $scope);
66+
}
67+
}
68+
} elseif ($callLike instanceof New_ && $callLike->class instanceof Name) {
69+
$staticCall = new StaticCall(
70+
$callLike->class,
71+
new Identifier('__construct'),
72+
$callLike->getArgs(),
73+
);
74+
foreach ($this->staticMethodParameterClosureTypeExtensions->getAll() as $extension) {
75+
if ($extension->isStaticMethodSupported($calleeReflection, $parameter)) {
76+
return $extension->getTypeFromStaticMethodCall($calleeReflection, $staticCall, $parameter, $scope);
77+
}
78+
}
79+
} elseif ($callLike instanceof MethodCall) {
80+
foreach ($this->methodParameterClosureTypeExtensions->getAll() as $extension) {
81+
if ($extension->isMethodSupported($calleeReflection, $parameter)) {
82+
return $extension->getTypeFromMethodCall($calleeReflection, $callLike, $parameter, $scope);
83+
}
84+
}
85+
}
86+
}
87+
88+
return null;
89+
}
90+
91+
}

‎tests/PHPStan/Analyser/Bug9307CallMethodsRuleTest.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Rules\Methods\MethodCallCheck;
88
use PHPStan\Rules\NonStringableDynamicAccessCheck;
99
use PHPStan\Rules\NullsafeCheck;
10+
use PHPStan\Rules\ParameterClosureTypeHelper;
1011
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
1112
use PHPStan\Rules\Properties\PropertyReflectionFinder;
1213
use PHPStan\Rules\Rule;
@@ -45,6 +46,7 @@ protected function getRule(): Rule
4546
new UnresolvableTypeHelper(),
4647
new PropertyReflectionFinder(),
4748
$reflectionProvider,
49+
self::getContainer()->getByType(ParameterClosureTypeHelper::class),
4850
checkArgumentTypes: true,
4951
checkArgumentsPassedByReference: true,
5052
checkExtraArguments: true,

0 commit comments

Comments
 (0)