@@ -550,30 +550,23 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
550
550
551
551
foreach ($ diff as $ i => $ diffElem ) {
552
552
$ diffType = $ diffElem ->type ;
553
- $ arrItem = $ diffElem ->new ;
554
- $ origArrayItem = $ diffElem ->old ;
553
+ $ newNode = $ diffElem ->new ;
554
+ $ originalNode = $ diffElem ->old ;
555
555
if ($ diffType === DiffElem::TYPE_KEEP || $ diffType === DiffElem::TYPE_REPLACE ) {
556
556
$ beforeFirstKeepOrReplace = false ;
557
- if (!$ arrItem instanceof Node || !$ origArrayItem instanceof Node) {
557
+ if (!$ newNode instanceof Node || !$ originalNode instanceof Node) {
558
558
return null ;
559
559
}
560
560
561
561
/** @var int $itemStartPos */
562
- $ itemStartPos = $ origArrayItem ->getAttribute (Attribute::START_INDEX );
562
+ $ itemStartPos = $ originalNode ->getAttribute (Attribute::START_INDEX );
563
563
564
564
/** @var int $itemEndPos */
565
- $ itemEndPos = $ origArrayItem ->getAttribute (Attribute::END_INDEX );
566
-
565
+ $ itemEndPos = $ originalNode ->getAttribute (Attribute::END_INDEX );
567
566
if ($ itemStartPos < 0 || $ itemEndPos < 0 || $ itemStartPos < $ tokenIndex ) {
568
567
throw new LogicException ();
569
568
}
570
569
571
- $ comments = $ arrItem ->getAttribute (Attribute::COMMENTS ) ?? [];
572
- $ origComments = $ origArrayItem ->getAttribute (Attribute::COMMENTS ) ?? [];
573
-
574
- $ commentStartPos = count ($ origComments ) > 0 ? $ origComments [0 ]->startIndex : $ itemStartPos ;
575
- assert ($ commentStartPos >= 0 );
576
-
577
570
$ result .= $ originalTokens ->getContentBetween ($ tokenIndex , $ itemStartPos );
578
571
579
572
if (count ($ delayedAdd ) > 0 ) {
@@ -583,15 +576,6 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
583
576
if ($ parenthesesNeeded ) {
584
577
$ result .= '( ' ;
585
578
}
586
-
587
- if ($ insertNewline ) {
588
- $ delayedAddComments = $ delayedAddNode ->getAttribute (Attribute::COMMENTS ) ?? [];
589
- if (count ($ delayedAddComments ) > 0 ) {
590
- $ result .= $ this ->printComments ($ delayedAddComments , $ beforeAsteriskIndent , $ afterAsteriskIndent );
591
- $ result .= sprintf ('%s%s*%s ' , $ originalTokens ->getDetectedNewline () ?? "\n" , $ beforeAsteriskIndent , $ afterAsteriskIndent );
592
- }
593
- }
594
-
595
579
$ result .= $ this ->printNodeFormatPreserving ($ delayedAddNode , $ originalTokens );
596
580
if ($ parenthesesNeeded ) {
597
581
$ result .= ') ' ;
@@ -608,21 +592,14 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
608
592
}
609
593
610
594
$ parenthesesNeeded = isset ($ this ->parenthesesListMap [$ mapKey ])
611
- && in_array (get_class ($ arrItem ), $ this ->parenthesesListMap [$ mapKey ], true )
612
- && !in_array (get_class ($ origArrayItem ), $ this ->parenthesesListMap [$ mapKey ], true );
595
+ && in_array (get_class ($ newNode ), $ this ->parenthesesListMap [$ mapKey ], true )
596
+ && !in_array (get_class ($ originalNode ), $ this ->parenthesesListMap [$ mapKey ], true );
613
597
$ addParentheses = $ parenthesesNeeded && !$ originalTokens ->hasParentheses ($ itemStartPos , $ itemEndPos );
614
598
if ($ addParentheses ) {
615
599
$ result .= '( ' ;
616
600
}
617
601
618
- if ($ comments !== $ origComments ) {
619
- if (count ($ comments ) > 0 ) {
620
- $ result .= $ this ->printComments ($ comments , $ beforeAsteriskIndent , $ afterAsteriskIndent );
621
- $ result .= sprintf ('%s%s*%s ' , $ originalTokens ->getDetectedNewline () ?? "\n" , $ beforeAsteriskIndent , $ afterAsteriskIndent );
622
- }
623
- }
624
-
625
- $ result .= $ this ->printNodeFormatPreserving ($ arrItem , $ originalTokens );
602
+ $ result .= $ this ->printNodeFormatPreserving ($ newNode , $ originalTokens );
626
603
if ($ addParentheses ) {
627
604
$ result .= ') ' ;
628
605
}
@@ -632,58 +609,52 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
632
609
if ($ insertStr === null ) {
633
610
return null ;
634
611
}
635
- if (!$ arrItem instanceof Node) {
612
+ if (!$ newNode instanceof Node) {
636
613
return null ;
637
614
}
638
615
639
- if ($ insertStr === ', ' && $ isMultiline || count ($ arrItem ->getAttribute (Attribute::COMMENTS ) ?? []) > 0 ) {
616
+ if ($ insertStr === ', ' && $ isMultiline || count ($ newNode ->getAttribute (Attribute::COMMENTS ) ?? []) > 0 ) {
640
617
$ insertStr = ', ' ;
641
618
$ insertNewline = true ;
642
619
}
643
620
644
621
if ($ beforeFirstKeepOrReplace ) {
645
622
// Will be inserted at the next "replace" or "keep" element
646
- $ delayedAdd [] = $ arrItem ;
623
+ $ delayedAdd [] = $ newNode ;
647
624
continue ;
648
625
}
649
626
650
627
/** @var int $itemEndPos */
651
628
$ itemEndPos = $ tokenIndex - 1 ;
652
629
if ($ insertNewline ) {
653
- $ comments = $ arrItem ->getAttribute (Attribute::COMMENTS ) ?? [];
654
- $ result .= $ insertStr ;
655
- if (count ($ comments ) > 0 ) {
656
- $ result .= sprintf ('%s%s*%s ' , $ originalTokens ->getDetectedNewline () ?? "\n" , $ beforeAsteriskIndent , $ afterAsteriskIndent );
657
- $ result .= $ this ->printComments ($ comments , $ beforeAsteriskIndent , $ afterAsteriskIndent );
658
- }
659
- $ result .= sprintf ('%s%s*%s ' , $ originalTokens ->getDetectedNewline () ?? "\n" , $ beforeAsteriskIndent , $ afterAsteriskIndent );
630
+ $ result .= $ insertStr . sprintf ('%s%s*%s ' , $ originalTokens ->getDetectedNewline () ?? "\n" , $ beforeAsteriskIndent , $ afterAsteriskIndent );
660
631
} else {
661
632
$ result .= $ insertStr ;
662
633
}
663
634
664
635
$ parenthesesNeeded = isset ($ this ->parenthesesListMap [$ mapKey ])
665
- && in_array (get_class ($ arrItem ), $ this ->parenthesesListMap [$ mapKey ], true );
636
+ && in_array (get_class ($ newNode ), $ this ->parenthesesListMap [$ mapKey ], true );
666
637
if ($ parenthesesNeeded ) {
667
638
$ result .= '( ' ;
668
639
}
669
640
670
- $ result .= $ this ->printNodeFormatPreserving ($ arrItem , $ originalTokens );
641
+ $ result .= $ this ->printNodeFormatPreserving ($ newNode , $ originalTokens );
671
642
if ($ parenthesesNeeded ) {
672
643
$ result .= ') ' ;
673
644
}
674
645
675
646
$ tokenIndex = $ itemEndPos + 1 ;
676
647
677
648
} elseif ($ diffType === DiffElem::TYPE_REMOVE ) {
678
- if (!$ origArrayItem instanceof Node) {
649
+ if (!$ originalNode instanceof Node) {
679
650
return null ;
680
651
}
681
652
682
653
/** @var int $itemStartPos */
683
- $ itemStartPos = $ origArrayItem ->getAttribute (Attribute::START_INDEX );
654
+ $ itemStartPos = $ originalNode ->getAttribute (Attribute::START_INDEX );
684
655
685
656
/** @var int $itemEndPos */
686
- $ itemEndPos = $ origArrayItem ->getAttribute (Attribute::END_INDEX );
657
+ $ itemEndPos = $ originalNode ->getAttribute (Attribute::END_INDEX );
687
658
if ($ itemStartPos < 0 || $ itemEndPos < 0 ) {
688
659
throw new LogicException ();
689
660
}
0 commit comments