Skip to content

Commit 9978172

Browse files
committed
UselessVariableSniff: Fixed false positive
1 parent cadf1fd commit 9978172

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

SlevomatCodingStandard/Sniffs/Variables/UselessVariableSniff.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ public function process(File $phpcsFile, $returnPointer): void
7777

7878
$variableName = $tokens[$variablePointer]['content'];
7979

80-
$previousVariablePointer = $this->findPreviousVariablePointer($phpcsFile, $returnPointer, $variableName);
81-
if ($previousVariablePointer === null) {
80+
if ($this->isStaticVariable($phpcsFile, $variablePointer, $variableName)) {
8281
return;
8382
}
8483

85-
if (!$this->isAssigmentToVariable($phpcsFile, $previousVariablePointer)) {
84+
$previousVariablePointer = $this->findPreviousVariablePointer($phpcsFile, $returnPointer, $variableName);
85+
if ($previousVariablePointer === null) {
8686
return;
8787
}
8888

89-
if ($this->isStaticVariable($phpcsFile, $previousVariablePointer)) {
89+
if (!$this->isAssigmentToVariable($phpcsFile, $previousVariablePointer)) {
9090
return;
9191
}
9292

@@ -217,10 +217,37 @@ private function isAssigmentToVariable(File $phpcsFile, int $pointer): bool
217217
], true);
218218
}
219219

220-
private function isStaticVariable(File $phpcsFile, int $variablePointer): bool
220+
private function isStaticVariable(File $phpcsFile, int $variablePointer, string $variableName): bool
221221
{
222-
$pointerBeforeVariable = TokenHelper::findPreviousEffective($phpcsFile, $variablePointer - 1);
223-
return $phpcsFile->getTokens()[$pointerBeforeVariable]['code'] === T_STATIC;
222+
$tokens = $phpcsFile->getTokens();
223+
224+
$functionPointer = null;
225+
foreach (array_reverse($tokens[$variablePointer]['conditions'], true) as $conditionPointer => $conditionTokenCode) {
226+
if (in_array($conditionTokenCode, TokenHelper::$functionTokenCodes, true)) {
227+
$functionPointer = $conditionPointer;
228+
break;
229+
}
230+
}
231+
232+
if ($functionPointer === null) {
233+
return false;
234+
}
235+
236+
for ($i = $tokens[$functionPointer]['scope_opener'] + 1; $i < $variablePointer; $i++) {
237+
if ($tokens[$i]['code'] !== T_VARIABLE) {
238+
continue;
239+
}
240+
if ($tokens[$i]['content'] !== $variableName) {
241+
continue;
242+
}
243+
244+
$pointerBeforeParameter = TokenHelper::findPreviousEffective($phpcsFile, $i - 1);
245+
if ($tokens[$pointerBeforeParameter]['code'] === T_STATIC) {
246+
return true;
247+
}
248+
}
249+
250+
return false;
224251
}
225252

226253
private function hasVariableVarAnnotation(File $phpcsFile, int $variablePointer): bool

tests/Sniffs/Variables/data/uselessVariableNoErrors.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ function staticVariable() {
4242
return $g;
4343
}
4444

45+
function uglyStaticVariable() {
46+
static $g;
47+
48+
if (is_string($g)) {
49+
return $g;
50+
}
51+
52+
$g = 'g';
53+
54+
return $g;
55+
}
56+
4557
function withDocComment() {
4658
/** @var string $h */
4759
$h = 'h';

0 commit comments

Comments
 (0)