Skip to content
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 @@ -23,6 +23,13 @@
class UnusedFunctionParameterSniff implements Sniff
{

/**
* The list of class type hints which will be ignored.
*
* @var array
*/
public $ignoreTypeHints = [];


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -191,6 +198,10 @@ public function process(File $phpcsFile, $stackPtr)
// If there is only one parameter and it is unused, no need for additional errorcode toggling logic.
if ($methodParamsCount === 1) {
foreach ($params as $paramName => $position) {
if (in_array($methodParams[0]['type_hint'], $this->ignoreTypeHints, true) === true) {
continue;
}

$data = [$paramName];
$phpcsFile->addWarning($error, $position, $errorCode, $data);
}
Expand All @@ -207,6 +218,7 @@ public function process(File $phpcsFile, $stackPtr)
$errorInfo[$methodParams[$i]['name']] = [
'position' => $params[$methodParams[$i]['name']],
'errorcode' => $errorCode.'BeforeLastUsed',
'typehint' => $methodParams[$i]['type_hint'],
];
}
} else {
Expand All @@ -216,14 +228,19 @@ public function process(File $phpcsFile, $stackPtr)
$errorInfo[$methodParams[$i]['name']] = [
'position' => $params[$methodParams[$i]['name']],
'errorcode' => $errorCode.'AfterLastUsed',
'typehint' => $methodParams[$i]['type_hint'],
];
}
}
}
}//end for

if (count($errorInfo) > 0) {
$errorInfo = array_reverse($errorInfo);
foreach ($errorInfo as $paramName => $info) {
if (in_array($info['typehint'], $this->ignoreTypeHints, true) === true) {
continue;
}

$data = [$paramName];
$phpcsFile->addWarning($error, $info['position'], $info['errorcode'], $data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,17 @@ function myCallback($a, $b, $c, $d) {
}

fn ($a, $b, $c) => $b;

// phpcs:set Generic.CodeAnalysis.UnusedFunctionParameter ignoreTypeHints[] Exception

function oneParam(Exception $foo) {
return 'foobar';
}

function moreParamFirst(Exception $foo, LogicException $bar) {
return 'foobar' . $bar;
}

function moreParamSecond(LogicException $bar, Exception $foo) {
return 'foobar' . $bar;
}