@@ -550,30 +550,23 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
550550
551551 foreach ($ diff as $ i => $ diffElem ) {
552552 $ diffType = $ diffElem ->type ;
553- $ arrItem = $ diffElem ->new ;
554- $ origArrayItem = $ diffElem ->old ;
553+ $ newNode = $ diffElem ->new ;
554+ $ originalNode = $ diffElem ->old ;
555555 if ($ diffType === DiffElem::TYPE_KEEP || $ diffType === DiffElem::TYPE_REPLACE ) {
556556 $ beforeFirstKeepOrReplace = false ;
557- if (!$ arrItem instanceof Node || !$ origArrayItem instanceof Node) {
557+ if (!$ newNode instanceof Node || !$ originalNode instanceof Node) {
558558 return null ;
559559 }
560560
561561 /** @var int $itemStartPos */
562- $ itemStartPos = $ origArrayItem ->getAttribute (Attribute::START_INDEX );
562+ $ itemStartPos = $ originalNode ->getAttribute (Attribute::START_INDEX );
563563
564564 /** @var int $itemEndPos */
565- $ itemEndPos = $ origArrayItem ->getAttribute (Attribute::END_INDEX );
566-
565+ $ itemEndPos = $ originalNode ->getAttribute (Attribute::END_INDEX );
567566 if ($ itemStartPos < 0 || $ itemEndPos < 0 || $ itemStartPos < $ tokenIndex ) {
568567 throw new LogicException ();
569568 }
570569
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-
577570 $ result .= $ originalTokens ->getContentBetween ($ tokenIndex , $ itemStartPos );
578571
579572 if (count ($ delayedAdd ) > 0 ) {
@@ -584,14 +577,6 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
584577 $ result .= '( ' ;
585578 }
586579
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-
595580 $ result .= $ this ->printNodeFormatPreserving ($ delayedAddNode , $ originalTokens );
596581 if ($ parenthesesNeeded ) {
597582 $ result .= ') ' ;
@@ -608,21 +593,14 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
608593 }
609594
610595 $ 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 );
596+ && in_array (get_class ($ newNode ), $ this ->parenthesesListMap [$ mapKey ], true )
597+ && !in_array (get_class ($ originalNode ), $ this ->parenthesesListMap [$ mapKey ], true );
613598 $ addParentheses = $ parenthesesNeeded && !$ originalTokens ->hasParentheses ($ itemStartPos , $ itemEndPos );
614599 if ($ addParentheses ) {
615600 $ result .= '( ' ;
616601 }
617602
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 );
603+ $ result .= $ this ->printNodeFormatPreserving ($ newNode , $ originalTokens );
626604 if ($ addParentheses ) {
627605 $ result .= ') ' ;
628606 }
@@ -632,25 +610,25 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
632610 if ($ insertStr === null ) {
633611 return null ;
634612 }
635- if (!$ arrItem instanceof Node) {
613+ if (!$ newNode instanceof Node) {
636614 return null ;
637615 }
638616
639- if ($ insertStr === ', ' && $ isMultiline || count ($ arrItem ->getAttribute (Attribute::COMMENTS ) ?? []) > 0 ) {
617+ if ($ insertStr === ', ' && $ isMultiline || count ($ newNode ->getAttribute (Attribute::COMMENTS ) ?? []) > 0 ) {
640618 $ insertStr = ', ' ;
641619 $ insertNewline = true ;
642620 }
643621
644622 if ($ beforeFirstKeepOrReplace ) {
645623 // Will be inserted at the next "replace" or "keep" element
646- $ delayedAdd [] = $ arrItem ;
624+ $ delayedAdd [] = $ newNode ;
647625 continue ;
648626 }
649627
650628 /** @var int $itemEndPos */
651629 $ itemEndPos = $ tokenIndex - 1 ;
652630 if ($ insertNewline ) {
653- $ comments = $ arrItem ->getAttribute (Attribute::COMMENTS ) ?? [];
631+ $ comments = $ newNode ->getAttribute (Attribute::COMMENTS ) ?? [];
654632 $ result .= $ insertStr ;
655633 if (count ($ comments ) > 0 ) {
656634 $ result .= sprintf ('%s%s*%s ' , $ originalTokens ->getDetectedNewline () ?? "\n" , $ beforeAsteriskIndent , $ afterAsteriskIndent );
@@ -662,28 +640,28 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
662640 }
663641
664642 $ parenthesesNeeded = isset ($ this ->parenthesesListMap [$ mapKey ])
665- && in_array (get_class ($ arrItem ), $ this ->parenthesesListMap [$ mapKey ], true );
643+ && in_array (get_class ($ newNode ), $ this ->parenthesesListMap [$ mapKey ], true );
666644 if ($ parenthesesNeeded ) {
667645 $ result .= '( ' ;
668646 }
669647
670- $ result .= $ this ->printNodeFormatPreserving ($ arrItem , $ originalTokens );
648+ $ result .= $ this ->printNodeFormatPreserving ($ newNode , $ originalTokens );
671649 if ($ parenthesesNeeded ) {
672650 $ result .= ') ' ;
673651 }
674652
675653 $ tokenIndex = $ itemEndPos + 1 ;
676654
677655 } elseif ($ diffType === DiffElem::TYPE_REMOVE ) {
678- if (!$ origArrayItem instanceof Node) {
656+ if (!$ originalNode instanceof Node) {
679657 return null ;
680658 }
681659
682660 /** @var int $itemStartPos */
683- $ itemStartPos = $ origArrayItem ->getAttribute (Attribute::START_INDEX );
661+ $ itemStartPos = $ originalNode ->getAttribute (Attribute::START_INDEX );
684662
685663 /** @var int $itemEndPos */
686- $ itemEndPos = $ origArrayItem ->getAttribute (Attribute::END_INDEX );
664+ $ itemEndPos = $ originalNode ->getAttribute (Attribute::END_INDEX );
687665 if ($ itemStartPos < 0 || $ itemEndPos < 0 ) {
688666 throw new LogicException ();
689667 }
0 commit comments