forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallUserFuncRule.php
More file actions
98 lines (85 loc) · 2.94 KB
/
CallUserFuncRule.php
File metadata and controls
98 lines (85 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Functions;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\FunctionCallParametersCheck;
use PHPStan\Rules\Rule;
use function count;
use function sprintf;
use function ucfirst;
/**
* @implements Rule<FuncCall>
*/
#[RegisteredRule(level: 5)]
final class CallUserFuncRule implements Rule
{
public function __construct(
private ReflectionProvider $reflectionProvider,
private FunctionCallParametersCheck $check,
)
{
}
public function getNodeType(): string
{
return FuncCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Name) {
return [];
}
if (count($node->getArgs()) === 0) {
return [];
}
if (!$this->reflectionProvider->hasFunction($node->name, $scope)) {
return [];
}
$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope);
$functionName = $functionReflection->getName();
if ($functionName === 'call_user_func') {
$result = ArgumentsNormalizer::reorderCallUserFuncArguments(
$node,
$scope,
);
} elseif ($functionName === 'call_user_func_array') {
$result = ArgumentsNormalizer::reorderCallUserFuncArrayArguments(
$node,
$scope,
);
} else {
return [];
}
if ($result === null) {
return [];
}
[$parametersAcceptor, $funcCall, $acceptsNamedArguments] = $result;
$callableDescription = sprintf('callable passed to %s()', $functionName);
return $this->check->check(
$parametersAcceptor,
$scope,
false,
$funcCall,
'function',
$acceptsNamedArguments,
ucfirst($callableDescription) . ' invoked with %d parameter, %d required.',
ucfirst($callableDescription) . ' invoked with %d parameters, %d required.',
ucfirst($callableDescription) . ' invoked with %d parameter, at least %d required.',
ucfirst($callableDescription) . ' invoked with %d parameters, at least %d required.',
ucfirst($callableDescription) . ' invoked with %d parameter, %d-%d required.',
ucfirst($callableDescription) . ' invoked with %d parameters, %d-%d required.',
'%s of ' . $callableDescription . ' expects %s, %s given.',
'Result of ' . $callableDescription . ' (void) is used.',
'%s of ' . $callableDescription . ' is passed by reference, so it expects variables only.',
'Unable to resolve the template type %s in call to ' . $callableDescription,
'Missing parameter $%s in call to ' . $callableDescription . '.',
'Unknown parameter $%s in call to ' . $callableDescription . '.',
'Return type of call to ' . $callableDescription . ' contains unresolvable type.',
'%s of ' . $callableDescription . ' contains unresolvable type.',
ucfirst($callableDescription) . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
);
}
}