@@ -123,23 +123,51 @@ public function process(File $phpcsFile, $stackPtr) {
123123 if (($ token ['code ' ] === T_STRING ) && ($ token ['content ' ] === 'compact ' )) {
124124 return $ this ->processCompact ($ phpcsFile , $ stackPtr );
125125 }
126+ if ($ this ->isGetDefinedVars ($ phpcsFile , $ stackPtr )) {
127+ Helpers::debug ('get_defined_vars is being called ' );
128+ return $ this ->markAllVariablesRead ($ phpcsFile , $ stackPtr );
129+ }
126130 if (($ token ['code ' ] === T_CLOSE_CURLY_BRACKET ) && isset ($ token ['scope_condition ' ])) {
127131 return $ this ->processScopeClose ($ phpcsFile , $ token ['scope_condition ' ]);
128132 }
129133 }
130134
135+ protected function isGetDefinedVars (File $ phpcsFile , $ stackPtr ) {
136+ $ tokens = $ phpcsFile ->getTokens ();
137+ $ token = $ tokens [$ stackPtr ];
138+ if (! $ token || $ token ['content ' ] !== 'get_defined_vars ' ) {
139+ return false ;
140+ }
141+ // Make sure this is a function call
142+ $ parenPointer = $ phpcsFile ->findNext (T_OPEN_PARENTHESIS , $ stackPtr , $ stackPtr + 2 );
143+ if (! $ parenPointer ) {
144+ return false ;
145+ }
146+ return true ;
147+ }
148+
131149 protected function getScopeKey ($ currScope ) {
132150 if ($ currScope === false ) {
133151 $ currScope = 'file ' ;
134152 }
135153 return ($ this ->currentFile ? $ this ->currentFile ->getFilename () : 'unknown file ' ) . ': ' . $ currScope ;
136154 }
137155
156+ /**
157+ * @param int $currScope
158+ *
159+ * @return ?ScopeInfo
160+ */
138161 protected function getScopeInfo ($ currScope ) {
139162 $ scopeKey = $ this ->getScopeKey ($ currScope );
140163 return isset ($ this ->scopes [$ scopeKey ]) ? $ this ->scopes [$ scopeKey ] : null ;
141164 }
142165
166+ /**
167+ * @param int $currScope
168+ *
169+ * @return ScopeInfo
170+ */
143171 protected function getOrCreateScopeInfo ($ currScope ) {
144172 $ scopeKey = $ this ->getScopeKey ($ currScope );
145173 if (!isset ($ this ->scopes [$ scopeKey ])) {
@@ -241,6 +269,13 @@ protected function markVariableDeclaration(
241269 $ varInfo ->firstDeclared = $ stackPtr ;
242270 }
243271
272+ /**
273+ * @param string $varName
274+ * @param int $stackPtr
275+ * @param int $currScope
276+ *
277+ * @return void
278+ */
244279 protected function markVariableRead ($ varName , $ stackPtr , $ currScope ) {
245280 $ varInfo = $ this ->getOrCreateVariableInfo ($ varName , $ currScope );
246281 if (isset ($ varInfo ->firstRead ) && ($ varInfo ->firstRead <= $ stackPtr )) {
@@ -284,6 +319,25 @@ protected function markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $sta
284319 }
285320 }
286321
322+ /**
323+ * @param File $phpcsFile
324+ * @param int $stackPtr
325+ *
326+ * @return void
327+ */
328+ protected function markAllVariablesRead (File $ phpcsFile , $ stackPtr ) {
329+ $ currScope = Helpers::findVariableScope ($ phpcsFile , $ stackPtr );
330+ if (! $ currScope ) {
331+ return false ;
332+ }
333+ $ scopeInfo = $ this ->getOrCreateScopeInfo ($ currScope );
334+ $ count = count ($ scopeInfo ->variables );
335+ Helpers::debug ("marking all $ count variables in scope as read " );
336+ foreach ($ scopeInfo ->variables as $ varInfo ) {
337+ $ this ->markVariableRead ($ varInfo ->name , $ stackPtr , $ scopeInfo ->owner );
338+ }
339+ }
340+
287341 protected function checkForFunctionPrototype (File $ phpcsFile , $ stackPtr , $ varName , $ currScope ) {
288342 $ tokens = $ phpcsFile ->getTokens ();
289343 $ token = $ tokens [$ stackPtr ];
@@ -723,7 +777,7 @@ protected function checkForPassByReferenceFunctionCall(File $phpcsFile, $stackPt
723777
724778 // Are we pass-by-reference to known pass-by-reference function?
725779 $ functionPtr = Helpers::findFunctionCall ($ phpcsFile , $ stackPtr );
726- if ($ functionPtr === false ) {
780+ if ($ functionPtr === false || ! isset ( $ tokens [ $ functionPtr ]) ) {
727781 return false ;
728782 }
729783
0 commit comments