@@ -68,22 +68,45 @@ public function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
68
68
$ next = $ stackPtr ;
69
69
$ end = $ tokens [$ stackPtr ]['scope_closer ' ];
70
70
71
+ $ this ->checkThisUsage ($ phpcsFile , $ next , $ end );
72
+
73
+ }//end processTokenWithinScope()
74
+
75
+
76
+ /**
77
+ * Check for $this variable usage between $next and $end tokens.
78
+ *
79
+ * @param File $phpcsFile The current file being scanned.
80
+ * @param int $next The position of the next token to check.
81
+ * @param int $end The position of the last token to check.
82
+ *
83
+ * @return void
84
+ */
85
+ private function checkThisUsage (File $ phpcsFile , $ next , $ end )
86
+ {
87
+ $ tokens = $ phpcsFile ->getTokens ();
88
+
71
89
do {
72
90
$ next = $ phpcsFile ->findNext ([T_VARIABLE , T_ANON_CLASS ], ($ next + 1 ), $ end );
73
91
if ($ next === false ) {
74
92
continue ;
75
- } else if ($ tokens [$ next ]['code ' ] === T_ANON_CLASS ) {
93
+ }
94
+
95
+ if ($ tokens [$ next ]['code ' ] === T_ANON_CLASS ) {
96
+ $ this ->checkThisUsage ($ phpcsFile , $ next , $ tokens [$ next ]['scope_opener ' ]);
76
97
$ next = $ tokens [$ next ]['scope_closer ' ];
77
98
continue ;
78
- } else if ($ tokens [$ next ]['content ' ] !== '$this ' ) {
99
+ }
100
+
101
+ if ($ tokens [$ next ]['content ' ] !== '$this ' ) {
79
102
continue ;
80
103
}
81
104
82
105
$ error = 'Usage of "$this" in static methods will cause runtime errors ' ;
83
106
$ phpcsFile ->addError ($ error , $ next , 'Found ' );
84
107
} while ($ next !== false );
85
108
86
- }//end processTokenWithinScope ()
109
+ }//end checkThisUsage ()
87
110
88
111
89
112
/**
0 commit comments