Skip to content

Commit 77f3c12

Browse files
committed
UnusedVariableSniff: Fixed false positive
1 parent 68fe9fa commit 77f3c12

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

SlevomatCodingStandard/Sniffs/Variables/UnusedVariableSniff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ private function isUsedInString(File $phpcsFile, int $functionPointer, string $v
453453
return true;
454454
}
455455

456+
$variableNameWithoutDollar = substr($variableName, 1);
457+
if (preg_match('~\$\{' . preg_quote($variableNameWithoutDollar, '~') . '\}~', $tokens[$stringPointer]['content'])) {
458+
return true;
459+
}
460+
456461
$currentPointer = $stringPointer + 1;
457462
} while (true);
458463

tests/Sniffs/Variables/data/unusedVariableNoErrors.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,25 @@ function () {
119119
echo "$a";
120120
};
121121

122+
function () {
123+
$a = '';
124+
echo "${a}";
125+
};
126+
122127
function () {
123128
$a = '';
124129
echo <<<TEXT
125130
$a
126131
TEXT;
127132
};
128133

134+
function () {
135+
$a = '';
136+
echo <<<TEXT
137+
${a}
138+
TEXT;
139+
};
140+
129141
function () {
130142
$a = 10;
131143
max(1, $a += 10);

0 commit comments

Comments
 (0)