Skip to content

Commit

Permalink
Generic/UnusedFunctionParameter: ignore class context for closures/ar…
Browse files Browse the repository at this point in the history
…row 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)
```
  • Loading branch information
jrfnl committed Dec 22, 2022
1 parent add95a7 commit 7993c81
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,17 @@ public function process(File $phpcsFile, $stackPtr)
$errorCode = 'Found';
$implements = false;
$extends = false;
$classPtr = $phpcsFile->getCondition($stackPtr, T_CLASS);
if ($classPtr !== false) {
$implements = $phpcsFile->findImplementedInterfaceNames($classPtr);
$extends = $phpcsFile->findExtendedClassName($classPtr);
if ($extends !== false) {
$errorCode .= 'InExtendedClass';
} else if ($implements !== false) {
$errorCode .= 'InImplementedInterface';

if ($token['code'] === T_FUNCTION) {
$classPtr = $phpcsFile->getCondition($stackPtr, T_CLASS);
if ($classPtr !== false) {
$implements = $phpcsFile->findImplementedInterfaceNames($classPtr);
$extends = $phpcsFile->findExtendedClassName($classPtr);
if ($extends !== false) {
$errorCode .= 'InExtendedClass';
} else if ($implements !== false) {
$errorCode .= 'InImplementedInterface';
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,23 @@ class ConstructorPropertyPromotionWithContentInMethod {
}

$found = in_array_cb($needle, $haystack, fn($array, $needle) => $array[2] === $needle);


/*
* Don't adjust the error code for closures and arrow functions in extended classes/classes implementing interfaces.
*/
class MyExtendedClass extends SomeClass {
public function something($a, $b) {
$c = $a + $b;
$closure = function ($c, $d) {
return $c * 2;
};
}
}

class MyExtendedClass implements SomeInterface {
public function something($a, $b) {
$c = $a + $b;
$fn = fn($c, $d) => $c[2];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public function getWarningList()
117 => 1,
121 => 2,
125 => 2,
163 => 1,
172 => 1,
];

}//end getWarningList()
Expand Down

0 comments on commit 7993c81

Please sign in to comment.