Skip to content

Commit ebe02f5

Browse files
authored
Merge pull request #249 from rodrigoprimo/test-coverage-unused-function-paremeter
Generic/UnusedFunctionParameter: improve code coverage
2 parents 17a4c19 + e881193 commit ebe02f5

File tree

5 files changed

+73
-28
lines changed

5 files changed

+73
-28
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public function process(File $phpcsFile, $stackPtr)
9494

9595
$errorCode = 'Found';
9696
$implements = false;
97-
$extends = false;
9897

9998
if ($token['code'] === T_FUNCTION) {
10099
$classPtr = $phpcsFile->getCondition($stackPtr, T_CLASS);
@@ -174,18 +173,22 @@ public function process(File $phpcsFile, $stackPtr)
174173

175174
// A return statement as the first content indicates an interface method.
176175
if ($code === T_RETURN) {
177-
$tmp = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
178-
if ($tmp === false && $implements !== false) {
176+
$firstNonEmptyTokenAfterReturn = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
177+
if ($tokens[$firstNonEmptyTokenAfterReturn]['code'] === T_SEMICOLON && $implements !== false) {
179178
return;
180179
}
181180

182-
// There is a return.
183-
if ($tokens[$tmp]['code'] === T_SEMICOLON && $implements !== false) {
184-
return;
185-
}
186-
187-
$tmp = $phpcsFile->findNext(Tokens::$emptyTokens, ($tmp + 1), null, true);
188-
if ($tmp !== false && $tokens[$tmp]['code'] === T_SEMICOLON && $implements !== false) {
181+
$secondNonEmptyTokenAfterReturn = $phpcsFile->findNext(
182+
Tokens::$emptyTokens,
183+
($firstNonEmptyTokenAfterReturn + 1),
184+
null,
185+
true
186+
);
187+
188+
if ($secondNonEmptyTokenAfterReturn !== false
189+
&& $tokens[$secondNonEmptyTokenAfterReturn]['code'] === T_SEMICOLON
190+
&& $implements !== false
191+
) {
189192
// There is a return <token>.
190193
return;
191194
}

src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.inc renamed to src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.1.inc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,26 @@ class MagicMethodsWithParamsNotDictatedByPHPInChildClass extends SomeParent{
249249
$this->foo = $foo;
250250
}
251251
}
252+
253+
/**
254+
* Methods that throw an exception or return on the first line and are part
255+
* of a class that implements an interface should not trigger the sniff.
256+
*/
257+
class InterfaceMethodNotImplement implements SomeInterface {
258+
public function notImplemented($param) {
259+
throw new Exception('Not implemented.');
260+
}
261+
262+
public function notImplemented2($param) {
263+
return 'Not implemented.';
264+
}
265+
}
266+
267+
/**
268+
* Should trigger the sniff as this method is not part of an interface.
269+
*/
270+
class MethodThrowsException {
271+
public function throwsException($param) {
272+
throw new Exception();
273+
}
274+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// Intentional parse error (missing opening parenthesis). Testing that the sniff is *not* triggered
4+
// in this case.
5+
function syntaxError
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// Intentional parse error (missing opening bracket). Testing that the sniff is *not* triggered
4+
// in this case.
5+
function syntaxError()

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,36 @@ public function getErrorList()
4141
* The key of the array should represent the line number and the value
4242
* should represent the number of warnings that should occur on that line.
4343
*
44+
* @param string $testFile The name of the file being tested.
45+
*
4446
* @return array<int, int>
4547
*/
46-
public function getWarningList()
48+
public function getWarningList($testFile='')
4749
{
48-
return [
49-
3 => 1,
50-
7 => 1,
51-
78 => 1,
52-
94 => 1,
53-
100 => 1,
54-
106 => 1,
55-
117 => 1,
56-
121 => 2,
57-
125 => 2,
58-
163 => 1,
59-
172 => 1,
60-
228 => 2,
61-
232 => 2,
62-
244 => 2,
63-
248 => 2,
64-
];
50+
switch ($testFile) {
51+
case 'UnusedFunctionParameterUnitTest.1.inc':
52+
return [
53+
3 => 1,
54+
7 => 1,
55+
78 => 1,
56+
94 => 1,
57+
100 => 1,
58+
106 => 1,
59+
117 => 1,
60+
121 => 2,
61+
125 => 2,
62+
163 => 1,
63+
172 => 1,
64+
228 => 2,
65+
232 => 2,
66+
244 => 2,
67+
248 => 2,
68+
271 => 1,
69+
];
70+
71+
default:
72+
return [];
73+
}//end switch
6574

6675
}//end getWarningList()
6776

0 commit comments

Comments
 (0)