Skip to content

Generic/UnusedFunctionParameter: improve code coverage #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 <token>.
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

// Intentional parse error (missing opening parenthesis). Testing that the sniff is *not* triggered
// in this case.
function syntaxError
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

// Intentional parse error (missing opening bracket). Testing that the sniff is *not* triggered
// in this case.
function syntaxError()
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,36 @@ public function getErrorList()
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
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()

Expand Down