@@ -326,14 +326,6 @@ public function formatList($list)
326326 */
327327 $ prev = null ;
328328
329- /**
330- * Comments are being formatted separately to maintain the whitespaces
331- * before and after them.
332- *
333- * @var string
334- */
335- $ comment = '' ;
336-
337329 // In order to be able to format the queries correctly, the next token
338330 // must be taken into consideration. The loop below uses two pointers,
339331 // `$prev` and `$curr` which store two consecutive tokens.
@@ -342,39 +334,26 @@ public function formatList($list)
342334 /**
343335 * Token parsed at this moment.
344336 *
345- * @var Token
337+ * @var Token $curr
346338 */
347339 $ curr = $ list ->tokens [$ list ->idx ];
348340
349341 if ($ curr ->type === Token::TYPE_WHITESPACE ) {
350342 // Whitespaces are skipped because the formatter adds its own.
351343 continue ;
352- } elseif ($ curr ->type === Token::TYPE_COMMENT ) {
353- // Whether the comments should be parsed.
354- if (!empty ($ this ->options ['remove_comments ' ])) {
355- continue ;
356- }
357-
358- if ($ list ->tokens [$ list ->idx - 1 ]->type === Token::TYPE_WHITESPACE ) {
359- // The whitespaces before and after are preserved for
360- // formatting reasons.
361- $ comment .= $ list ->tokens [$ list ->idx - 1 ]->token ;
362- }
363- $ comment .= $ this ->toString ($ curr );
364- if (($ list ->tokens [$ list ->idx + 1 ]->type === Token::TYPE_WHITESPACE )
365- && ($ list ->tokens [$ list ->idx + 2 ]->type !== Token::TYPE_COMMENT )
366- ) {
367- // Adding the next whitespace only there is no comment that
368- // follows it immediately which may cause adding a
369- // whitespace twice.
370- $ comment .= $ list ->tokens [$ list ->idx + 1 ]->token ;
371- }
344+ }
372345
373- // Everything was handled here, no need to continue.
346+ if ($ curr ->type === Token::TYPE_COMMENT && $ this ->options ['remove_comments ' ]) {
347+ // Skip Comments if option `remove_comments` is enabled
374348 continue ;
375349 }
376350
377351 // Checking if pointers were initialized.
352+ /**
353+ * Previous Token.
354+ *
355+ * @var Token $prev
356+ */
378357 if ($ prev !== null ) {
379358 // Checking if a new clause started.
380359 if (static ::isClause ($ prev ) !== false ) {
@@ -453,12 +432,6 @@ public function formatList($list)
453432 $ shortGroup = false ;
454433 }
455434
456- // Delimiter must be placed on the same line with the last
457- // clause.
458- if ($ curr ->type === Token::TYPE_DELIMITER ) {
459- $ lineEnded = false ;
460- }
461-
462435 // Adding the token.
463436 $ ret .= $ this ->toString ($ prev );
464437
@@ -469,32 +442,29 @@ public function formatList($list)
469442 $ indent = 0 ;
470443 }
471444
472- if ($ curr ->type !== Token::TYPE_COMMENT ) {
473- $ ret .= $ this ->options ['line_ending ' ]
474- . str_repeat ($ this ->options ['indentation ' ], $ indent );
475- }
445+ $ ret .= $ this ->options ['line_ending ' ]
446+ . str_repeat ($ this ->options ['indentation ' ], $ indent );
447+
476448 $ lineEnded = false ;
477449 } else {
478450 // If the line ended there is no point in adding whitespaces.
479451 // Also, some tokens do not have spaces before or after them.
480- if (!(($ prev ->type === Token::TYPE_OPERATOR && ($ prev ->value === '. ' || $ prev ->value === '( ' ))
481- // No space after . (
482- || ($ curr ->type === Token::TYPE_OPERATOR && ($ curr ->value === '. ' || $ curr ->value === ', ' || $ curr ->value === '( ' || $ curr ->value === ') ' ))
483- // No space before . , ( )
484- || $ curr ->type === Token::TYPE_DELIMITER && mb_strlen ($ curr ->value , 'UTF-8 ' ) < 2 )
452+ if (
485453 // A space after delimiters that are longer than 2 characters.
486- || $ prev ->value === 'DELIMITER '
454+ $ prev ->value === 'DELIMITER '
455+ || !(
456+ ($ prev ->type === Token::TYPE_OPERATOR && ($ prev ->value === '. ' || $ prev ->value === '( ' ))
457+ // No space after . (
458+ || ($ curr ->type === Token::TYPE_OPERATOR && ($ curr ->value === '. ' || $ curr ->value === ', ' || $ curr ->value === '( ' || $ curr ->value === ') ' ))
459+ // No space before . , ( )
460+ || $ curr ->type === Token::TYPE_DELIMITER && mb_strlen ($ curr ->value , 'UTF-8 ' ) < 2
461+ )
487462 ) {
488463 $ ret .= ' ' ;
489464 }
490465 }
491466 }
492467
493- if (!empty ($ comment )) {
494- $ ret .= $ comment ;
495- $ comment = '' ;
496- }
497-
498468 // Iteration finished, consider current token as previous.
499469 $ prev = $ curr ;
500470 }
0 commit comments