Repository navigation
Expand file tree
/
Copy pathUnusedMethodParametersRule.php
More file actions
182 lines (164 loc) · 5.2 KB
/
Copy pathUnusedMethodParametersRule.php
File metadata and controls
182 lines (164 loc) · 5.2 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Methods;
use PhpParser\Node;
use PHPStan\Analyser\RuleErrorTransformer;
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Node\ClassMethodsNode;
use PHPStan\Node\Method\MethodVariableWrites;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrors\TransformedRuleError;
use PHPStan\Rules\UnusedParametersCheck;
use function count;
use function sprintf;
use function str_starts_with;
use function strtolower;
/**
* Reports unused parameters of private methods: a private method has no
* callers outside the class, so its signature is not a contract - unlike a
* public or protected one, whose parameters may be dictated by an interface,
* a parent or an override. A private method the class hands out as a callable
* (first-class callable syntax or a callable array) escapes the class, so it
* is skipped as well.
*
* Errors are reported with the scope inside the method, as if the rule
* listened to the method's VariableWritesNode, so a method declared in a
* trait is reported in the trait's file.
*
* @implements Rule<ClassMethodsNode>
*/
final class UnusedMethodParametersRule implements Rule
{
public function __construct(
private UnusedParametersCheck $check,
private RuleErrorTransformer $ruleErrorTransformer,
)
{
}
public function getNodeType(): string
{
return ClassMethodsNode::class;
}
public function processNode(Node $node, Scope $scope): array
{
$referencedAsCallable = null;
$errors = [];
foreach ($node->getMethodVariableWrites() as $methodVariableWrites) {
$methodErrors = $this->getMethodErrors($methodVariableWrites);
if (count($methodErrors) === 0) {
continue;
}
$referencedAsCallable ??= $this->getMethodsReferencedAsCallable($node);
if ($referencedAsCallable === true) {
return [];
}
$methodNode = $methodVariableWrites->getNode()->getFunctionLike();
if (!$methodNode instanceof Node\Stmt\ClassMethod) {
continue;
}
if (isset($referencedAsCallable[$methodNode->name->toLowerString()])) {
continue;
}
foreach ($methodErrors as $methodError) {
$errors[] = new TransformedRuleError($this->ruleErrorTransformer->transform(
$methodError,
$methodVariableWrites->getScope(),
[],
$methodVariableWrites->getNode(),
));
}
}
return $errors;
}
/**
* @return list<IdentifierRuleError>
*/
private function getMethodErrors(MethodVariableWrites $methodVariableWrites): array
{
$variableWritesNode = $methodVariableWrites->getNode();
$scope = $methodVariableWrites->getScope();
$originalNode = $variableWritesNode->getFunctionLike();
if (!$originalNode instanceof Node\Stmt\ClassMethod) {
return [];
}
if (!$originalNode->isPrivate() || $originalNode->stmts === null || $this->check->isNoOpBody($originalNode->stmts)) {
return [];
}
if (str_starts_with($originalNode->name->toString(), '__')) {
// a magic method's signature is dictated by the engine, and the
// constructor has its own rule
return [];
}
if (count($originalNode->params) === 0) {
return [];
}
if (!$scope->isInClass()) {
return [];
}
$method = $scope->getFunction();
if ($method === null) {
return [];
}
$methodDescription = sprintf(
'%s::%s()',
SprintfHelper::escapeFormatString($scope->getClassReflection()->getDisplayName()),
SprintfHelper::escapeFormatString($originalNode->name->toString()),
);
return $this->check->getUnusedParameterErrors(
$variableWritesNode,
$method,
$originalNode->params,
sprintf('Method %s has an unused parameter $%%s.', $methodDescription),
'method.unusedParameter',
true,
sprintf('Method %s has a parameter $%%s that only flows into values that are never used.', $methodDescription),
'method.unusedParameterFlow',
);
}
/**
* Lowercased names of the methods the class references as callables.
* True when a first-class callable's method name cannot be resolved, so
* any method may be referenced.
*
* @return array<string, true>|true
*/
private function getMethodsReferencedAsCallable(ClassMethodsNode $node): array|bool
{
$methodNames = [];
foreach ($node->getMethodCalls() as $methodCall) {
$callNode = $methodCall->getNode();
$callScope = $methodCall->getScope();
if ($callNode instanceof Node\Expr\Array_) {
$arrayType = $callScope->getType($callNode);
if ($arrayType->isCallable()->no()) {
continue;
}
foreach ($arrayType->getConstantArrays() as $constantArray) {
foreach ($constantArray->findTypeAndMethodNames() as $typeAndMethod) {
if ($typeAndMethod->isUnknown()) {
continue;
}
$methodNames[strtolower($typeAndMethod->getMethod())] = true;
}
}
continue;
}
if (!$callNode->isFirstClassCallable()) {
continue;
}
if ($callNode->name instanceof Node\Identifier) {
$methodNames[$callNode->name->toLowerString()] = true;
continue;
}
$constantStrings = $callScope->getType($callNode->name)->getConstantStrings();
if (count($constantStrings) === 0) {
return true;
}
foreach ($constantStrings as $constantString) {
$methodNames[strtolower($constantString->getValue())] = true;
}
}
return $methodNames;
}
}