diff --git a/VariableAnalysis/Lib/Helpers.php b/VariableAnalysis/Lib/Helpers.php index 50b4ce1..d793e67 100644 --- a/VariableAnalysis/Lib/Helpers.php +++ b/VariableAnalysis/Lib/Helpers.php @@ -446,18 +446,18 @@ public static function findVariableScope(File $phpcsFile, $stackPtr, $varName = } /** - * Return the variable names of each variable targetted by a `compact()` call. + * Return the variable names and positions of each variable targetted by a `compact()` call. * * @param File $phpcsFile * @param int $stackPtr * @param array> $arguments The stack pointers of each argument; see findFunctionCallArguments * - * @return string[] + * @return array each variable's firstRead position and its name; other VariableInfo properties are not set! */ - public static function getVariableNamesFromCompact(File $phpcsFile, $stackPtr, $arguments) + public static function getVariablesInsideCompact(File $phpcsFile, $stackPtr, $arguments) { $tokens = $phpcsFile->getTokens(); - $variableNames = []; + $variablePositionsAndNames = []; foreach ($arguments as $argumentPtrs) { $argumentPtrs = array_values(array_filter($argumentPtrs, function ($argumentPtr) use ($tokens) { @@ -473,7 +473,7 @@ public static function getVariableNamesFromCompact(File $phpcsFile, $stackPtr, $ if ($argumentFirstToken['code'] === T_ARRAY) { // It's an array argument, recurse. $arrayArguments = Helpers::findFunctionCallArguments($phpcsFile, $argumentPtrs[0]); - $variableNames = array_merge($variableNames, self::getVariableNamesFromCompact($phpcsFile, $stackPtr, $arrayArguments)); + $variablePositionsAndNames = array_merge($variablePositionsAndNames, self::getVariablesInsideCompact($phpcsFile, $stackPtr, $arrayArguments)); continue; } if (count($argumentPtrs) > 1) { @@ -484,7 +484,9 @@ public static function getVariableNamesFromCompact(File $phpcsFile, $stackPtr, $ // Single-quoted string literal, ie compact('whatever'). // Substr is to strip the enclosing single-quotes. $varName = substr($argumentFirstToken['content'], 1, -1); - $variableNames[] = $varName; + $variable = new VariableInfo($varName); + $variable->firstRead = $argumentPtrs[0]; + $variablePositionsAndNames[] = $variable; continue; } if ($argumentFirstToken['code'] === T_DOUBLE_QUOTED_STRING) { @@ -496,11 +498,13 @@ public static function getVariableNamesFromCompact(File $phpcsFile, $stackPtr, $ } // Substr is to strip the enclosing double-quotes. $varName = substr($argumentFirstToken['content'], 1, -1); - $variableNames[] = $varName; + $variable = new VariableInfo($varName); + $variable->firstRead = $argumentPtrs[0]; + $variablePositionsAndNames[] = $variable; continue; } } - return $variableNames; + return $variablePositionsAndNames; } /** diff --git a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php index 3294530..e9c6f8a 100644 --- a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php +++ b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php @@ -1880,16 +1880,14 @@ protected function processVariableInString(File $phpcsFile, $stackPtr) protected function processCompact(File $phpcsFile, $stackPtr) { Helpers::debug("processCompact at {$stackPtr}"); - $arguments = Helpers::findFunctionCallArguments($phpcsFile, $stackPtr); - $variableNames = Helpers::getVariableNamesFromCompact($phpcsFile, $stackPtr, $arguments); - - foreach ( $variableNames as $variableName ) { - $currScope = Helpers::findVariableScope($phpcsFile, $stackPtr, $variableName); + $variables = Helpers::getVariablesInsideCompact($phpcsFile, $stackPtr, $arguments); + foreach ( $variables as $variable ) { + $currScope = Helpers::findVariableScope($phpcsFile, $stackPtr, $variable->name); if ($currScope === null) { continue; } - $this->markVariableReadAndWarnIfUndefined($phpcsFile, $variableName, $stackPtr, $currScope); + $this->markVariableReadAndWarnIfUndefined($phpcsFile, $variable->name, $variable->firstRead, $currScope); } }