diff --git a/Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php b/Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php index 0d882a47..cfff73a5 100644 --- a/Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php +++ b/Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php @@ -71,3 +71,9 @@ function arrowFunctionAsExpressionWithUnusedVariableOutsideArrow($subject) { //u echo $post; // undefined variable $post; } +function arrowFunctionWithVariableUsedInsideQuotes($allowed_extensions) { + $data = array_map( fn($extension) => '.' . $extension, $allowed_extensions ); + $data = array_map( fn($extension) => ".$extension", $allowed_extensions ); + $data = array_map( fn($extension) => ".{$extension}", $allowed_extensions ); + return $data; +} diff --git a/VariableAnalysis/Lib/Helpers.php b/VariableAnalysis/Lib/Helpers.php index cce5ee5a..b9ffb1b6 100644 --- a/VariableAnalysis/Lib/Helpers.php +++ b/VariableAnalysis/Lib/Helpers.php @@ -304,12 +304,14 @@ public static function normalizeVarName($varName) { /** * @param File $phpcsFile * @param int $stackPtr + * @param string $varName (optional) if it differs from the normalized 'content' of the token at $stackPtr * * @return ?int */ - public static function findVariableScope(File $phpcsFile, $stackPtr) { + public static function findVariableScope(File $phpcsFile, $stackPtr, $varName = null) { $tokens = $phpcsFile->getTokens(); $token = $tokens[$stackPtr]; + $varName = isset($varName) ? $varName : self::normalizeVarName($token['content']); $arrowFunctionIndex = self::getContainingArrowFunctionIndex($phpcsFile, $stackPtr); $isTokenInsideArrowFunctionBody = is_int($arrowFunctionIndex); @@ -319,7 +321,8 @@ public static function findVariableScope(File $phpcsFile, $stackPtr) { // otherwise, it uses the enclosing scope. if ($arrowFunctionIndex) { $variableNames = self::getVariablesDefinedByArrowFunction($phpcsFile, $arrowFunctionIndex); - if (in_array($token['content'], $variableNames, true)) { + self::debug('findVariableScope: looking for', $varName, 'in arrow function variables', $variableNames); + if (in_array($varName, $variableNames, true)) { return $arrowFunctionIndex; } } @@ -654,9 +657,10 @@ public static function getVariablesDefinedByArrowFunction(File $phpcsFile, $stac for ($index = $arrowFunctionToken['parenthesis_opener']; $index < $arrowFunctionToken['parenthesis_closer']; $index++) { $token = $tokens[$index]; if ($token['code'] === T_VARIABLE) { - $variableNames[] = $token['content']; + $variableNames[] = self::normalizeVarName($token['content']); } } + self::debug('found these variables in arrow function token', $variableNames); return $variableNames; } diff --git a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php index 6ab2deb0..442efacf 100644 --- a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php +++ b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php @@ -1528,12 +1528,9 @@ protected function processVariableInString(File $phpcsFile, $stackPtr) { } Helpers::debug("examining token for variable in string", $token); - $currScope = Helpers::findVariableScope($phpcsFile, $stackPtr); - if ($currScope === null) { - return; - } foreach ($matches[1] as $varName) { $varName = Helpers::normalizeVarName($varName); + // Are we $this within a class? if ($this->processVariableAsThisWithinClass($phpcsFile, $stackPtr, $varName)) { continue; @@ -1548,6 +1545,11 @@ protected function processVariableInString(File $phpcsFile, $stackPtr) { continue; } + $currScope = Helpers::findVariableScope($phpcsFile, $stackPtr, $varName); + if ($currScope === null) { + continue; + } + $this->markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $stackPtr, $currScope); } }