Skip to content

Commit 80301b0

Browse files
committed
StaticClosureSniff: Fixed false positive
1 parent eaa0c35 commit 80301b0

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

SlevomatCodingStandard/Helpers/VariableHelper.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private static function isUsedInScopeInternal(File $phpcsFile, int $scopeOwnerPo
6969

7070
if (
7171
in_array($tokens[$i]['code'], [T_DOUBLE_QUOTED_STRING, T_HEREDOC], true)
72-
&& self::isUsedInScopeInString($phpcsFile, $variablePointer, $i)
72+
&& self::isUsedInScopeInString($phpcsFile, $tokens[$variablePointer]['content'], $i)
7373
) {
7474
return true;
7575
}
@@ -118,13 +118,12 @@ public static function isUsedInCompactFunction(File $phpcsFile, int $variablePoi
118118
return false;
119119
}
120120

121-
public static function isUsedInScopeInString(File $phpcsFile, int $variablePointer, int $stringPointer): bool
121+
public static function isUsedInScopeInString(File $phpcsFile, string $variableName, int $stringPointer): bool
122122
{
123123
$tokens = $phpcsFile->getTokens();
124124

125125
$stringContent = $tokens[$stringPointer]['content'];
126126

127-
$variableName = $tokens[$variablePointer]['content'];
128127
if (preg_match('~(\\\\)?(' . preg_quote($variableName, '~') . ')\b~', $stringContent, $matches) !== 0) {
129128
if ($matches[1] === '') {
130129
return true;

SlevomatCodingStandard/Sniffs/Functions/StaticClosureSniff.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
77
use SlevomatCodingStandard\Helpers\TokenHelper;
8+
use SlevomatCodingStandard\Helpers\VariableHelper;
89
use const T_CLOSURE;
10+
use const T_DOUBLE_QUOTED_STRING;
911
use const T_FN;
1012
use const T_OPEN_PARENTHESIS;
1113
use const T_PARENT;
@@ -58,6 +60,13 @@ public function process(File $phpcsFile, $closurePointer): void
5860
return;
5961
}
6062

63+
$stringPointers = TokenHelper::findNextAll($phpcsFile, T_DOUBLE_QUOTED_STRING, $tokens[$closurePointer]['scope_opener'] + 1, $tokens[$closurePointer]['scope_closer']);
64+
foreach ($stringPointers as $stringPointer) {
65+
if (VariableHelper::isUsedInScopeInString($phpcsFile, '$this', $stringPointer)) {
66+
return;
67+
}
68+
}
69+
6170
$parentPointer = TokenHelper::findNext($phpcsFile, T_PARENT, $tokens[$closurePointer]['scope_opener'] + 1, $tokens[$closurePointer]['scope_closer']);
6271
if ($parentPointer !== null) {
6372
return;

SlevomatCodingStandard/Sniffs/Variables/UnusedVariableSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ private function isUsedInLoop(File $phpcsFile, int $variablePointer, string $var
362362
for ($i = $tokens[$loopPointer]['scope_opener'] + 1; $i < $tokens[$loopPointer]['scope_closer']; $i++) {
363363
if (
364364
in_array($tokens[$i]['code'], [T_DOUBLE_QUOTED_STRING, T_HEREDOC], true)
365-
&& VariableHelper::isUsedInScopeInString($phpcsFile, $variablePointer, $i)
365+
&& VariableHelper::isUsedInScopeInString($phpcsFile, $variableName, $i)
366366
) {
367367
return true;
368368
}

tests/Sniffs/Functions/data/staticClosureNoErrors.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ public function withParent()
3030
}, []);
3131
}
3232

33+
public function withThisInString()
34+
{
35+
return array_map(function ($i) {
36+
return "{$this->anything}{$i}";
37+
}, []);
38+
39+
}
3340
}
3441

3542
Closure::bind(function ($instance, $value) {

0 commit comments

Comments
 (0)