Skip to content

Commit

Permalink
Also track position of compact variables
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Dec 1, 2024
1 parent 78f099a commit 445ed17
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
20 changes: 12 additions & 8 deletions VariableAnalysis/Lib/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, array<int>> $arguments The stack pointers of each argument; see findFunctionCallArguments
*
* @return string[]
* @return array<VariableInfo> 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) {
Expand All @@ -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]);

Check failure on line 475 in VariableAnalysis/Lib/Helpers.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Must use "self::" for local static member reference
$variableNames = array_merge($variableNames, self::getVariableNamesFromCompact($phpcsFile, $stackPtr, $arrayArguments));
$variablePositionsAndNames = array_merge($variablePositionsAndNames, self::getVariablesInsideCompact($phpcsFile, $stackPtr, $arrayArguments));
continue;
}
if (count($argumentPtrs) > 1) {
Expand All @@ -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) {
Expand All @@ -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;
}

/**
Expand Down
10 changes: 4 additions & 6 deletions VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Check failure on line 1885 in VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Expected 0 spaces after opening bracket; 1 found

Check failure on line 1885 in VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

View workflow job for this annotation

GitHub Actions / Basic CS and QA checks

Expected 0 spaces before closing bracket; 1 found
$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);

Check failure on line 1890 in VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

View workflow job for this annotation

GitHub Actions / PHPStan & Psalm

PossiblyNullArgument

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php:1890:75: PossiblyNullArgument: Argument 3 of VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff::markVariableReadAndWarnIfUndefined cannot be null, possibly null value provided (see https://psalm.dev/078)
}
}

Expand Down

0 comments on commit 445ed17

Please sign in to comment.