-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathCallMethodsRule.php
More file actions
168 lines (149 loc) · 5.23 KB
/
CallMethodsRule.php
File metadata and controls
168 lines (149 loc) · 5.23 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Methods;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\FunctionCallParametersCheck;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use function array_merge;
use function str_contains;
/**
* @implements Rule<Node\Expr\MethodCall>
*/
#[RegisteredRule(level: 0)]
final class CallMethodsRule implements Rule
{
public function __construct(
private MethodCallCheck $methodCallCheck,
private FunctionCallParametersCheck $parametersCheck,
)
{
}
public function getNodeType(): string
{
return MethodCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
$errors = [];
if ($node->name instanceof Node\Identifier) {
$methodNameScopes = [$node->name->name => $scope];
} else {
$nameType = $scope->getType($node->name);
$methodNameScopes = [];
foreach ($nameType->getConstantStrings() as $constantString) {
$name = $constantString->getValue();
$methodNameScopes[$name] = $scope->filterByTruthyValue(new Identical($node->name, new String_($name)));
}
}
foreach ($methodNameScopes as $methodName => $methodScope) {
$errors = array_merge($errors, $this->processSingleMethodCall(
$methodScope,
$node,
(string) $methodName, // @phpstan-ignore cast.useless
));
}
return $errors;
}
/**
* @return list<IdentifierRuleError>
*/
private function processSingleMethodCall(Scope $scope, MethodCall $node, string $methodName): array
{
[$errors, $methodReflection] = $this->methodCallCheck->check($scope, $methodName, $node->var, $node->name);
if ($methodReflection === null) {
return $errors;
}
$args = $node->getArgs();
$selectedAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
$args,
$methodReflection->getVariants(),
$methodReflection->getNamedArgumentsVariants(),
);
if ($this->shouldCheckPerUnionMember($selectedAcceptor, $args)) {
$callerType = $scope->getType($node->var);
foreach ($callerType->getObjectClassReflections() as $classReflection) {
if (!$classReflection->hasMethod($methodName)) {
continue;
}
$memberMethod = $classReflection->getMethod($methodName, $scope);
$memberAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
$args,
$memberMethod->getVariants(),
$memberMethod->getNamedArgumentsVariants(),
);
$errors = array_merge($errors, $this->checkMethodParameters($memberAcceptor, $scope, $node, $memberMethod));
}
} else {
$errors = array_merge($errors, $this->checkMethodParameters($selectedAcceptor, $scope, $node, $methodReflection));
}
return $errors;
}
/**
* @return list<IdentifierRuleError>
*/
private function checkMethodParameters(
\PHPStan\Reflection\ParametersAcceptor $acceptor,
Scope $scope,
MethodCall $node,
ExtendedMethodReflection $methodReflection,
): array
{
$declaringClass = $methodReflection->getDeclaringClass();
$messagesMethodName = SprintfHelper::escapeFormatString($declaringClass->getDisplayName() . '::' . $methodReflection->getName() . '()');
return $this->parametersCheck->check(
$acceptor,
$scope,
$declaringClass->isBuiltin(),
$node,
'method',
$methodReflection->acceptsNamedArguments(),
'Method ' . $messagesMethodName . ' invoked with %d parameter, %d required.',
'Method ' . $messagesMethodName . ' invoked with %d parameters, %d required.',
'Method ' . $messagesMethodName . ' invoked with %d parameter, at least %d required.',
'Method ' . $messagesMethodName . ' invoked with %d parameters, at least %d required.',
'Method ' . $messagesMethodName . ' invoked with %d parameter, %d-%d required.',
'Method ' . $messagesMethodName . ' invoked with %d parameters, %d-%d required.',
'%s of method ' . $messagesMethodName . ' expects %s, %s given.',
'Result of method ' . $messagesMethodName . ' (void) is used.',
'%s of method ' . $messagesMethodName . ' is passed by reference, so it expects variables only.',
'Unable to resolve the template type %s in call to method ' . $messagesMethodName,
'Missing parameter $%s in call to method ' . $messagesMethodName . '.',
'Unknown parameter $%s in call to method ' . $messagesMethodName . '.',
'Return type of call to method ' . $messagesMethodName . ' contains unresolvable type.',
'%s of method ' . $messagesMethodName . ' contains unresolvable type.',
'Method ' . $messagesMethodName . ' invoked with %s, but it\'s not allowed because of @no-named-arguments.',
);
}
/**
* @param Node\Arg[] $args
*/
private function shouldCheckPerUnionMember(\PHPStan\Reflection\ParametersAcceptor $acceptor, array $args): bool
{
$hasCompoundName = false;
foreach ($acceptor->getParameters() as $parameter) {
if (str_contains($parameter->getName(), '|')) {
$hasCompoundName = true;
break;
}
}
if (!$hasCompoundName) {
return false;
}
foreach ($args as $arg) {
if ($arg->name !== null) {
return false;
}
}
return true;
}
}