Skip to content

Commit a52720f

Browse files
committed
SlevomatCodingStandard.Variables.UselessVariable: Fixed false positives
1 parent 2695d1c commit a52720f

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

SlevomatCodingStandard/Sniffs/Variables/UselessVariableSniff.php

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHP_CodeSniffer\Sniffs\Sniff;
77
use SlevomatCodingStandard\Helpers\ScopeHelper;
88
use SlevomatCodingStandard\Helpers\TokenHelper;
9-
use function array_key_exists;
109
use function array_keys;
1110
use function array_reverse;
1211
use function count;
@@ -24,12 +23,13 @@
2423
use const T_DOUBLE_COLON;
2524
use const T_ELSEIF;
2625
use const T_EQUAL;
26+
use const T_FOR;
27+
use const T_FOREACH;
2728
use const T_IF;
2829
use const T_MINUS_EQUAL;
2930
use const T_MOD_EQUAL;
3031
use const T_MUL_EQUAL;
3132
use const T_OPEN_CURLY_BRACKET;
32-
use const T_OPEN_PARENTHESIS;
3333
use const T_OR_EQUAL;
3434
use const T_PLUS_EQUAL;
3535
use const T_POW_EQUAL;
@@ -38,6 +38,7 @@
3838
use const T_SL_EQUAL;
3939
use const T_SR_EQUAL;
4040
use const T_STATIC;
41+
use const T_SWITCH;
4142
use const T_VARIABLE;
4243
use const T_WHILE;
4344
use const T_WHITESPACE;
@@ -104,6 +105,10 @@ public function process(File $phpcsFile, $returnPointer): void
104105
return;
105106
}
106107

108+
if ($this->isAssignedInControlStructure($phpcsFile, $previousVariablePointer)) {
109+
return;
110+
}
111+
107112
if ($this->hasVariableVarAnnotation($phpcsFile, $previousVariablePointer)) {
108113
return;
109114
}
@@ -122,28 +127,6 @@ public function process(File $phpcsFile, $returnPointer): void
122127
self::CODE_USELESS_VARIABLE,
123128
];
124129

125-
$searchBefore = $previousVariablePointer;
126-
do {
127-
$previousOpenParenthesisPointer = TokenHelper::findPrevious($phpcsFile, T_OPEN_PARENTHESIS, $searchBefore - 1);
128-
129-
if (
130-
$previousOpenParenthesisPointer === null
131-
|| $tokens[$previousOpenParenthesisPointer]['parenthesis_closer'] < $previousVariablePointer
132-
) {
133-
break;
134-
}
135-
136-
if (
137-
array_key_exists('parenthesis_owner', $tokens[$previousOpenParenthesisPointer])
138-
&& in_array($tokens[$tokens[$previousOpenParenthesisPointer]['parenthesis_owner']]['code'], [T_IF, T_ELSEIF, T_WHILE], true)
139-
) {
140-
return;
141-
}
142-
143-
$searchBefore = $previousOpenParenthesisPointer;
144-
145-
} while (true);
146-
147130
$pointerBeforePreviousVariable = TokenHelper::findPreviousEffective($phpcsFile, $previousVariablePointer - 1);
148131

149132
if (
@@ -234,6 +217,26 @@ private function findPreviousVariablePointer(File $phpcsFile, int $pointer, stri
234217
return null;
235218
}
236219

220+
private function isAssignedInControlStructure(File $phpcsFile, int $pointer): bool
221+
{
222+
$controlStructure = TokenHelper::findPrevious($phpcsFile, [
223+
T_WHILE,
224+
T_FOR,
225+
T_FOREACH,
226+
T_SWITCH,
227+
T_IF,
228+
T_ELSEIF,
229+
], $pointer - 1);
230+
231+
if ($controlStructure === null) {
232+
return false;
233+
}
234+
235+
$tokens = $phpcsFile->getTokens();
236+
237+
return $tokens[$controlStructure]['parenthesis_opener'] < $pointer && $pointer < $tokens[$controlStructure]['parenthesis_closer'];
238+
}
239+
237240
private function isAssigmentToVariable(File $phpcsFile, int $pointer): bool
238241
{
239242
$assigmentPointer = TokenHelper::findNextEffective($phpcsFile, $pointer + 1);

tests/Sniffs/Variables/data/uselessVariableNoErrors.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,8 @@ function foo( $bar ) {
186186
$bar .= ' more bar';
187187
return $bar;
188188
}
189+
190+
function boo($backtrace) {
191+
for ($class = $backtrace[$frame]['class']; ($parent = get_parent_class($class)) !== false; $class = $parent);
192+
return $class;
193+
}

0 commit comments

Comments
 (0)