Skip to content

Commit 445ed17

Browse files
committed
Also track position of compact variables
1 parent 78f099a commit 445ed17

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

VariableAnalysis/Lib/Helpers.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -446,18 +446,18 @@ public static function findVariableScope(File $phpcsFile, $stackPtr, $varName =
446446
}
447447

448448
/**
449-
* Return the variable names of each variable targetted by a `compact()` call.
449+
* Return the variable names and positions of each variable targetted by a `compact()` call.
450450
*
451451
* @param File $phpcsFile
452452
* @param int $stackPtr
453453
* @param array<int, array<int>> $arguments The stack pointers of each argument; see findFunctionCallArguments
454454
*
455-
* @return string[]
455+
* @return array<VariableInfo> each variable's firstRead position and its name; other VariableInfo properties are not set!
456456
*/
457-
public static function getVariableNamesFromCompact(File $phpcsFile, $stackPtr, $arguments)
457+
public static function getVariablesInsideCompact(File $phpcsFile, $stackPtr, $arguments)
458458
{
459459
$tokens = $phpcsFile->getTokens();
460-
$variableNames = [];
460+
$variablePositionsAndNames = [];
461461

462462
foreach ($arguments as $argumentPtrs) {
463463
$argumentPtrs = array_values(array_filter($argumentPtrs, function ($argumentPtr) use ($tokens) {
@@ -473,7 +473,7 @@ public static function getVariableNamesFromCompact(File $phpcsFile, $stackPtr, $
473473
if ($argumentFirstToken['code'] === T_ARRAY) {
474474
// It's an array argument, recurse.
475475
$arrayArguments = Helpers::findFunctionCallArguments($phpcsFile, $argumentPtrs[0]);
476-
$variableNames = array_merge($variableNames, self::getVariableNamesFromCompact($phpcsFile, $stackPtr, $arrayArguments));
476+
$variablePositionsAndNames = array_merge($variablePositionsAndNames, self::getVariablesInsideCompact($phpcsFile, $stackPtr, $arrayArguments));
477477
continue;
478478
}
479479
if (count($argumentPtrs) > 1) {
@@ -484,7 +484,9 @@ public static function getVariableNamesFromCompact(File $phpcsFile, $stackPtr, $
484484
// Single-quoted string literal, ie compact('whatever').
485485
// Substr is to strip the enclosing single-quotes.
486486
$varName = substr($argumentFirstToken['content'], 1, -1);
487-
$variableNames[] = $varName;
487+
$variable = new VariableInfo($varName);
488+
$variable->firstRead = $argumentPtrs[0];
489+
$variablePositionsAndNames[] = $variable;
488490
continue;
489491
}
490492
if ($argumentFirstToken['code'] === T_DOUBLE_QUOTED_STRING) {
@@ -496,11 +498,13 @@ public static function getVariableNamesFromCompact(File $phpcsFile, $stackPtr, $
496498
}
497499
// Substr is to strip the enclosing double-quotes.
498500
$varName = substr($argumentFirstToken['content'], 1, -1);
499-
$variableNames[] = $varName;
501+
$variable = new VariableInfo($varName);
502+
$variable->firstRead = $argumentPtrs[0];
503+
$variablePositionsAndNames[] = $variable;
500504
continue;
501505
}
502506
}
503-
return $variableNames;
507+
return $variablePositionsAndNames;
504508
}
505509

506510
/**

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,16 +1880,14 @@ protected function processVariableInString(File $phpcsFile, $stackPtr)
18801880
protected function processCompact(File $phpcsFile, $stackPtr)
18811881
{
18821882
Helpers::debug("processCompact at {$stackPtr}");
1883-
18841883
$arguments = Helpers::findFunctionCallArguments($phpcsFile, $stackPtr);
1885-
$variableNames = Helpers::getVariableNamesFromCompact($phpcsFile, $stackPtr, $arguments);
1886-
1887-
foreach ( $variableNames as $variableName ) {
1888-
$currScope = Helpers::findVariableScope($phpcsFile, $stackPtr, $variableName);
1884+
$variables = Helpers::getVariablesInsideCompact($phpcsFile, $stackPtr, $arguments);
1885+
foreach ( $variables as $variable ) {
1886+
$currScope = Helpers::findVariableScope($phpcsFile, $stackPtr, $variable->name);
18891887
if ($currScope === null) {
18901888
continue;
18911889
}
1892-
$this->markVariableReadAndWarnIfUndefined($phpcsFile, $variableName, $stackPtr, $currScope);
1890+
$this->markVariableReadAndWarnIfUndefined($phpcsFile, $variable->name, $variable->firstRead, $currScope);
18931891
}
18941892
}
18951893

0 commit comments

Comments
 (0)