Skip to content

Commit 6a0b28f

Browse files
authored
Properly detect variables used in quotes inside arrow functions (#222)
* Add test for variables used in arrow functions within quotes * Allow passing a different varName to findVariableScope * Pass normalized varName to scope detection in processVariableInString * Replace null coalescing operator
1 parent df30329 commit 6a0b28f

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php

+6
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,9 @@ function arrowFunctionAsExpressionWithUnusedVariableOutsideArrow($subject) { //u
7171
echo $post; // undefined variable $post;
7272
}
7373

74+
function arrowFunctionWithVariableUsedInsideQuotes($allowed_extensions) {
75+
$data = array_map( fn($extension) => '.' . $extension, $allowed_extensions );
76+
$data = array_map( fn($extension) => ".$extension", $allowed_extensions );
77+
$data = array_map( fn($extension) => ".{$extension}", $allowed_extensions );
78+
return $data;
79+
}

VariableAnalysis/Lib/Helpers.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,14 @@ public static function normalizeVarName($varName) {
304304
/**
305305
* @param File $phpcsFile
306306
* @param int $stackPtr
307+
* @param string $varName (optional) if it differs from the normalized 'content' of the token at $stackPtr
307308
*
308309
* @return ?int
309310
*/
310-
public static function findVariableScope(File $phpcsFile, $stackPtr) {
311+
public static function findVariableScope(File $phpcsFile, $stackPtr, $varName = null) {
311312
$tokens = $phpcsFile->getTokens();
312313
$token = $tokens[$stackPtr];
314+
$varName = isset($varName) ? $varName : self::normalizeVarName($token['content']);
313315

314316
$arrowFunctionIndex = self::getContainingArrowFunctionIndex($phpcsFile, $stackPtr);
315317
$isTokenInsideArrowFunctionBody = is_int($arrowFunctionIndex);
@@ -319,7 +321,8 @@ public static function findVariableScope(File $phpcsFile, $stackPtr) {
319321
// otherwise, it uses the enclosing scope.
320322
if ($arrowFunctionIndex) {
321323
$variableNames = self::getVariablesDefinedByArrowFunction($phpcsFile, $arrowFunctionIndex);
322-
if (in_array($token['content'], $variableNames, true)) {
324+
self::debug('findVariableScope: looking for', $varName, 'in arrow function variables', $variableNames);
325+
if (in_array($varName, $variableNames, true)) {
323326
return $arrowFunctionIndex;
324327
}
325328
}
@@ -654,9 +657,10 @@ public static function getVariablesDefinedByArrowFunction(File $phpcsFile, $stac
654657
for ($index = $arrowFunctionToken['parenthesis_opener']; $index < $arrowFunctionToken['parenthesis_closer']; $index++) {
655658
$token = $tokens[$index];
656659
if ($token['code'] === T_VARIABLE) {
657-
$variableNames[] = $token['content'];
660+
$variableNames[] = self::normalizeVarName($token['content']);
658661
}
659662
}
663+
self::debug('found these variables in arrow function token', $variableNames);
660664
return $variableNames;
661665
}
662666

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -1528,12 +1528,9 @@ protected function processVariableInString(File $phpcsFile, $stackPtr) {
15281528
}
15291529
Helpers::debug("examining token for variable in string", $token);
15301530

1531-
$currScope = Helpers::findVariableScope($phpcsFile, $stackPtr);
1532-
if ($currScope === null) {
1533-
return;
1534-
}
15351531
foreach ($matches[1] as $varName) {
15361532
$varName = Helpers::normalizeVarName($varName);
1533+
15371534
// Are we $this within a class?
15381535
if ($this->processVariableAsThisWithinClass($phpcsFile, $stackPtr, $varName)) {
15391536
continue;
@@ -1548,6 +1545,11 @@ protected function processVariableInString(File $phpcsFile, $stackPtr) {
15481545
continue;
15491546
}
15501547

1548+
$currScope = Helpers::findVariableScope($phpcsFile, $stackPtr, $varName);
1549+
if ($currScope === null) {
1550+
continue;
1551+
}
1552+
15511553
$this->markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $stackPtr, $currScope);
15521554
}
15531555
}

0 commit comments

Comments
 (0)