Skip to content

Commit 7d5ccdd

Browse files
committed
UnusedParameterSniff: Fixed false positive
1 parent 9e6619c commit 7d5ccdd

File tree

4 files changed

+60
-14
lines changed

4 files changed

+60
-14
lines changed

SlevomatCodingStandard/Sniffs/Functions/UnusedParameterSniff.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use PHP_CodeSniffer\Util\Tokens;
78
use SlevomatCodingStandard\Helpers\FunctionHelper;
89
use SlevomatCodingStandard\Helpers\SuppressHelper;
910
use SlevomatCodingStandard\Helpers\TokenHelper;
1011
use SlevomatCodingStandard\Helpers\VariableHelper;
12+
use function array_merge;
13+
use function in_array;
1114
use function sprintf;
15+
use const T_COMMA;
1216
use const T_VARIABLE;
1317

1418
class UnusedParameterSniff implements Sniff
@@ -55,16 +59,31 @@ public function process(File $phpcsFile, $functionPointer): void
5559
break;
5660
}
5761

58-
if (!VariableHelper::isUsedInScope($phpcsFile, $functionPointer, $parameterPointer)) {
59-
if (!$isSuppressed) {
60-
$phpcsFile->addError(
61-
sprintf('Unused parameter %s.', $tokens[$parameterPointer]['content']),
62-
$parameterPointer,
63-
self::CODE_UNUSED_PARAMETER
64-
);
65-
} else {
66-
$suppressUseless = false;
67-
}
62+
$previousPointer = TokenHelper::findPrevious(
63+
$phpcsFile,
64+
array_merge([T_COMMA], Tokens::$scopeModifiers),
65+
$parameterPointer - 1,
66+
$tokens[$functionPointer]['parenthesis_opener']
67+
);
68+
69+
if ($previousPointer !== null && in_array($tokens[$previousPointer]['code'], Tokens::$scopeModifiers, true)) {
70+
$currentPointer = $parameterPointer + 1;
71+
continue;
72+
}
73+
74+
if (VariableHelper::isUsedInScope($phpcsFile, $functionPointer, $parameterPointer)) {
75+
$currentPointer = $parameterPointer + 1;
76+
continue;
77+
}
78+
79+
if (!$isSuppressed) {
80+
$phpcsFile->addError(
81+
sprintf('Unused parameter %s.', $tokens[$parameterPointer]['content']),
82+
$parameterPointer,
83+
self::CODE_UNUSED_PARAMETER
84+
);
85+
} else {
86+
$suppressUseless = false;
6887
}
6988

7089
$currentPointer = $parameterPointer + 1;

tests/Sniffs/Functions/UnusedParameterSniffTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testErrors(): void
1717
{
1818
$report = self::checkFile(__DIR__ . '/data/unusedParameterErrors.php');
1919

20-
self::assertSame(12, $report->getErrorCount());
20+
self::assertSame(13, $report->getErrorCount());
2121

2222
self::assertSniffError($report, 3, UnusedParameterSniff::CODE_UNUSED_PARAMETER, 'Unused parameter $a.');
2323
self::assertSniffError($report, 3, UnusedParameterSniff::CODE_UNUSED_PARAMETER, 'Unused parameter $b.');
@@ -30,10 +30,11 @@ public function testErrors(): void
3030
self::assertSniffError($report, 47, UnusedParameterSniff::CODE_UNUSED_PARAMETER, 'Unused parameter $a.');
3131
self::assertSniffError($report, 51, UnusedParameterSniff::CODE_UNUSED_PARAMETER, 'Unused parameter $a.');
3232
self::assertSniffError($report, 53, UnusedParameterSniff::CODE_UNUSED_PARAMETER, 'Unused parameter $a.');
33+
self::assertSniffError($report, 66, UnusedParameterSniff::CODE_UNUSED_PARAMETER, 'Unused parameter $b.');
3334

3435
self::assertSniffError(
3536
$report,
36-
63,
37+
77,
3738
UnusedParameterSniff::CODE_USELESS_SUPPRESS,
3839
'Useless @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter'
3940
);

tests/Sniffs/Functions/data/unusedParameterErrors.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php // lint >= 7.4
1+
<?php // lint >= 8.0
22

33
function unusedParameters($a, $b)
44
{
@@ -57,6 +57,20 @@ function sameNameParameterInAnotherScope($a)
5757
};
5858
}
5959

60+
61+
class PropertyPromotion
62+
{
63+
64+
public function __construct(
65+
public $a,
66+
string $b,
67+
private int $c,
68+
) {
69+
70+
}
71+
72+
}
73+
6074
/**
6175
* @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
6276
*/

tests/Sniffs/Functions/data/unusedParameterNoErrors.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php // lint >= 7.4
1+
<?php // lint >= 8.0
22

33
function noParameter()
44
{
@@ -90,3 +90,15 @@ private static function staticMethodCallViaVariable(string $methodName): ?self
9090

9191
}
9292

93+
class PropertyPromotion
94+
{
95+
96+
public function __construct(
97+
public $a,
98+
private string $b
99+
) {
100+
101+
}
102+
103+
}
104+

0 commit comments

Comments
 (0)