@@ -123,23 +123,51 @@ public function process(File $phpcsFile, $stackPtr) {
123
123
if (($ token ['code ' ] === T_STRING ) && ($ token ['content ' ] === 'compact ' )) {
124
124
return $ this ->processCompact ($ phpcsFile , $ stackPtr );
125
125
}
126
+ if ($ this ->isGetDefinedVars ($ phpcsFile , $ stackPtr )) {
127
+ Helpers::debug ('get_defined_vars is being called ' );
128
+ return $ this ->markAllVariablesRead ($ phpcsFile , $ stackPtr );
129
+ }
126
130
if (($ token ['code ' ] === T_CLOSE_CURLY_BRACKET ) && isset ($ token ['scope_condition ' ])) {
127
131
return $ this ->processScopeClose ($ phpcsFile , $ token ['scope_condition ' ]);
128
132
}
129
133
}
130
134
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
+
131
149
protected function getScopeKey ($ currScope ) {
132
150
if ($ currScope === false ) {
133
151
$ currScope = 'file ' ;
134
152
}
135
153
return ($ this ->currentFile ? $ this ->currentFile ->getFilename () : 'unknown file ' ) . ': ' . $ currScope ;
136
154
}
137
155
156
+ /**
157
+ * @param int $currScope
158
+ *
159
+ * @return ?ScopeInfo
160
+ */
138
161
protected function getScopeInfo ($ currScope ) {
139
162
$ scopeKey = $ this ->getScopeKey ($ currScope );
140
163
return isset ($ this ->scopes [$ scopeKey ]) ? $ this ->scopes [$ scopeKey ] : null ;
141
164
}
142
165
166
+ /**
167
+ * @param int $currScope
168
+ *
169
+ * @return ScopeInfo
170
+ */
143
171
protected function getOrCreateScopeInfo ($ currScope ) {
144
172
$ scopeKey = $ this ->getScopeKey ($ currScope );
145
173
if (!isset ($ this ->scopes [$ scopeKey ])) {
@@ -241,6 +269,13 @@ protected function markVariableDeclaration(
241
269
$ varInfo ->firstDeclared = $ stackPtr ;
242
270
}
243
271
272
+ /**
273
+ * @param string $varName
274
+ * @param int $stackPtr
275
+ * @param int $currScope
276
+ *
277
+ * @return void
278
+ */
244
279
protected function markVariableRead ($ varName , $ stackPtr , $ currScope ) {
245
280
$ varInfo = $ this ->getOrCreateVariableInfo ($ varName , $ currScope );
246
281
if (isset ($ varInfo ->firstRead ) && ($ varInfo ->firstRead <= $ stackPtr )) {
@@ -284,6 +319,25 @@ protected function markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $sta
284
319
}
285
320
}
286
321
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
+
287
341
protected function checkForFunctionPrototype (File $ phpcsFile , $ stackPtr , $ varName , $ currScope ) {
288
342
$ tokens = $ phpcsFile ->getTokens ();
289
343
$ token = $ tokens [$ stackPtr ];
@@ -723,7 +777,7 @@ protected function checkForPassByReferenceFunctionCall(File $phpcsFile, $stackPt
723
777
724
778
// Are we pass-by-reference to known pass-by-reference function?
725
779
$ functionPtr = Helpers::findFunctionCall ($ phpcsFile , $ stackPtr );
726
- if ($ functionPtr === false ) {
780
+ if ($ functionPtr === false || ! isset ( $ tokens [ $ functionPtr ]) ) {
727
781
return false ;
728
782
}
729
783
0 commit comments