44
55use LogicException ;
66use PHPStan \PhpDocParser \Ast \Attribute ;
7+ use PHPStan \PhpDocParser \Ast \Comment ;
78use PHPStan \PhpDocParser \Ast \ConstExpr \ConstExprArrayNode ;
89use PHPStan \PhpDocParser \Ast \ConstExpr \ConstExprNode ;
910use PHPStan \PhpDocParser \Ast \Node ;
@@ -521,19 +522,22 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
521522
522523 foreach ($ diff as $ i => $ diffElem ) {
523524 $ diffType = $ diffElem ->type ;
524- $ newNode = $ diffElem ->new ;
525- $ originalNode = $ diffElem ->old ;
525+ $ arrItem = $ diffElem ->new ;
526+ $ origArrayItem = $ diffElem ->old ;
526527 if ($ diffType === DiffElem::TYPE_KEEP || $ diffType === DiffElem::TYPE_REPLACE ) {
527528 $ beforeFirstKeepOrReplace = false ;
528- if (!$ newNode instanceof Node || !$ originalNode instanceof Node) {
529+ if (!$ arrItem instanceof Node || !$ origArrayItem instanceof Node) {
529530 return null ;
530531 }
531- $ itemStartPos = $ originalNode ->getAttribute (Attribute::START_INDEX );
532- $ itemEndPos = $ originalNode ->getAttribute (Attribute::END_INDEX );
532+ $ itemStartPos = $ origArrayItem ->getAttribute (Attribute::START_INDEX );
533+ $ itemEndPos = $ origArrayItem ->getAttribute (Attribute::END_INDEX );
533534 if ($ itemStartPos < 0 || $ itemEndPos < 0 || $ itemStartPos < $ tokenIndex ) {
534535 throw new LogicException ();
535536 }
536537
538+ // $comments = $arrItem->getComments();
539+ // $origComments = $origArrayItem->getComments();
540+
537541 $ result .= $ originalTokens ->getContentBetween ($ tokenIndex , $ itemStartPos );
538542
539543 if (count ($ delayedAdd ) > 0 ) {
@@ -559,14 +563,14 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
559563 }
560564
561565 $ parenthesesNeeded = isset ($ this ->parenthesesListMap [$ mapKey ])
562- && in_array (get_class ($ newNode ), $ this ->parenthesesListMap [$ mapKey ], true )
563- && !in_array (get_class ($ originalNode ), $ this ->parenthesesListMap [$ mapKey ], true );
566+ && in_array (get_class ($ arrItem ), $ this ->parenthesesListMap [$ mapKey ], true )
567+ && !in_array (get_class ($ origArrayItem ), $ this ->parenthesesListMap [$ mapKey ], true );
564568 $ addParentheses = $ parenthesesNeeded && !$ originalTokens ->hasParentheses ($ itemStartPos , $ itemEndPos );
565569 if ($ addParentheses ) {
566570 $ result .= '( ' ;
567571 }
568572
569- $ result .= $ this ->printNodeFormatPreserving ($ newNode , $ originalTokens );
573+ $ result .= $ this ->printNodeFormatPreserving ($ arrItem , $ originalTokens );
570574 if ($ addParentheses ) {
571575 $ result .= ') ' ;
572576 }
@@ -576,48 +580,54 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
576580 if ($ insertStr === null ) {
577581 return null ;
578582 }
579- if (!$ newNode instanceof Node) {
583+ if (!$ arrItem instanceof Node) {
580584 return null ;
581585 }
582586
583- if ($ insertStr === ', ' && $ isMultiline ) {
587+ if ($ insertStr === ', ' && $ isMultiline || count ( $ arrItem -> getComments ()) > 0 ) {
584588 $ insertStr = ', ' ;
585589 $ insertNewline = true ;
586590 }
587591
588592 if ($ beforeFirstKeepOrReplace ) {
589593 // Will be inserted at the next "replace" or "keep" element
590- $ delayedAdd [] = $ newNode ;
594+ $ delayedAdd [] = $ arrItem ;
591595 continue ;
592596 }
593597
594598 $ itemEndPos = $ tokenIndex - 1 ;
595599 if ($ insertNewline ) {
596- $ result .= $ insertStr . sprintf ('%s%s*%s ' , $ originalTokens ->getDetectedNewline () ?? "\n" , $ beforeAsteriskIndent , $ afterAsteriskIndent );
600+ $ comments = $ arrItem ->getComments ();
601+ $ result .= $ insertStr ;
602+ if (count ($ comments ) > 0 ) {
603+ $ result .= sprintf ('%s%s*%s ' , $ originalTokens ->getDetectedNewline () ?? "\n" , $ beforeAsteriskIndent , $ afterAsteriskIndent );
604+ $ result .= $ this ->pComments ($ comments );
605+ }
606+ $ result .= sprintf ('%s%s*%s ' , $ originalTokens ->getDetectedNewline () ?? "\n" , $ beforeAsteriskIndent , $ afterAsteriskIndent );
597607 } else {
598608 $ result .= $ insertStr ;
599609 }
600610
601611 $ parenthesesNeeded = isset ($ this ->parenthesesListMap [$ mapKey ])
602- && in_array (get_class ($ newNode ), $ this ->parenthesesListMap [$ mapKey ], true );
612+ && in_array (get_class ($ arrItem ), $ this ->parenthesesListMap [$ mapKey ], true );
603613 if ($ parenthesesNeeded ) {
604614 $ result .= '( ' ;
605615 }
606616
607- $ result .= $ this ->printNodeFormatPreserving ($ newNode , $ originalTokens );
617+ $ result .= $ this ->printNodeFormatPreserving ($ arrItem , $ originalTokens );
608618 if ($ parenthesesNeeded ) {
609619 $ result .= ') ' ;
610620 }
611621
612622 $ tokenIndex = $ itemEndPos + 1 ;
613623
614624 } elseif ($ diffType === DiffElem::TYPE_REMOVE ) {
615- if (!$ originalNode instanceof Node) {
625+ if (!$ origArrayItem instanceof Node) {
616626 return null ;
617627 }
618628
619- $ itemStartPos = $ originalNode ->getAttribute (Attribute::START_INDEX );
620- $ itemEndPos = $ originalNode ->getAttribute (Attribute::END_INDEX );
629+ $ itemStartPos = $ origArrayItem ->getAttribute (Attribute::START_INDEX );
630+ $ itemEndPos = $ origArrayItem ->getAttribute (Attribute::END_INDEX );
621631 if ($ itemStartPos < 0 || $ itemEndPos < 0 ) {
622632 throw new LogicException ();
623633 }
@@ -675,6 +685,20 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
675685 return $ result ;
676686 }
677687
688+ /**
689+ * @param array<Comment> $comments
690+ */
691+ protected function pComments (array $ comments ): string
692+ {
693+ $ formattedComments = [];
694+
695+ foreach ($ comments as $ comment ) {
696+ $ formattedComments [] = $ comment ->getReformattedText () ?? '' ;
697+ }
698+
699+ return implode ("\n" , $ formattedComments );
700+ }
701+
678702 /**
679703 * @param Node[] $nodes
680704 * @return array{bool, string, string}
0 commit comments