Skip to content

Commit

Permalink
SlevomatCodingStandard.Variables.UselessVariable: Fixed false positives
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Sep 21, 2022
1 parent 2695d1c commit a52720f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
51 changes: 27 additions & 24 deletions SlevomatCodingStandard/Sniffs/Variables/UselessVariableSniff.php
Expand Up @@ -6,7 +6,6 @@
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\ScopeHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function array_key_exists;
use function array_keys;
use function array_reverse;
use function count;
Expand All @@ -24,12 +23,13 @@
use const T_DOUBLE_COLON;
use const T_ELSEIF;
use const T_EQUAL;
use const T_FOR;
use const T_FOREACH;
use const T_IF;
use const T_MINUS_EQUAL;
use const T_MOD_EQUAL;
use const T_MUL_EQUAL;
use const T_OPEN_CURLY_BRACKET;
use const T_OPEN_PARENTHESIS;
use const T_OR_EQUAL;
use const T_PLUS_EQUAL;
use const T_POW_EQUAL;
Expand All @@ -38,6 +38,7 @@
use const T_SL_EQUAL;
use const T_SR_EQUAL;
use const T_STATIC;
use const T_SWITCH;
use const T_VARIABLE;
use const T_WHILE;
use const T_WHITESPACE;
Expand Down Expand Up @@ -104,6 +105,10 @@ public function process(File $phpcsFile, $returnPointer): void
return;
}

if ($this->isAssignedInControlStructure($phpcsFile, $previousVariablePointer)) {
return;
}

if ($this->hasVariableVarAnnotation($phpcsFile, $previousVariablePointer)) {
return;
}
Expand All @@ -122,28 +127,6 @@ public function process(File $phpcsFile, $returnPointer): void
self::CODE_USELESS_VARIABLE,
];

$searchBefore = $previousVariablePointer;
do {
$previousOpenParenthesisPointer = TokenHelper::findPrevious($phpcsFile, T_OPEN_PARENTHESIS, $searchBefore - 1);

if (
$previousOpenParenthesisPointer === null
|| $tokens[$previousOpenParenthesisPointer]['parenthesis_closer'] < $previousVariablePointer
) {
break;
}

if (
array_key_exists('parenthesis_owner', $tokens[$previousOpenParenthesisPointer])
&& in_array($tokens[$tokens[$previousOpenParenthesisPointer]['parenthesis_owner']]['code'], [T_IF, T_ELSEIF, T_WHILE], true)
) {
return;
}

$searchBefore = $previousOpenParenthesisPointer;

} while (true);

$pointerBeforePreviousVariable = TokenHelper::findPreviousEffective($phpcsFile, $previousVariablePointer - 1);

if (
Expand Down Expand Up @@ -234,6 +217,26 @@ private function findPreviousVariablePointer(File $phpcsFile, int $pointer, stri
return null;
}

private function isAssignedInControlStructure(File $phpcsFile, int $pointer): bool
{
$controlStructure = TokenHelper::findPrevious($phpcsFile, [
T_WHILE,
T_FOR,
T_FOREACH,
T_SWITCH,
T_IF,
T_ELSEIF,
], $pointer - 1);

if ($controlStructure === null) {
return false;
}

$tokens = $phpcsFile->getTokens();

return $tokens[$controlStructure]['parenthesis_opener'] < $pointer && $pointer < $tokens[$controlStructure]['parenthesis_closer'];
}

private function isAssigmentToVariable(File $phpcsFile, int $pointer): bool
{
$assigmentPointer = TokenHelper::findNextEffective($phpcsFile, $pointer + 1);
Expand Down
5 changes: 5 additions & 0 deletions tests/Sniffs/Variables/data/uselessVariableNoErrors.php
Expand Up @@ -186,3 +186,8 @@ function foo( $bar ) {
$bar .= ' more bar';
return $bar;
}

function boo($backtrace) {
for ($class = $backtrace[$frame]['class']; ($parent = get_parent_class($class)) !== false; $class = $parent);
return $class;
}

0 comments on commit a52720f

Please sign in to comment.