diff --git a/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php b/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php index 66fe781ac7..e076b83dce 100644 --- a/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php +++ b/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php @@ -94,7 +94,6 @@ public function process(File $phpcsFile, $stackPtr) $errorCode = 'Found'; $implements = false; - $extends = false; if ($token['code'] === T_FUNCTION) { $classPtr = $phpcsFile->getCondition($stackPtr, T_CLASS); @@ -174,18 +173,22 @@ public function process(File $phpcsFile, $stackPtr) // A return statement as the first content indicates an interface method. if ($code === T_RETURN) { - $tmp = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); - if ($tmp === false && $implements !== false) { + $firstNonEmptyTokenAfterReturn = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); + if ($tokens[$firstNonEmptyTokenAfterReturn]['code'] === T_SEMICOLON && $implements !== false) { return; } - // There is a return. - if ($tokens[$tmp]['code'] === T_SEMICOLON && $implements !== false) { - return; - } - - $tmp = $phpcsFile->findNext(Tokens::$emptyTokens, ($tmp + 1), null, true); - if ($tmp !== false && $tokens[$tmp]['code'] === T_SEMICOLON && $implements !== false) { + $secondNonEmptyTokenAfterReturn = $phpcsFile->findNext( + Tokens::$emptyTokens, + ($firstNonEmptyTokenAfterReturn + 1), + null, + true + ); + + if ($secondNonEmptyTokenAfterReturn !== false + && $tokens[$secondNonEmptyTokenAfterReturn]['code'] === T_SEMICOLON + && $implements !== false + ) { // There is a return . return; } diff --git a/src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.inc b/src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.1.inc similarity index 90% rename from src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.inc rename to src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.1.inc index 154f03157b..f13253d301 100644 --- a/src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.inc +++ b/src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.1.inc @@ -249,3 +249,26 @@ class MagicMethodsWithParamsNotDictatedByPHPInChildClass extends SomeParent{ $this->foo = $foo; } } + +/** + * Methods that throw an exception or return on the first line and are part + * of a class that implements an interface should not trigger the sniff. + */ +class InterfaceMethodNotImplement implements SomeInterface { + public function notImplemented($param) { + throw new Exception('Not implemented.'); + } + + public function notImplemented2($param) { + return 'Not implemented.'; + } +} + +/** + * Should trigger the sniff as this method is not part of an interface. + */ +class MethodThrowsException { + public function throwsException($param) { + throw new Exception(); + } +} diff --git a/src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.2.inc b/src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.2.inc new file mode 100644 index 0000000000..d39eefe305 --- /dev/null +++ b/src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.2.inc @@ -0,0 +1,5 @@ + */ - public function getWarningList() + public function getWarningList($testFile='') { - return [ - 3 => 1, - 7 => 1, - 78 => 1, - 94 => 1, - 100 => 1, - 106 => 1, - 117 => 1, - 121 => 2, - 125 => 2, - 163 => 1, - 172 => 1, - 228 => 2, - 232 => 2, - 244 => 2, - 248 => 2, - ]; + switch ($testFile) { + case 'UnusedFunctionParameterUnitTest.1.inc': + return [ + 3 => 1, + 7 => 1, + 78 => 1, + 94 => 1, + 100 => 1, + 106 => 1, + 117 => 1, + 121 => 2, + 125 => 2, + 163 => 1, + 172 => 1, + 228 => 2, + 232 => 2, + 244 => 2, + 248 => 2, + 271 => 1, + ]; + + default: + return []; + }//end switch }//end getWarningList()