33namespace PHPStan \Rules ;
44
55use PhpParser \Node ;
6+ use PHPStan \Analyser \Scope ;
7+ use PHPStan \Type \Constant \ConstantStringType ;
68
79class UnusedFunctionParametersCheck
810{
911
1012 /**
13+ * @param \PHPStan\Analyser\Scope $scope
1114 * @param string[] $parameterNames
1215 * @param \PhpParser\Node[] $statements
1316 * @param string $unusedParameterMessage
1417 * @return string[]
1518 */
1619 public function getUnusedParameters (
20+ Scope $ scope ,
1721 array $ parameterNames ,
1822 array $ statements ,
1923 string $ unusedParameterMessage
2024 ): array
2125 {
2226 $ unusedParameters = array_fill_keys ($ parameterNames , true );
23- foreach ($ this ->getUsedVariables ($ statements ) as $ variableName ) {
27+ foreach ($ this ->getUsedVariables ($ scope , $ statements ) as $ variableName ) {
2428 if (isset ($ unusedParameters [$ variableName ])) {
2529 unset($ unusedParameters [$ variableName ]);
2630 }
@@ -34,10 +38,11 @@ public function getUnusedParameters(
3438 }
3539
3640 /**
41+ * @param \PHPStan\Analyser\Scope $scope
3742 * @param \PhpParser\Node[]|\PhpParser\Node $node
3843 * @return string[]
3944 */
40- private function getUsedVariables ($ node ): array
45+ private function getUsedVariables (Scope $ scope , $ node ): array
4146 {
4247 $ variableNames = [];
4348 if ($ node instanceof Node) {
@@ -53,8 +58,9 @@ private function getUsedVariables($node): array
5358 && (string ) $ node ->name === 'compact '
5459 ) {
5560 foreach ($ node ->args as $ arg ) {
56- if ($ arg ->value instanceof Node \Scalar \String_) {
57- $ variableNames [] = $ arg ->value ->value ;
61+ $ argType = $ scope ->getType ($ arg ->value );
62+ if ($ argType instanceof ConstantStringType) {
63+ $ variableNames [] = $ argType ->getValue ();
5864 }
5965 }
6066 }
@@ -63,11 +69,11 @@ private function getUsedVariables($node): array
6369 continue ;
6470 }
6571 $ subNode = $ node ->{$ subNodeName };
66- $ variableNames = array_merge ($ variableNames , $ this ->getUsedVariables ($ subNode ));
72+ $ variableNames = array_merge ($ variableNames , $ this ->getUsedVariables ($ scope , $ subNode ));
6773 }
6874 } elseif (is_array ($ node )) {
6975 foreach ($ node as $ subNode ) {
70- $ variableNames = array_merge ($ variableNames , $ this ->getUsedVariables ($ subNode ));
76+ $ variableNames = array_merge ($ variableNames , $ this ->getUsedVariables ($ scope , $ subNode ));
7177 }
7278 }
7379
0 commit comments