4
4
5
5
use LogicException ;
6
6
use PHPStan \PhpDocParser \Ast \Attribute ;
7
+ use PHPStan \PhpDocParser \Ast \Comment ;
7
8
use PHPStan \PhpDocParser \Ast \ConstExpr \ConstExprArrayNode ;
8
9
use PHPStan \PhpDocParser \Ast \ConstExpr \ConstExprNode ;
9
10
use PHPStan \PhpDocParser \Ast \Node ;
@@ -521,19 +522,22 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
521
522
522
523
foreach ($ diff as $ i => $ diffElem ) {
523
524
$ diffType = $ diffElem ->type ;
524
- $ newNode = $ diffElem ->new ;
525
- $ originalNode = $ diffElem ->old ;
525
+ $ arrItem = $ diffElem ->new ;
526
+ $ origArrayItem = $ diffElem ->old ;
526
527
if ($ diffType === DiffElem::TYPE_KEEP || $ diffType === DiffElem::TYPE_REPLACE ) {
527
528
$ beforeFirstKeepOrReplace = false ;
528
- if (!$ newNode instanceof Node || !$ originalNode instanceof Node) {
529
+ if (!$ arrItem instanceof Node || !$ origArrayItem instanceof Node) {
529
530
return null ;
530
531
}
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 );
533
534
if ($ itemStartPos < 0 || $ itemEndPos < 0 || $ itemStartPos < $ tokenIndex ) {
534
535
throw new LogicException ();
535
536
}
536
537
538
+ // $comments = $arrItem->getComments();
539
+ // $origComments = $origArrayItem->getComments();
540
+
537
541
$ result .= $ originalTokens ->getContentBetween ($ tokenIndex , $ itemStartPos );
538
542
539
543
if (count ($ delayedAdd ) > 0 ) {
@@ -559,14 +563,14 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
559
563
}
560
564
561
565
$ 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 );
564
568
$ addParentheses = $ parenthesesNeeded && !$ originalTokens ->hasParentheses ($ itemStartPos , $ itemEndPos );
565
569
if ($ addParentheses ) {
566
570
$ result .= '( ' ;
567
571
}
568
572
569
- $ result .= $ this ->printNodeFormatPreserving ($ newNode , $ originalTokens );
573
+ $ result .= $ this ->printNodeFormatPreserving ($ arrItem , $ originalTokens );
570
574
if ($ addParentheses ) {
571
575
$ result .= ') ' ;
572
576
}
@@ -576,48 +580,54 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
576
580
if ($ insertStr === null ) {
577
581
return null ;
578
582
}
579
- if (!$ newNode instanceof Node) {
583
+ if (!$ arrItem instanceof Node) {
580
584
return null ;
581
585
}
582
586
583
- if ($ insertStr === ', ' && $ isMultiline ) {
587
+ if ($ insertStr === ', ' && $ isMultiline || count ( $ arrItem -> getComments ()) > 0 ) {
584
588
$ insertStr = ', ' ;
585
589
$ insertNewline = true ;
586
590
}
587
591
588
592
if ($ beforeFirstKeepOrReplace ) {
589
593
// Will be inserted at the next "replace" or "keep" element
590
- $ delayedAdd [] = $ newNode ;
594
+ $ delayedAdd [] = $ arrItem ;
591
595
continue ;
592
596
}
593
597
594
598
$ itemEndPos = $ tokenIndex - 1 ;
595
599
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 );
597
607
} else {
598
608
$ result .= $ insertStr ;
599
609
}
600
610
601
611
$ 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 );
603
613
if ($ parenthesesNeeded ) {
604
614
$ result .= '( ' ;
605
615
}
606
616
607
- $ result .= $ this ->printNodeFormatPreserving ($ newNode , $ originalTokens );
617
+ $ result .= $ this ->printNodeFormatPreserving ($ arrItem , $ originalTokens );
608
618
if ($ parenthesesNeeded ) {
609
619
$ result .= ') ' ;
610
620
}
611
621
612
622
$ tokenIndex = $ itemEndPos + 1 ;
613
623
614
624
} elseif ($ diffType === DiffElem::TYPE_REMOVE ) {
615
- if (!$ originalNode instanceof Node) {
625
+ if (!$ origArrayItem instanceof Node) {
616
626
return null ;
617
627
}
618
628
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 );
621
631
if ($ itemStartPos < 0 || $ itemEndPos < 0 ) {
622
632
throw new LogicException ();
623
633
}
@@ -675,6 +685,20 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
675
685
return $ result ;
676
686
}
677
687
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
+
678
702
/**
679
703
* @param Node[] $nodes
680
704
* @return array{bool, string, string}
0 commit comments