@@ -346,10 +346,10 @@ class Generic_Sniffs_CodeAnalysis_VariableAnalysisSniff implements PHP_CodeSniff
346346
347347 /**
348348 * Returns an array of tokens this test wants to listen for.
349- *
349+ *
350350 * @return array
351351 */
352- public function register () {
352+ public function register () {
353353 // Magic to modfy $_passByRefFunctions with any site-specific settings.
354354 if (!empty ($ this ->sitePassByRefFunctions )) {
355355 foreach (preg_split ('/\s+/ ' , trim ($ this ->sitePassByRefFunctions )) as $ line ) {
@@ -364,6 +364,7 @@ public function register() {
364364 return array (
365365 T_VARIABLE ,
366366 T_DOUBLE_QUOTED_STRING ,
367+ T_HEREDOC ,
367368 T_CLOSE_CURLY_BRACKET ,
368369 );
369370 }//end register()
@@ -374,7 +375,7 @@ public function register() {
374375 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
375376 * @param int $stackPtr The position of the current token
376377 * in the stack passed in $tokens.
377- *
378+ *
378379 * @return void
379380 */
380381 public function process (PHP_CodeSniffer_File $ phpcsFile , $ stackPtr ) {
@@ -392,7 +393,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
392393 if ($ token ['code ' ] === T_VARIABLE ) {
393394 return $ this ->processVariable ($ phpcsFile , $ stackPtr );
394395 }
395- if ($ token ['code ' ] === T_DOUBLE_QUOTED_STRING ) {
396+ if (($ token ['code ' ] === T_DOUBLE_QUOTED_STRING ) ||
397+ ($ token ['code ' ] === T_HEREDOC )) {
396398 return $ this ->processVariableInString ($ phpcsFile , $ stackPtr );
397399 }
398400 if (($ token ['code ' ] === T_CLOSE_CURLY_BRACKET ) &&
@@ -541,15 +543,29 @@ function findVariableScope(
541543 $ tokens = $ phpcsFile ->getTokens ();
542544 $ token = $ tokens [$ stackPtr ];
543545
546+ $ in_class = false ;
544547 if (!empty ($ token ['conditions ' ])) {
545548 foreach (array_reverse ($ token ['conditions ' ], true ) as $ scopePtr => $ scopeCode ) {
546549 if (($ scopeCode === T_FUNCTION ) || ($ scopeCode === T_CLOSURE )) {
547550 return $ scopePtr ;
548551 }
552+ if (($ scopeCode === T_CLASS ) || ($ scopeCode === T_INTERFACE )) {
553+ $ in_class = true ;
554+ }
549555 }
550556 }
551557
552- return $ this ->findFunctionPrototype ($ phpcsFile , $ stackPtr );
558+ if (($ scopePtr = $ this ->findFunctionPrototype ($ phpcsFile , $ stackPtr )) !== false ) {
559+ return $ scopePtr ;
560+ }
561+
562+ if ($ in_class ) {
563+ // Member var of a class, we don't care.
564+ return false ;
565+ }
566+
567+ // File scope, hmm, lets use first token of file?
568+ return 0 ;
553569 }
554570
555571 function isNextThingAnAssign (
@@ -1229,7 +1245,7 @@ protected function processVariableInString(
12291245 $ tokens = $ phpcsFile ->getTokens ();
12301246 $ token = $ tokens [$ stackPtr ];
12311247
1232- $ pattern = '|[^ \\\] \${?([a-zA-Z0-9_]+)}?| ' ;
1248+ $ pattern = '|(?<! \\\\ )(?: \\\\ {2})* \${?([a-zA-Z0-9_]+)}?| ' ;
12331249 if (!preg_match_all ($ pattern , $ token ['content ' ], $ matches )) {
12341250 return ;
12351251 }
@@ -1242,7 +1258,7 @@ protected function processVariableInString(
12421258 continue ;
12431259 }
12441260 if ($ this ->checkForSuperGlobal ($ phpcsFile , $ stackPtr , $ varName , $ currScope )) {
1245- return ;
1261+ continue ;
12461262 }
12471263 $ this ->markVariableRead ($ varName , $ stackPtr , $ currScope );
12481264 if ($ this ->isVariableUndefined ($ varName , $ stackPtr , $ currScope ) === true ) {
0 commit comments