@@ -129,6 +129,7 @@ public function parseIdentifier(bool $ignoreCase = true): string
129129 if ($ ignoreCase ) {
130130 $ result = $ this ->strtolower ($ result );
131131 }
132+
132133 return $ result ;
133134 }
134135
@@ -181,11 +182,12 @@ public function parseCharacter(bool $isForIdentifier): ?string
181182 } else {
182183 return $ this ->consume (1 );
183184 }
185+
184186 return null ;
185187 }
186188
187189 /**
188- * @return array<int, Comment>|void
190+ * @return list< Comment>
189191 *
190192 * @throws UnexpectedEOFException
191193 * @throws UnexpectedTokenException
@@ -202,24 +204,27 @@ public function consumeWhiteSpace(): array
202204 $ comment = $ this ->consumeComment ();
203205 } catch (UnexpectedEOFException $ e ) {
204206 $ this ->currentPosition = \count ($ this ->characters );
205- return $ comments ;
207+ break ;
206208 }
207209 } else {
208210 $ comment = $ this ->consumeComment ();
209211 }
210- if ($ comment !== false ) {
212+ if ($ comment instanceof Comment ) {
211213 $ comments [] = $ comment ;
212214 }
213- } while ($ comment !== false );
215+ } while ($ comment instanceof Comment);
216+
214217 return $ comments ;
215218 }
216219
220+ /**
221+ * @param non-empty-string $string
222+ */
217223 public function comes (string $ string , bool $ caseInsensitive = false ): bool
218224 {
219225 $ peek = $ this ->peek (\strlen ($ string ));
220- return ($ peek == '' )
221- ? false
222- : $ this ->streql ($ peek , $ string , $ caseInsensitive );
226+
227+ return ($ peek !== '' ) && $ this ->streql ($ peek , $ string , $ caseInsensitive );
223228 }
224229
225230 /**
@@ -232,6 +237,7 @@ public function peek(int $length = 1, int $offset = 0): string
232237 if ($ offset >= \count ($ this ->characters )) {
233238 return '' ;
234239 }
240+
235241 return $ this ->substr ($ offset , $ length );
236242 }
237243
@@ -254,19 +260,22 @@ public function consume($value = 1): string
254260 $ this ->lineNumber
255261 );
256262 }
263+
257264 $ this ->lineNumber += $ numberOfLines ;
258265 $ this ->currentPosition += $ this ->strlen ($ value );
259- return $ value ;
266+ $ result = $ value ;
260267 } else {
261268 if ($ this ->currentPosition + $ value > \count ($ this ->characters )) {
262269 throw new UnexpectedEOFException ((string ) $ value , $ this ->peek (5 ), 'count ' , $ this ->lineNumber );
263270 }
271+
264272 $ result = $ this ->substr ($ this ->currentPosition , $ value );
265273 $ numberOfLines = \substr_count ($ result , "\n" );
266274 $ this ->lineNumber += $ numberOfLines ;
267275 $ this ->currentPosition += $ value ;
268- return $ result ;
269276 }
277+
278+ return $ result ;
270279 }
271280
272281 /**
@@ -279,21 +288,23 @@ public function consume($value = 1): string
279288 public function consumeExpression (string $ expression , ?int $ maximumLength = null ): string
280289 {
281290 $ matches = null ;
282- $ input = $ maximumLength !== null ? $ this ->peek ($ maximumLength ) : $ this ->inputLeft ();
283- if (\preg_match ($ expression , $ input , $ matches , PREG_OFFSET_CAPTURE ) = == 1 ) {
284- return $ this ->consume ( $ matches [ 0 ][ 0 ] );
291+ $ input = ( $ maximumLength !== null ) ? $ this ->peek ($ maximumLength ) : $ this ->inputLeft ();
292+ if (\preg_match ($ expression , $ input , $ matches , PREG_OFFSET_CAPTURE ) ! == 1 ) {
293+ throw new UnexpectedTokenException ( $ expression , $ this ->peek ( 5 ), ' expression ' , $ this -> lineNumber );
285294 }
286- throw new UnexpectedTokenException ($ expression , $ this ->peek (5 ), 'expression ' , $ this ->lineNumber );
295+
296+ return $ this ->consume ($ matches [0 ][0 ]);
287297 }
288298
289299 /**
290300 * @return Comment|false
291301 */
292302 public function consumeComment ()
293303 {
294- $ comment = false ;
304+ $ lineNumber = $ this ->lineNumber ;
305+ $ comment = null ;
306+
295307 if ($ this ->comes ('/* ' )) {
296- $ lineNumber = $ this ->lineNumber ;
297308 $ this ->consume (1 );
298309 $ comment = '' ;
299310 while (($ char = $ this ->consume (1 )) !== '' ) {
@@ -305,12 +316,8 @@ public function consumeComment()
305316 }
306317 }
307318
308- if ($ comment !== false ) {
309- // We skip the * which was included in the comment.
310- return new Comment (\substr ($ comment , 1 ), $ lineNumber );
311- }
312-
313- return $ comment ;
319+ // We skip the * which was included in the comment.
320+ return \is_string ($ comment ) ? new Comment (\substr ($ comment , 1 ), $ lineNumber ) : false ;
314321 }
315322
316323 public function isEnd (): bool
@@ -319,7 +326,7 @@ public function isEnd(): bool
319326 }
320327
321328 /**
322- * @param array<array-key, string>|string $stopCharacters
329+ * @param list< string>|string $stopCharacters
323330 * @param array<int, Comment> $comments
324331 *
325332 * @throws UnexpectedEOFException
@@ -346,7 +353,8 @@ public function consumeUntil(
346353 return $ consumedCharacters ;
347354 }
348355 $ consumedCharacters .= $ character ;
349- if ($ comment = $ this ->consumeComment ()) {
356+ $ comment = $ this ->consumeComment ();
357+ if ($ comment instanceof Comment) {
350358 $ comments [] = $ comment ;
351359 }
352360 }
@@ -371,11 +379,9 @@ private function inputLeft(): string
371379
372380 public function streql (string $ string1 , string $ string2 , bool $ caseInsensitive = true ): bool
373381 {
374- if ($ caseInsensitive ) {
375- return $ this ->strtolower ($ string1 ) === $ this ->strtolower ($ string2 );
376- } else {
377- return $ string1 === $ string2 ;
378- }
382+ return $ caseInsensitive
383+ ? ($ this ->strtolower ($ string1 ) === $ this ->strtolower ($ string2 ))
384+ : ($ string1 === $ string2 );
379385 }
380386
381387 /**
@@ -391,11 +397,9 @@ public function backtrack(int $numberOfCharacters): void
391397 */
392398 public function strlen (string $ string ): int
393399 {
394- if ($ this ->parserSettings ->hasMultibyteSupport ()) {
395- return \mb_strlen ($ string , $ this ->charset );
396- } else {
397- return \strlen ($ string );
398- }
400+ return $ this ->parserSettings ->hasMultibyteSupport ()
401+ ? \mb_strlen ($ string , $ this ->charset )
402+ : \strlen ($ string );
399403 }
400404
401405 /**
@@ -415,20 +419,22 @@ private function substr(int $offset, int $length): string
415419 $ offset ++;
416420 $ length --;
417421 }
422+
418423 return $ result ;
419424 }
420425
426+ /**
427+ * @return ($string is non-empty-string ? non-empty-string : string)
428+ */
421429 private function strtolower (string $ string ): string
422430 {
423- if ($ this ->parserSettings ->hasMultibyteSupport ()) {
424- return \mb_strtolower ($ string , $ this ->charset );
425- } else {
426- return \strtolower ($ string );
427- }
431+ return $ this ->parserSettings ->hasMultibyteSupport ()
432+ ? \mb_strtolower ($ string , $ this ->charset )
433+ : \strtolower ($ string );
428434 }
429435
430436 /**
431- * @return array<int, string>
437+ * @return list< string>
432438 *
433439 * @throws SourceException if the charset is UTF-8 and the string contains invalid byte sequences
434440 */
@@ -440,33 +446,27 @@ private function strsplit(string $string): array
440446 if (!\is_array ($ result )) {
441447 throw new SourceException ('`preg_split` failed with error ' . \preg_last_error ());
442448 }
443- return $ result ;
444449 } else {
445450 $ length = \mb_strlen ($ string , $ this ->charset );
446451 $ result = [];
447452 for ($ i = 0 ; $ i < $ length ; ++$ i ) {
448453 $ result [] = \mb_substr ($ string , $ i , 1 , $ this ->charset );
449454 }
450- return $ result ;
451455 }
452456 } else {
453- if ($ string === '' ) {
454- return [];
455- } else {
456- return \str_split ($ string );
457- }
457+ $ result = ($ string !== '' ) ? \str_split ($ string ) : [];
458458 }
459+
460+ return $ result ;
459461 }
460462
461463 /**
462- * @return int|false
464+ * @return int<0, max> |false
463465 */
464466 private function strpos (string $ haystack , string $ needle , int $ offset )
465467 {
466- if ($ this ->parserSettings ->hasMultibyteSupport ()) {
467- return \mb_strpos ($ haystack , $ needle , $ offset , $ this ->charset );
468- } else {
469- return \strpos ($ haystack , $ needle , $ offset );
470- }
468+ return $ this ->parserSettings ->hasMultibyteSupport ()
469+ ? \mb_strpos ($ haystack , $ needle , $ offset , $ this ->charset )
470+ : \strpos ($ haystack , $ needle , $ offset );
471471 }
472472}
0 commit comments