Skip to content

Commit 2df613c

Browse files
committed
support changing of comments
1 parent 7e14184 commit 2df613c

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/Printer/Printer.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,15 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
535535
throw new LogicException();
536536
}
537537

538-
// $comments = $arrItem->getComments();
539-
// $origComments = $origArrayItem->getComments();
538+
$comments = $arrItem->getAttribute(Attribute::COMMENTS) ?? [];
539+
$origComments = $origArrayItem->getAttribute(Attribute::COMMENTS) ?? [];
540+
541+
if(count($comments) && !count($origComments)) {
542+
$idfdf= 5;
543+
}
544+
545+
$commentStartPos = count($origComments) ? $origComments[0]->startIndex : $itemStartPos;
546+
\assert($commentStartPos >= 0);
540547

541548
$result .= $originalTokens->getContentBetween($tokenIndex, $itemStartPos);
542549

@@ -570,6 +577,13 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
570577
$result .= '(';
571578
}
572579

580+
if ($comments !== $origComments) {
581+
if ($comments) {
582+
$result .= $this->pComments($comments, $beforeAsteriskIndent, $afterAsteriskIndent);// . "\n";
583+
$result .= sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent);
584+
}
585+
}
586+
573587
$result .= $this->printNodeFormatPreserving($arrItem, $originalTokens);
574588
if ($addParentheses) {
575589
$result .= ')';

tests/PHPStan/Printer/PrinterTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ public function enterNode(Node $node)
664664

665665
};
666666

667-
$addCommentsToMultilineArrayShape = new class extends AbstractNodeVisitor {
667+
$addItemsWithCommentsToMultilineArrayShape = new class extends AbstractNodeVisitor {
668668

669669
public function enterNode(Node $node)
670670
{
@@ -725,7 +725,35 @@ public function enterNode(Node $node)
725725
* e: string
726726
* }
727727
*/',
728-
$addCommentsToMultilineArrayShape,
728+
$addItemsWithCommentsToMultilineArrayShape,
729+
];
730+
731+
$changeCommentOnArrayShapeItem = new class extends AbstractNodeVisitor {
732+
733+
public function enterNode(Node $node)
734+
{
735+
if ($node instanceof ArrayShapeItemNode) {
736+
$node->setAttribute(Attribute::COMMENTS, [new Comment('// puppies')]);
737+
}
738+
739+
return $node;
740+
}
741+
742+
};
743+
744+
yield [
745+
'/**
746+
* @return array{
747+
* a: int,
748+
* }
749+
*/',
750+
'/**
751+
* @return array{
752+
* // puppies
753+
* a: int,
754+
* }
755+
*/',
756+
$changeCommentOnArrayShapeItem,
729757
];
730758

731759

0 commit comments

Comments
 (0)