@@ -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 /**
0 commit comments