@@ -112,15 +112,15 @@ private function collectLongLinesData(File $file, int $start): array
112
112
$ lastLine = null ;
113
113
for ($ i = $ start ; $ i < $ file ->numTokens ; $ i ++) {
114
114
// Still processing previous line: increment length and continue.
115
- if ($ lastLine && ($ tokens [$ i ]['line ' ] === $ lastLine )) {
115
+ if (( $ lastLine !== null ) && ( $ lastLine > 0 ) && ($ tokens [$ i ]['line ' ] === $ lastLine )) {
116
116
$ content = (string ) $ tokens [$ i ]['content ' ];
117
117
$ data [$ lastLine ]['length ' ] += strlen ($ content );
118
118
$ data [$ lastLine ]['nonEmptyLength ' ] += strlen (trim ($ content ));
119
119
continue ;
120
120
}
121
121
122
122
// A new line started: let's set "end" for the previous line (if this isn't 1st line)
123
- if ($ lastLine && isset ($ data [$ lastLine ])) {
123
+ if (( $ lastLine !== null ) && ( $ lastLine > 0 ) && isset ($ data [$ lastLine ])) {
124
124
$ data [$ lastLine ]['end ' ] = $ i - 1 ;
125
125
}
126
126
@@ -135,32 +135,45 @@ private function collectLongLinesData(File $file, int $start): array
135
135
}
136
136
137
137
// We still have to set the "end" for last file line.
138
- if ($ lastLine && ($ data [$ lastLine ]['end ' ] === null )) {
138
+ if (($ lastLine !== null ) && ($ lastLine > 0 ) && ($ data [$ lastLine ]['end ' ] === null )) {
139
+ /** @var int $lastLine */
139
140
$ data [$ lastLine ]['end ' ] = $ i - 1 ;
140
141
}
141
142
142
143
$ longLines = [];
144
+ /**
145
+ * @var int $lineNumber
146
+ * @var array{length:int, nonEmptyLength:int, start:int, end:int|null} $lineData
147
+ */
143
148
foreach ($ data as $ lineNumber => $ lineData ) {
144
- $ lineEnd = $ lineData ['end ' ] ?? $ lineData ['start ' ];
145
- if (
146
- (($ lineData ['length ' ] - $ this ->lineLimit ) <= 1 ) // 1 char of tolerance
147
- || ($ lineData ['nonEmptyLength ' ] === 0 ) // ignore empty lines
148
- || $ this ->isLongUse ($ file , $ tokens , $ lineData ['start ' ], $ lineEnd )
149
- || $ this ->isLongI10nFunction ($ file , $ tokens , $ lineData ['start ' ], $ lineEnd )
150
- || $ this ->isLongWord ($ file , $ tokens , $ lineData ['start ' ], $ lineEnd )
151
- ) {
152
- continue ;
149
+ if (!$ this ->isLengthAcceptable ($ lineData , $ file , $ tokens )) {
150
+ $ longLines [$ lineNumber ] = [$ lineData ['length ' ], $ lineData ['start ' ]];
153
151
}
154
-
155
- $ longLines [$ lineNumber ] = [$ lineData ['length ' ], $ lineData ['start ' ]];
156
152
}
157
153
158
154
return $ longLines ;
159
155
}
160
156
157
+ /**
158
+ * @param array{length:int, nonEmptyLength:int, start:int, end:int|null} $lineData
159
+ * @param File $file
160
+ * @param array<int, array<string, mixed>> $tokens
161
+ * @return bool
162
+ */
163
+ private function isLengthAcceptable (array $ lineData , File $ file , array $ tokens ): bool
164
+ {
165
+ $ lineEnd = $ lineData ['end ' ] ?? $ lineData ['start ' ];
166
+
167
+ return (($ lineData ['length ' ] - $ this ->lineLimit ) <= 1 ) // 1 char of tolerance
168
+ || ($ lineData ['nonEmptyLength ' ] === 0 ) // ignore empty lines
169
+ || $ this ->isLongUse ($ file , $ tokens , $ lineData ['start ' ], $ lineEnd )
170
+ || $ this ->isLongI10nFunction ($ file , $ tokens , $ lineData ['start ' ], $ lineEnd )
171
+ || $ this ->isLongWord ($ file , $ tokens , $ lineData ['start ' ], $ lineEnd );
172
+ }
173
+
161
174
/**
162
175
* We don't want to split a single word in multiple lines.
163
- * So if there's a long word (e.g. an URL) that alone is above max line length, we don't show
176
+ * So if there's a long word (e.g. a URL) that alone is above max line length, we don't show
164
177
* warnings for it.
165
178
*
166
179
* @param File $file
0 commit comments