@@ -88,15 +88,15 @@ private function collectLongLinesData(File $file, int $start): array
8888 $ lastLine = null ;
8989 for ($ i = $ start ; $ i < $ file ->numTokens ; $ i ++) {
9090 // Still processing previous line: increment length and continue.
91- if (($ lastLine !== null ) && ($ tokens [$ i ]['line ' ] === $ lastLine )) {
92- $ content = (string )$ tokens [$ i ]['content ' ];
91+ if (($ lastLine !== null ) && ($ lastLine > 0 ) && ( $ tokens [$ i ]['line ' ] === $ lastLine )) {
92+ $ content = (string ) $ tokens [$ i ]['content ' ];
9393 $ data [$ lastLine ]['length ' ] += strlen ($ content );
9494 $ data [$ lastLine ]['nonEmptyLength ' ] += strlen (trim ($ content ));
9595 continue ;
9696 }
9797
9898 // A new line started: let's set "end" for the previous line (if this isn't 1st line)
99- if (($ lastLine !== null ) && isset ($ data [$ lastLine ])) {
99+ if (($ lastLine !== null ) && ( $ lastLine > 0 ) && isset ($ data [$ lastLine ])) {
100100 $ data [$ lastLine ]['end ' ] = $ i - 1 ;
101101 }
102102
@@ -111,32 +111,45 @@ private function collectLongLinesData(File $file, int $start): array
111111 }
112112
113113 // We still have to set the "end" for last file line.
114- if (($ lastLine !== null ) && ($ data [$ lastLine ]['end ' ] === null )) {
114+ if (($ lastLine !== null ) && ($ lastLine > 0 ) && ($ data [$ lastLine ]['end ' ] === null )) {
115+ /** @var int $lastLine */
115116 $ data [$ lastLine ]['end ' ] = $ i - 1 ;
116117 }
117118
118119 $ longLines = [];
120+ /**
121+ * @var int $lineNumber
122+ * @var array{length:int, nonEmptyLength:int, start:int, end:int|null} $lineData
123+ */
119124 foreach ($ data as $ lineNumber => $ lineData ) {
120- $ lineEnd = $ lineData ['end ' ] ?? $ lineData ['start ' ];
121- if (
122- (($ lineData ['length ' ] - $ this ->lineLimit ) <= 1 ) // 1 char of tolerance
123- || ($ lineData ['nonEmptyLength ' ] === 0 ) // ignore empty lines
124- || $ this ->isLongUse ($ file , $ tokens , $ lineData ['start ' ], $ lineEnd )
125- || $ this ->isLongI10nFunction ($ file , $ tokens , $ lineData ['start ' ], $ lineEnd )
126- || $ this ->isLongWord ($ file , $ tokens , $ lineData ['start ' ], $ lineEnd )
127- ) {
128- continue ;
125+ if (!$ this ->isLengthAcceptable ($ lineData , $ file , $ tokens )) {
126+ $ longLines [$ lineNumber ] = [$ lineData ['length ' ], $ lineData ['start ' ]];
129127 }
130-
131- $ longLines [$ lineNumber ] = [$ lineData ['length ' ], $ lineData ['start ' ]];
132128 }
133129
134130 return $ longLines ;
135131 }
136132
133+ /**
134+ * @param array{length:int, nonEmptyLength:int, start:int, end:int|null} $lineData
135+ * @param File $file
136+ * @param array<int, array<string, mixed>> $tokens
137+ * @return bool
138+ */
139+ private function isLengthAcceptable (array $ lineData , File $ file , array $ tokens ): bool
140+ {
141+ $ lineEnd = $ lineData ['end ' ] ?? $ lineData ['start ' ];
142+
143+ return (($ lineData ['length ' ] - $ this ->lineLimit ) <= 1 ) // 1 char of tolerance
144+ || ($ lineData ['nonEmptyLength ' ] === 0 ) // ignore empty lines
145+ || $ this ->isLongUse ($ file , $ tokens , $ lineData ['start ' ], $ lineEnd )
146+ || $ this ->isLongI10nFunction ($ file , $ tokens , $ lineData ['start ' ], $ lineEnd )
147+ || $ this ->isLongWord ($ file , $ tokens , $ lineData ['start ' ], $ lineEnd );
148+ }
149+
137150 /**
138151 * We don't want to split a single word in multiple lines.
139- * So if there's a long word (e.g. an URL) that alone is above max line length, we don't show
152+ * So if there's a long word (e.g. a URL) that alone is above max line length, we don't show
140153 * warnings for it.
141154 *
142155 * @param File $file
@@ -204,7 +217,7 @@ private function isLongHtmlAttribute(
204217 $ inPhp = true ;
205218 }
206219 if ($ tokens [$ i ]['code ' ] === T_INLINE_HTML || $ inPhp ) {
207- $ tokenContent = (string )$ tokens [$ i ]['content ' ];
220+ $ tokenContent = (string ) $ tokens [$ i ]['content ' ];
208221 $ content .= $ inPhp ? str_repeat ('x ' , strlen ($ tokenContent )) : $ tokenContent ;
209222 }
210223 if ($ tokens [$ i ]['code ' ] === T_CLOSE_TAG && $ inPhp ) {
@@ -247,7 +260,12 @@ private function isLongSingleWord(
247260 array $ tokens
248261 ): bool {
249262
250- $ words = preg_split ('~\s+~ ' , (string )$ tokens [$ position ]['content ' ], 2 , PREG_SPLIT_NO_EMPTY );
263+ $ words = preg_split (
264+ '~\s+~ ' ,
265+ (string ) $ tokens [$ position ]['content ' ],
266+ 2 ,
267+ PREG_SPLIT_NO_EMPTY
268+ );
251269
252270 // If multiple words exceed line limit, we can split each word in its own line
253271 if ($ words === false || count ($ words ) !== 1 ) {
@@ -257,7 +275,7 @@ private function isLongSingleWord(
257275 $ word = reset ($ words );
258276 $ firstNonWhitePos = $ file ->findNext (T_WHITESPACE , $ position , $ lineEnd , true );
259277 $ firstNonWhite = ($ firstNonWhitePos === false ) ? null : $ tokens [$ firstNonWhitePos ];
260- $ tolerance = is_array ($ firstNonWhite ) ? ((int )($ firstNonWhite ['column ' ] ?? 1 ) + 3 ) : 4 ;
278+ $ tolerance = is_array ($ firstNonWhite ) ? ((int ) ($ firstNonWhite ['column ' ] ?? 1 ) + 3 ) : 4 ;
261279
262280 return (strlen ($ word ) + $ tolerance ) > $ this ->lineLimit ;
263281 }
@@ -296,7 +314,7 @@ private function isLongI10nFunction(File $file, array $tokens, int $start, int $
296314 return false ;
297315 }
298316
299- $ function = strtolower ((string )$ tokens [$ functionPos ]['content ' ]);
317+ $ function = strtolower ((string ) $ tokens [$ functionPos ]['content ' ]);
300318 if (!in_array ($ function , self ::I18N_FUNCTIONS , true )) {
301319 return false ;
302320 }
@@ -309,7 +327,7 @@ private function isLongI10nFunction(File $file, array $tokens, int $start, int $
309327 if ($ tokens [$ i ]['line ' ] !== $ targetLine ) {
310328 continue ;
311329 }
312- $ textLen += max (1 , strlen ((string )$ tokens [$ i ]['content ' ]));
330+ $ textLen += max (1 , strlen ((string ) $ tokens [$ i ]['content ' ]));
313331 }
314332
315333 return ($ textLen + 2 ) > $ this ->lineLimit ;
@@ -336,7 +354,7 @@ private function isLongUse(File $file, array $tokens, int $start, int $end): boo
336354 $ endUse = $ file ->findEndOfStatement ($ usePos );
337355 $ useLen = 0 ;
338356 for ($ i = $ usePos ; $ i <= $ endUse ; $ i ++) {
339- $ useLen += strlen ((string )$ tokens [$ i ]['content ' ]);
357+ $ useLen += strlen ((string ) $ tokens [$ i ]['content ' ]);
340358 }
341359
342360 return $ useLen > $ this ->lineLimit ;
0 commit comments