Skip to content

Commit f5dec4c

Browse files
committed
Generic/UnusedFunctionParameter: ignore class context for closures/arrow functions
As things were, if a closure or arrow function declared within a class which extends or implements would have an unused function parameter, it would get the `InExtendedClass` or `InImplementedInterface` addition in the error code. Those additions were only intended for function declarations where the declaration would potentially be overloading a method from a parent and would have to comply with the method signature of the method in the parent class/interface. This could lead to underreporting if a standard explicitly excludes the error codes contain `InExtendedClass` and/or `InImplementedInterface`. Fixed now. Includes additional unit test, though the tests don't safeguard this much as they don't check the error codes of the messages thrown. The change can be tested manually by running the new tests against `master`, which will show: ``` 163 | WARNING | The method parameter $d is never used (Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed) 172 | WARNING | The method parameter $d is never used | | (Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed) ``` ... while with the change in this commit, this will be fixed to: ``` 163 | WARNING | The method parameter $d is never used (Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed) 172 | WARNING | The method parameter $d is never used (Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed) ```
1 parent 4c2ba65 commit f5dec4c

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ The file documents changes to the PHP_CodeSniffer project.
114114
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
115115
- Fixed bug #3557 : Squiz.Arrays.ArrayDeclaration will now ignore PHP 7.4 array unpacking when determining whether an array is associative
116116
- Thanks to Volker Dusch (@edorian) for the patch
117+
- Fixed bug #3715 : Generic/UnusedFunctionParameter: fixed incorrect errorcode for closures/arrow functions nested within extended classes/classes which implement.
118+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
117119
- Fixed bug #3717 : Squiz.Commenting.FunctionComment: fixed false positive for InvalidNoReturn when type is never
118120
- Thanks to Choraimy Kroonstuiver (@axlon) for the patch
119121
- Fixed bug #3720 : Generic/RequireStrictTypes : will now bow out silently in case of parse errors/live coding instead of throwing false positives/false negatives

src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,17 @@ public function process(File $phpcsFile, $stackPtr)
6969
$errorCode = 'Found';
7070
$implements = false;
7171
$extends = false;
72-
$classPtr = $phpcsFile->getCondition($stackPtr, T_CLASS);
73-
if ($classPtr !== false) {
74-
$implements = $phpcsFile->findImplementedInterfaceNames($classPtr);
75-
$extends = $phpcsFile->findExtendedClassName($classPtr);
76-
if ($extends !== false) {
77-
$errorCode .= 'InExtendedClass';
78-
} else if ($implements !== false) {
79-
$errorCode .= 'InImplementedInterface';
72+
73+
if ($token['code'] === T_FUNCTION) {
74+
$classPtr = $phpcsFile->getCondition($stackPtr, T_CLASS);
75+
if ($classPtr !== false) {
76+
$implements = $phpcsFile->findImplementedInterfaceNames($classPtr);
77+
$extends = $phpcsFile->findExtendedClassName($classPtr);
78+
if ($extends !== false) {
79+
$errorCode .= 'InExtendedClass';
80+
} else if ($implements !== false) {
81+
$errorCode .= 'InImplementedInterface';
82+
}
8083
}
8184
}
8285

src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.inc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,23 @@ class ConstructorPropertyPromotionWithContentInMethod {
152152
}
153153

154154
$found = in_array_cb($needle, $haystack, fn($array, $needle) => $array[2] === $needle);
155+
156+
157+
/*
158+
* Don't adjust the error code for closures and arrow functions in extended classes/classes implementing interfaces.
159+
*/
160+
class MyExtendedClass extends SomeClass {
161+
public function something($a, $b) {
162+
$c = $a + $b;
163+
$closure = function ($c, $d) {
164+
return $c * 2;
165+
};
166+
}
167+
}
168+
169+
class MyExtendedClass implements SomeInterface {
170+
public function something($a, $b) {
171+
$c = $a + $b;
172+
$fn = fn($c, $d) => $c[2];
173+
}
174+
}

src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public function getWarningList()
5050
117 => 1,
5151
121 => 2,
5252
125 => 2,
53+
163 => 1,
54+
172 => 1,
5355
];
5456

5557
}//end getWarningList()

0 commit comments

Comments
 (0)