@@ -178,7 +178,7 @@ protected function checkLineLength(File $phpcsFile, array $tokens, int $stackPtr
178178 && $ lineLength > $ this ->absoluteLineLimit
179179 ) {
180180 $ code = 'MaxExceeded ' ;
181- if ($ this ->isLineNamespacedName ($ tokens , $ tokens [ $ stackPtr][ ' line ' ] ) === true ) {
181+ if ($ this ->isNamespacedNameExceedingLength ($ tokens , $ stackPtr, $ this -> absoluteLineLimit ) === true ) {
182182 $ code = 'NamespacedNameMaxExceeded ' ;
183183 }
184184
@@ -191,7 +191,7 @@ protected function checkLineLength(File $phpcsFile, array $tokens, int $stackPtr
191191 $ phpcsFile ->addError ($ error , $ stackPtr , $ code , $ data );
192192 } elseif ($ lineLength > $ this ->lineLimit ) {
193193 $ code = 'TooLong ' ;
194- if ($ this ->isLineNamespacedName ($ tokens , $ tokens [ $ stackPtr][ ' line ' ] ) === true ) {
194+ if ($ this ->isNamespacedNameExceedingLength ($ tokens , $ stackPtr, $ this -> lineLimit ) === true ) {
195195 $ code = 'NamespacedNameTooLong ' ;
196196 }
197197
@@ -209,21 +209,38 @@ protected function checkLineLength(File $phpcsFile, array $tokens, int $stackPtr
209209 /**
210210 * Checks if a line is a namespaced name
211211 *
212- * @param array $tokens The token stack.
213- * @param int $line The line to check validate
212+ * @param array $tokens The token stack.
213+ * @param int $stackPtr The last token on the line.
214+ * @param int $lineLimit The line limit.
214215 *
215216 * @return bool
216217 */
217- private function isLineNamespacedName (array $ tokens , int $ line ): bool
218+ private function isNamespacedNameExceedingLength (array $ tokens , int $ stackPtr , int $ lineLimit ): bool
218219 {
219- $ filteredTokens = array_filter (
220- $ tokens ,
221- function ($ token ) use ($ line ) {
222- return $ token ['line ' ] === $ line
223- && in_array ($ token ['code ' ], [T_NAME_QUALIFIED , T_NAME_FULLY_QUALIFIED , T_NAME_RELATIVE ], true );
220+ $ line = $ tokens [$ stackPtr ]['line ' ];
221+
222+ for ($ i = $ stackPtr ; $ i --;) {
223+ // Only look back the tokens on this line else stop looking
224+ if ($ tokens [$ i ]['line ' ] !== $ line ) {
225+ break ;
226+ }
227+
228+ $ length = ($ tokens [$ i ]['column ' ] + $ tokens [$ i ]['length ' ] - 1 );
229+
230+ // Check the line limit of the namespaced name is equal or over the line length. This check accounts for the
231+ // fact that namespaced names are or closed by ; or opened by ( or {
232+ if (
233+ $ length >= $ lineLimit &&
234+ (
235+ $ tokens [$ i ]['code ' ] === T_NAME_QUALIFIED ||
236+ $ tokens [$ i ]['code ' ] === T_NAME_FULLY_QUALIFIED ||
237+ $ tokens [$ i ]['code ' ] === T_NAME_RELATIVE
238+ )
239+ ) {
240+ return true ;
224241 }
225- );
242+ }
226243
227- return $ filteredTokens !== [] ;
244+ return false ;
228245 }
229246}
0 commit comments