-
Notifications
You must be signed in to change notification settings - Fork 564
Expand file tree
/
Copy pathUnusedFunctionParametersCheck.php
More file actions
120 lines (109 loc) · 3.42 KB
/
UnusedFunctionParametersCheck.php
File metadata and controls
120 lines (109 loc) · 3.42 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
<?php declare(strict_types = 1);
namespace PHPStan\Rules;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
use function array_merge;
use function in_array;
use function is_array;
use function is_string;
use function sprintf;
#[AutowiredService]
final class UnusedFunctionParametersCheck
{
public function __construct(
private ReflectionProvider $reflectionProvider,
#[AutowiredParameter(ref: '%featureToggles.reportPreciseLineForUnusedFunctionParameter%')]
private bool $reportExactLine,
)
{
}
/**
* @param Variable[] $parameterVars
* @param Node[] $statements
* @param 'constructor.unusedParameter'|'closure.unusedUse' $identifier
* @return list<IdentifierRuleError>
*/
public function getUnusedParameters(
Scope $scope,
array $parameterVars,
array $statements,
string $unusedParameterMessage,
string $identifier,
): array
{
$unusedParameters = [];
foreach ($parameterVars as $variable) {
if (!is_string($variable->name)) {
throw new ShouldNotHappenException();
}
$unusedParameters[$variable->name] = $variable;
}
foreach ($this->getUsedVariables($scope, $statements) as $variableName) {
unset($unusedParameters[$variableName]);
}
$errors = [];
foreach ($unusedParameters as $name => $variable) {
$errorBuilder = RuleErrorBuilder::message(sprintf($unusedParameterMessage, $name))->identifier($identifier);
if ($this->reportExactLine) {
$errorBuilder->line($variable->getStartLine());
}
$errors[] = $errorBuilder->build();
}
return $errors;
}
/**
* @param Node[]|Node|scalar|null $node
* @return string[]
*/
private function getUsedVariables(Scope $scope, $node): array
{
$variableNames = [];
if ($node instanceof Node) {
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name && !$node->isFirstClassCallable()) {
$functionName = $this->reflectionProvider->resolveFunctionName($node->name, $scope);
if (in_array($functionName, ['func_get_args', 'get_defined_vars'], true)) {
return $scope->getDefinedVariables();
}
}
if ($node instanceof Node\Expr\Include_) {
return $scope->getDefinedVariables();
}
if ($node instanceof Variable && is_string($node->name) && $node->name !== 'this') {
return [$node->name];
}
if ($node instanceof Node\ClosureUse && is_string($node->var->name)) {
return [$node->var->name];
}
if (
$node instanceof Node\Expr\FuncCall
&& !$node->isFirstClassCallable()
&& $node->name instanceof Node\Name
&& (string) $node->name === 'compact'
) {
foreach ($node->getArgs() as $arg) {
$argType = $scope->getType($arg->value);
foreach ($argType->getConstantStrings() as $constantStringType) {
$variableNames[] = $constantStringType->getValue();
}
}
}
foreach ($node->getSubNodeNames() as $subNodeName) {
if ($node instanceof Node\Expr\Closure && $subNodeName !== 'uses') {
continue;
}
$subNode = $node->{$subNodeName};
$variableNames = array_merge($variableNames, $this->getUsedVariables($scope, $subNode));
}
} elseif (is_array($node)) {
foreach ($node as $subNode) {
$variableNames = array_merge($variableNames, $this->getUsedVariables($scope, $subNode));
}
}
return $variableNames;
}
}