Skip to content

Commit 2252d17

Browse files
committed
Merge branch 'ignore-type-hints' of https://github.com/o5/PHP_CodeSniffer
2 parents 0aeb095 + d8337c5 commit 2252d17

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
class UnusedFunctionParameterSniff implements Sniff
2424
{
2525

26+
/**
27+
* The list of class type hints which will be ignored.
28+
*
29+
* @var array
30+
*/
31+
public $ignoreTypeHints = [];
32+
2633

2734
/**
2835
* Returns an array of tokens this test wants to listen for.
@@ -191,6 +198,10 @@ public function process(File $phpcsFile, $stackPtr)
191198
// If there is only one parameter and it is unused, no need for additional errorcode toggling logic.
192199
if ($methodParamsCount === 1) {
193200
foreach ($params as $paramName => $position) {
201+
if (in_array($methodParams[0]['type_hint'], $this->ignoreTypeHints, true) === true) {
202+
continue;
203+
}
204+
194205
$data = [$paramName];
195206
$phpcsFile->addWarning($error, $position, $errorCode, $data);
196207
}
@@ -207,6 +218,7 @@ public function process(File $phpcsFile, $stackPtr)
207218
$errorInfo[$methodParams[$i]['name']] = [
208219
'position' => $params[$methodParams[$i]['name']],
209220
'errorcode' => $errorCode.'BeforeLastUsed',
221+
'typehint' => $methodParams[$i]['type_hint'],
210222
];
211223
}
212224
} else {
@@ -216,14 +228,19 @@ public function process(File $phpcsFile, $stackPtr)
216228
$errorInfo[$methodParams[$i]['name']] = [
217229
'position' => $params[$methodParams[$i]['name']],
218230
'errorcode' => $errorCode.'AfterLastUsed',
231+
'typehint' => $methodParams[$i]['type_hint'],
219232
];
220233
}
221234
}
222-
}
235+
}//end for
223236

224237
if (count($errorInfo) > 0) {
225238
$errorInfo = array_reverse($errorInfo);
226239
foreach ($errorInfo as $paramName => $info) {
240+
if (in_array($info['typehint'], $this->ignoreTypeHints, true) === true) {
241+
continue;
242+
}
243+
227244
$data = [$paramName];
228245
$phpcsFile->addWarning($error, $info['position'], $info['errorcode'], $data);
229246
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,17 @@ function myCallback($a, $b, $c, $d) {
123123
}
124124

125125
fn ($a, $b, $c) => $b;
126+
127+
// phpcs:set Generic.CodeAnalysis.UnusedFunctionParameter ignoreTypeHints[] Exception
128+
129+
function oneParam(Exception $foo) {
130+
return 'foobar';
131+
}
132+
133+
function moreParamFirst(Exception $foo, LogicException $bar) {
134+
return 'foobar' . $bar;
135+
}
136+
137+
function moreParamSecond(LogicException $bar, Exception $foo) {
138+
return 'foobar' . $bar;
139+
}

0 commit comments

Comments
 (0)