Skip to content

Commit 21d6e6f

Browse files
committed
Printer: new empty line in docblock without trailing space
A new empty PhpDocTextNode was printed as " * ". print() prints " *". Amp-Thread-ID: https://ampcode.com/threads/T-01a17930-0285-73c5-a957-4325c77aa1b2
1 parent 88fdee7 commit 21d6e6f

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎src/Printer/Printer.php‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
672672
}
673673
}
674674

675-
$result .= $this->printNodeFormatPreserving($delayedAddNode, $originalTokens);
675+
$result = $this->appendNode($result, $this->printNodeFormatPreserving($delayedAddNode, $originalTokens), $afterAsteriskIndent);
676676
if ($parenthesesNeeded) {
677677
$result .= ')';
678678
}
@@ -747,7 +747,7 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
747747
$result .= '(';
748748
}
749749

750-
$result .= $this->printNodeFormatPreserving($arrItem, $originalTokens);
750+
$result = $this->appendNode($result, $this->printNodeFormatPreserving($arrItem, $originalTokens), $afterAsteriskIndent);
751751
if ($parenthesesNeeded) {
752752
$result .= ')';
753753
}
@@ -818,7 +818,7 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
818818
}
819819
}
820820

821-
$result .= $this->printNodeFormatPreserving($delayedAddNode, $originalTokens);
821+
$result = $this->appendNode($result, $this->printNodeFormatPreserving($delayedAddNode, $originalTokens), $afterAsteriskIndent);
822822
$first = false;
823823
}
824824
$result .= $extraRight;
@@ -827,6 +827,19 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
827827
return $result;
828828
}
829829

830+
/**
831+
* A new empty line in a docblock is " *", without the space after the asterisk
832+
*/
833+
private function appendNode(string $result, string $printedNode, string $afterAsteriskIndent): string
834+
{
835+
$lineStart = '*' . $afterAsteriskIndent;
836+
if ($printedNode !== '' || $afterAsteriskIndent === '' || substr($result, -strlen($lineStart)) !== $lineStart) {
837+
return $result . $printedNode;
838+
}
839+
840+
return substr($result, 0, -strlen($afterAsteriskIndent));
841+
}
842+
830843
/**
831844
* @param list<Comment> $comments
832845
*/

‎tests/PHPStan/Printer/PrinterTest.php‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,6 +2937,32 @@ public function enterNode(Node $node)
29372937
*/'),
29382938
$unsetSecond,
29392939
];
2940+
2941+
yield [
2942+
self::nowdoc('
2943+
/**
2944+
* Text.
2945+
*/'),
2946+
self::nowdoc('
2947+
/**
2948+
* Text.
2949+
*
2950+
* @return int
2951+
*/'),
2952+
new class extends AbstractNodeVisitor {
2953+
2954+
public function enterNode(Node $node)
2955+
{
2956+
if ($node instanceof PhpDocNode) {
2957+
$node->children[] = new PhpDocTextNode('');
2958+
$node->children[] = new PhpDocTagNode('@return', new ReturnTagValueNode(new IdentifierTypeNode('int'), ''));
2959+
}
2960+
2961+
return $node;
2962+
}
2963+
2964+
},
2965+
];
29402966
}
29412967

29422968
/**

0 commit comments

Comments
 (0)