Skip to content

Commit

Permalink
Merge pull request PHPCSStandards#249 from rodrigoprimo/test-coverage…
Browse files Browse the repository at this point in the history
…-unused-function-paremeter

Generic/UnusedFunctionParameter: improve code coverage
  • Loading branch information
jrfnl committed Jan 17, 2024
2 parents 17a4c19 + e881193 commit ebe02f5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 28 deletions.
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

0 comments on commit ebe02f5

Please sign in to comment.