Skip to content

Commit 786ee23

Browse files
committed
support editing of comments
1 parent 2df613c commit 786ee23

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/Printer/Printer.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
use PHPStan\PhpDocParser\Parser\TokenIterator;
6161
use function array_keys;
6262
use function array_map;
63+
use function assert;
6364
use function count;
6465
use function get_class;
6566
use function get_object_vars;
@@ -68,6 +69,7 @@
6869
use function is_array;
6970
use function preg_match_all;
7071
use function sprintf;
72+
use function str_replace;
7173
use function strlen;
7274
use function strpos;
7375
use function trim;
@@ -538,12 +540,8 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
538540
$comments = $arrItem->getAttribute(Attribute::COMMENTS) ?? [];
539541
$origComments = $origArrayItem->getAttribute(Attribute::COMMENTS) ?? [];
540542

541-
if(count($comments) && !count($origComments)) {
542-
$idfdf= 5;
543-
}
544-
545-
$commentStartPos = count($origComments) ? $origComments[0]->startIndex : $itemStartPos;
546-
\assert($commentStartPos >= 0);
543+
$commentStartPos = count($origComments) > 0 ? $origComments[0]->startIndex : $itemStartPos;
544+
assert($commentStartPos >= 0);
547545

548546
$result .= $originalTokens->getContentBetween($tokenIndex, $itemStartPos);
549547

@@ -578,7 +576,7 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
578576
}
579577

580578
if ($comments !== $origComments) {
581-
if ($comments) {
579+
if (count($comments) > 0) {
582580
$result .= $this->pComments($comments, $beforeAsteriskIndent, $afterAsteriskIndent);// . "\n";
583581
$result .= sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent);
584582
}
@@ -707,7 +705,7 @@ protected function pComments(array $comments, string $beforeAsteriskIndent, stri
707705
$formattedComments = [];
708706

709707
foreach ($comments as $comment) {
710-
$formattedComments[] = str_replace("\n", "\n".$beforeAsteriskIndent."*".$afterAsteriskIndent, $comment->getReformattedText() ?? '');
708+
$formattedComments[] = str_replace("\n", "\n" . $beforeAsteriskIndent . '*' . $afterAsteriskIndent, $comment->getReformattedText() ?? '');
711709
}
712710

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

tests/PHPStan/Printer/PrinterTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ public function enterNode(Node $node)
676676
]);
677677
$commentedNode = new ArrayShapeItemNode(new IdentifierTypeNode('d'), false, new IdentifierTypeNode('string'));
678678
$commentedNode->setAttribute(Attribute::COMMENTS, [new Comment(
679-
'/* This is a
679+
'/* This is a
680680
multiline
681681
comment
682682
*/'
@@ -756,7 +756,6 @@ public function enterNode(Node $node)
756756
$changeCommentOnArrayShapeItem,
757757
];
758758

759-
760759
yield [
761760
'/**
762761
* @return array{float}

0 commit comments

Comments
 (0)