@@ -1869,61 +1869,6 @@ protected function processVariableInString(File $phpcsFile, $stackPtr)
1869
1869
}
1870
1870
}
1871
1871
1872
- /**
1873
- * @param File $phpcsFile
1874
- * @param int $stackPtr
1875
- * @param array<int, array<int>> $arguments The stack pointers of each argument
1876
- * @param int $currScope
1877
- *
1878
- * @return void
1879
- */
1880
- protected function processCompactArguments (File $ phpcsFile , $ stackPtr , $ arguments , $ currScope )
1881
- {
1882
- $ tokens = $ phpcsFile ->getTokens ();
1883
-
1884
- foreach ($ arguments as $ argumentPtrs ) {
1885
- $ argumentPtrs = array_values (array_filter ($ argumentPtrs , function ($ argumentPtr ) use ($ tokens ) {
1886
- return isset (Tokens::$ emptyTokens [$ tokens [$ argumentPtr ]['code ' ]]) === false ;
1887
- }));
1888
- if (empty ($ argumentPtrs )) {
1889
- continue ;
1890
- }
1891
- if (!isset ($ tokens [$ argumentPtrs [0 ]])) {
1892
- continue ;
1893
- }
1894
- $ argumentFirstToken = $ tokens [$ argumentPtrs [0 ]];
1895
- if ($ argumentFirstToken ['code ' ] === T_ARRAY ) {
1896
- // It's an array argument, recurse.
1897
- $ arrayArguments = Helpers::findFunctionCallArguments ($ phpcsFile , $ argumentPtrs [0 ]);
1898
- $ this ->processCompactArguments ($ phpcsFile , $ stackPtr , $ arrayArguments , $ currScope );
1899
- continue ;
1900
- }
1901
- if (count ($ argumentPtrs ) > 1 ) {
1902
- // Complex argument, we can't handle it, ignore.
1903
- continue ;
1904
- }
1905
- if ($ argumentFirstToken ['code ' ] === T_CONSTANT_ENCAPSED_STRING ) {
1906
- // Single-quoted string literal, ie compact('whatever').
1907
- // Substr is to strip the enclosing single-quotes.
1908
- $ varName = substr ($ argumentFirstToken ['content ' ], 1 , -1 );
1909
- $ this ->markVariableReadAndWarnIfUndefined ($ phpcsFile , $ varName , $ argumentPtrs [0 ], $ currScope );
1910
- continue ;
1911
- }
1912
- if ($ argumentFirstToken ['code ' ] === T_DOUBLE_QUOTED_STRING ) {
1913
- // Double-quoted string literal.
1914
- $ regexp = Constants::getDoubleQuotedVarRegexp ();
1915
- if (! empty ($ regexp ) && preg_match ($ regexp , $ argumentFirstToken ['content ' ])) {
1916
- // Bail if the string needs variable expansion, that's runtime stuff.
1917
- continue ;
1918
- }
1919
- // Substr is to strip the enclosing double-quotes.
1920
- $ varName = substr ($ argumentFirstToken ['content ' ], 1 , -1 );
1921
- $ this ->markVariableReadAndWarnIfUndefined ($ phpcsFile , $ varName , $ argumentPtrs [0 ], $ currScope );
1922
- continue ;
1923
- }
1924
- }
1925
- }
1926
-
1927
1872
/**
1928
1873
* Called to process variables named in a call to compact().
1929
1874
*
@@ -1934,13 +1879,17 @@ protected function processCompactArguments(File $phpcsFile, $stackPtr, $argument
1934
1879
*/
1935
1880
protected function processCompact (File $ phpcsFile , $ stackPtr )
1936
1881
{
1937
- $ currScope = Helpers::findVariableScope ($ phpcsFile , $ stackPtr );
1938
- if ($ currScope === null ) {
1939
- return ;
1940
- }
1941
-
1882
+ Helpers::debug ("processCompact at {$ stackPtr }" );
1942
1883
$ arguments = Helpers::findFunctionCallArguments ($ phpcsFile , $ stackPtr );
1943
- $ this ->processCompactArguments ($ phpcsFile , $ stackPtr , $ arguments , $ currScope );
1884
+ $ variables = Helpers::getVariablesInsideCompact ($ phpcsFile , $ stackPtr , $ arguments );
1885
+ foreach ($ variables as $ variable ) {
1886
+ $ currScope = Helpers::findVariableScope ($ phpcsFile , $ stackPtr , $ variable ->name );
1887
+ if ($ currScope === null ) {
1888
+ continue ;
1889
+ }
1890
+ $ variablePosition = $ variable ->firstRead ? $ variable ->firstRead : $ stackPtr ;
1891
+ $ this ->markVariableReadAndWarnIfUndefined ($ phpcsFile , $ variable ->name , $ variablePosition , $ currScope );
1892
+ }
1944
1893
}
1945
1894
1946
1895
/**
0 commit comments