3
3
namespace PHPStan \Rules ;
4
4
5
5
use PhpParser \Node ;
6
+ use PhpParser \Node \Expr \Variable ;
6
7
use PHPStan \Analyser \Scope ;
7
8
use PHPStan \Reflection \ReflectionProvider ;
9
+ use PHPStan \ShouldNotHappenException ;
8
10
use PHPStan \Type \Constant \ConstantStringType ;
9
- use function array_fill_keys ;
10
- use function array_keys ;
11
+ use function array_combine ;
12
+ use function array_map ;
11
13
use function array_merge ;
12
14
use function is_array ;
13
15
use function is_string ;
16
18
final class UnusedFunctionParametersCheck
17
19
{
18
20
19
- public function __construct (private ReflectionProvider $ reflectionProvider )
21
+ public function __construct (
22
+ private ReflectionProvider $ reflectionProvider ,
23
+ private bool $ reportExactLine ,
24
+ )
20
25
{
21
26
}
22
27
23
28
/**
24
- * @param string [] $parameterNames
29
+ * @param Variable [] $parameterVars
25
30
* @param Node[] $statements
26
31
* @param 'constructor.unusedParameter'|'closure.unusedUse' $identifier
27
32
* @return list<IdentifierRuleError>
28
33
*/
29
34
public function getUnusedParameters (
30
35
Scope $ scope ,
31
- array $ parameterNames ,
36
+ array $ parameterVars ,
32
37
array $ statements ,
33
38
string $ unusedParameterMessage ,
34
39
string $ identifier ,
35
40
): array
36
41
{
37
- $ unusedParameters = array_fill_keys ($ parameterNames , true );
42
+ $ parameterNames = array_map (static function (Variable $ variable ): string {
43
+ if (!is_string ($ variable ->name )) {
44
+ throw new ShouldNotHappenException ();
45
+ }
46
+ return $ variable ->name ;
47
+ }, $ parameterVars );
48
+ $ unusedParameters = array_combine ($ parameterNames , $ parameterVars );
38
49
foreach ($ this ->getUsedVariables ($ scope , $ statements ) as $ variableName ) {
39
50
if (!isset ($ unusedParameters [$ variableName ])) {
40
51
continue ;
@@ -43,10 +54,12 @@ public function getUnusedParameters(
43
54
unset($ unusedParameters [$ variableName ]);
44
55
}
45
56
$ errors = [];
46
- foreach (array_keys ($ unusedParameters ) as $ name ) {
47
- $ errors [] = RuleErrorBuilder::message (
48
- sprintf ($ unusedParameterMessage , $ name ),
49
- )->identifier ($ identifier )->build ();
57
+ foreach ($ unusedParameters as $ name => $ variable ) {
58
+ $ errorBuilder = RuleErrorBuilder::message (sprintf ($ unusedParameterMessage , $ name ))->identifier ($ identifier );
59
+ if ($ this ->reportExactLine ) {
60
+ $ errorBuilder ->line ($ variable ->getStartLine ());
61
+ }
62
+ $ errors [] = $ errorBuilder ->build ();
50
63
}
51
64
52
65
return $ errors ;
@@ -66,7 +79,7 @@ private function getUsedVariables(Scope $scope, $node): array
66
79
return $ scope ->getDefinedVariables ();
67
80
}
68
81
}
69
- if ($ node instanceof Node \ Expr \ Variable && is_string ($ node ->name ) && $ node ->name !== 'this ' ) {
82
+ if ($ node instanceof Variable && is_string ($ node ->name ) && $ node ->name !== 'this ' ) {
70
83
return [$ node ->name ];
71
84
}
72
85
if ($ node instanceof Node \ClosureUse && is_string ($ node ->var ->name )) {
0 commit comments