Skip to content

Commit 8c22c85

Browse files
committed
support multiline comments in add flow
1 parent 44f36e7 commit 8c22c85

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/Printer/Printer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
601601
$result .= $insertStr;
602602
if (count($comments) > 0) {
603603
$result .= sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent);
604-
$result .= $this->pComments($comments);
604+
$result .= $this->pComments($comments, $beforeAsteriskIndent, $afterAsteriskIndent);
605605
}
606606
$result .= sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent);
607607
} else {
@@ -688,12 +688,12 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
688688
/**
689689
* @param array<Comment> $comments
690690
*/
691-
protected function pComments(array $comments): string
691+
protected function pComments(array $comments, string $beforeAsteriskIndent, string $afterAsteriskIndent): string
692692
{
693693
$formattedComments = [];
694694

695695
foreach ($comments as $comment) {
696-
$formattedComments[] = $comment->getReformattedText() ?? '';
696+
$formattedComments[] = str_replace("\n", "\n".$beforeAsteriskIndent."*".$afterAsteriskIndent, $comment->getReformattedText() ?? '');
697697
}
698698

699699
return implode("\n", $formattedComments);

tests/PHPStan/Printer/PrinterTest.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -664,17 +664,33 @@ public function enterNode(Node $node)
664664

665665
};
666666

667-
$addItemsToMultilineArrayShape = new class extends AbstractNodeVisitor {
667+
$addSingleLineCommentsToMultilineArrayShape = new class extends AbstractNodeVisitor {
668668

669669
public function enterNode(Node $node)
670670
{
671671
if ($node instanceof ArrayShapeNode) {
672-
$commentedNode = new ArrayShapeItemNode(new IdentifierTypeNode('c'), false, new IdentifierTypeNode('int'));
672+
$commentedNode = new ArrayShapeItemNode(new IdentifierTypeNode('b'), false, new IdentifierTypeNode('int'));
673673
$commentedNode->setAttribute(Attribute::COMMENTS, [new Comment('// bar')]);
674674
array_splice($node->items, 1, 0, [
675675
$commentedNode,
676676
]);
677-
$node->items[] = new ArrayShapeItemNode(new IdentifierTypeNode('d'), false, new IdentifierTypeNode('string'));
677+
$commentedNode = new ArrayShapeItemNode(new IdentifierTypeNode('d'), false, new IdentifierTypeNode('string'));
678+
$commentedNode->setAttribute(Attribute::COMMENTS, [new Comment(
679+
'/* This is a
680+
multiline
681+
comment
682+
*/'
683+
)]);
684+
$node->items[] = $commentedNode;
685+
686+
$commentedNode = new ArrayShapeItemNode(new IdentifierTypeNode('e'), false, new IdentifierTypeNode('string'));
687+
$commentedNode->setAttribute(Attribute::COMMENTS, [new Comment(
688+
'/* Another
689+
* multiline
690+
* comment with leading asterisks
691+
*/'
692+
)]);
693+
$node->items[] = $commentedNode;
678694
}
679695

680696
return $node;
@@ -687,22 +703,32 @@ public function enterNode(Node $node)
687703
* @return array{
688704
* // foo
689705
* a: int,
690-
* b: string
706+
* c: string
691707
* }
692708
*/',
693709
'/**
694710
* @return array{
695711
* // foo
696712
* a: int,
697713
* // bar
698-
* c: int,
699-
* b: string,
700-
* d: string
714+
* b: int,
715+
* c: string,
716+
* /* This is a
717+
* multiline
718+
* comment
719+
* */
720+
* d: string,
721+
* /* Another
722+
* * multiline
723+
* * comment with leading asterisks
724+
* */
725+
* e: string
701726
* }
702727
*/',
703-
$addItemsToMultilineArrayShape,
728+
$addSingleLineCommentsToMultilineArrayShape,
704729
];
705730

731+
706732
yield [
707733
'/**
708734
* @return array{float}

0 commit comments

Comments
 (0)