Skip to content

Commit

Permalink
Merge branch 'ignore-type-hints' of https://github.com/o5/PHP_CodeSni…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Jan 18, 2021
2 parents 0aeb095 + d8337c5 commit 2252d17
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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;
}

0 comments on commit 2252d17

Please sign in to comment.