Skip to content

Commit 360ed8f

Browse files
committed
UselessVariableSniff: Fixed false positives
1 parent 91728b6 commit 360ed8f

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

SlevomatCodingStandard/Sniffs/Variables/UselessVariableSniff.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use const T_AND_EQUAL;
1010
use const T_CONCAT_EQUAL;
1111
use const T_DIV_EQUAL;
12+
use const T_DO;
1213
use const T_DOC_COMMENT_CLOSE_TAG;
1314
use const T_ELSEIF;
1415
use const T_EQUAL;
@@ -31,6 +32,8 @@
3132
use const T_WHITESPACE;
3233
use const T_XOR_EQUAL;
3334
use function array_key_exists;
35+
use function array_keys;
36+
use function array_reverse;
3437
use function count;
3538
use function in_array;
3639
use function preg_match;
@@ -244,7 +247,7 @@ private function hasAnotherAssigmentBefore(File $phpcsFile, int $variablePointer
244247
return false;
245248
}
246249

247-
return $this->areBothPointersNearby($phpcsFile, $previousVariablePointer, $variablePointer);
250+
return $this->areBothVariablesNearby($phpcsFile, $previousVariablePointer, $variablePointer);
248251
}
249252

250253
private function areBothPointersNearby(File $phpcsFile, int $firstPointer, int $secondPointer): bool
@@ -255,6 +258,23 @@ private function areBothPointersNearby(File $phpcsFile, int $firstPointer, int $
255258
return $pointerAfterFirstVariableSemicolon === $secondPointer;
256259
}
257260

261+
private function areBothVariablesNearby(File $phpcsFile, int $firstVariablePointer, int $secondVariablePointer): bool
262+
{
263+
if ($this->areBothPointersNearby($phpcsFile, $firstVariablePointer, $secondVariablePointer)) {
264+
return true;
265+
}
266+
267+
$tokens = $phpcsFile->getTokens();
268+
269+
$lastConditionPointer = array_reverse(array_keys($tokens[$firstVariablePointer]['conditions']))[0];
270+
$lastConditionScopeCloserPointer = $tokens[$lastConditionPointer]['scope_closer'];
271+
if ($tokens[$lastConditionPointer]['code'] === T_DO) {
272+
$lastConditionScopeCloserPointer = TokenHelper::findNext($phpcsFile, T_SEMICOLON, $lastConditionScopeCloserPointer + 1);
273+
}
274+
275+
return TokenHelper::findNextEffective($phpcsFile, $lastConditionScopeCloserPointer + 1) === $secondVariablePointer;
276+
}
277+
258278
private function findSemicolon(File $phpcsFile, int $pointer): int
259279
{
260280
$tokens = $phpcsFile->getTokens();

tests/Sniffs/Variables/data/uselessVariableNoErrors.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,52 @@ function moreAssigments() {
5454
return $i;
5555
}
5656

57+
function moreAssigmentsWithIf() {
58+
$i = 'i';
59+
if (true) {
60+
$i .= 'ii';
61+
}
62+
$i .= 'ii';
63+
return $i;
64+
}
65+
66+
function moreAssigmentsWithFor($values) {
67+
$i = 'i';
68+
for ($x = 0; $x < count($values); $x++) {
69+
$i .= $values[$x];
70+
}
71+
$i .= 'ii';
72+
return $i;
73+
}
74+
75+
function moreAssigmentsWithForeach($values) {
76+
$i = 'i';
77+
foreach ($values as $value) {
78+
$i .= $value;
79+
}
80+
$i .= 'ii';
81+
return $i;
82+
}
83+
84+
function moreAssigmentsWithWhile($values) {
85+
$i = 'i';
86+
while ($value = current($values)) {
87+
$i .= $value;
88+
}
89+
$i .= 'ii';
90+
return $i;
91+
}
92+
93+
function moreAssigmentsWithDo($values) {
94+
$i = 'i';
95+
$value = current($values);
96+
do {
97+
$i .= $value;
98+
} while ($value = next($values));
99+
$i .= 'ii';
100+
return $i;
101+
}
102+
57103
function somethingBetweenAssigmentAndReturn() {
58104
$j = 'j';
59105
doSomething();

0 commit comments

Comments
 (0)