@@ -326,14 +326,6 @@ public function formatList($list)
326
326
*/
327
327
$ prev = null ;
328
328
329
- /**
330
- * Comments are being formatted separately to maintain the whitespaces
331
- * before and after them.
332
- *
333
- * @var string
334
- */
335
- $ comment = '' ;
336
-
337
329
// In order to be able to format the queries correctly, the next token
338
330
// must be taken into consideration. The loop below uses two pointers,
339
331
// `$prev` and `$curr` which store two consecutive tokens.
@@ -342,39 +334,26 @@ public function formatList($list)
342
334
/**
343
335
* Token parsed at this moment.
344
336
*
345
- * @var Token
337
+ * @var Token $curr
346
338
*/
347
339
$ curr = $ list ->tokens [$ list ->idx ];
348
340
349
341
if ($ curr ->type === Token::TYPE_WHITESPACE ) {
350
342
// Whitespaces are skipped because the formatter adds its own.
351
343
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
+ }
372
345
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
374
348
continue ;
375
349
}
376
350
377
351
// Checking if pointers were initialized.
352
+ /**
353
+ * Previous Token.
354
+ *
355
+ * @var Token $prev
356
+ */
378
357
if ($ prev !== null ) {
379
358
// Checking if a new clause started.
380
359
if (static ::isClause ($ prev ) !== false ) {
@@ -453,12 +432,6 @@ public function formatList($list)
453
432
$ shortGroup = false ;
454
433
}
455
434
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
-
462
435
// Adding the token.
463
436
$ ret .= $ this ->toString ($ prev );
464
437
@@ -469,32 +442,29 @@ public function formatList($list)
469
442
$ indent = 0 ;
470
443
}
471
444
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
+
476
448
$ lineEnded = false ;
477
449
} else {
478
450
// If the line ended there is no point in adding whitespaces.
479
451
// 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 (
485
453
// 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
+ )
487
462
) {
488
463
$ ret .= ' ' ;
489
464
}
490
465
}
491
466
}
492
467
493
- if (!empty ($ comment )) {
494
- $ ret .= $ comment ;
495
- $ comment = '' ;
496
- }
497
-
498
468
// Iteration finished, consider current token as previous.
499
469
$ prev = $ curr ;
500
470
}
0 commit comments