Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
}
}

$result .= $this->printNodeFormatPreserving($delayedAddNode, $originalTokens);
$result = $this->appendNode($result, $this->printNodeFormatPreserving($delayedAddNode, $originalTokens), $afterAsteriskIndent);
if ($parenthesesNeeded) {
$result .= ')';
}
Expand Down Expand Up @@ -747,7 +747,7 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
$result .= '(';
}

$result .= $this->printNodeFormatPreserving($arrItem, $originalTokens);
$result = $this->appendNode($result, $this->printNodeFormatPreserving($arrItem, $originalTokens), $afterAsteriskIndent);
if ($parenthesesNeeded) {
$result .= ')';
}
Expand Down Expand Up @@ -818,7 +818,7 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
}
}

$result .= $this->printNodeFormatPreserving($delayedAddNode, $originalTokens);
$result = $this->appendNode($result, $this->printNodeFormatPreserving($delayedAddNode, $originalTokens), $afterAsteriskIndent);
$first = false;
}
$result .= $extraRight;
Expand All @@ -827,6 +827,19 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
return $result;
}

/**
* A new empty line in a docblock is " *", without the space after the asterisk
*/
private function appendNode(string $result, string $printedNode, string $afterAsteriskIndent): string
{
$lineStart = '*' . $afterAsteriskIndent;
if ($printedNode !== '' || $afterAsteriskIndent === '' || substr($result, -strlen($lineStart)) !== $lineStart) {
return $result . $printedNode;
}

return substr($result, 0, -strlen($afterAsteriskIndent));
}

/**
* @param list<Comment> $comments
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Printer/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2937,6 +2937,32 @@ public function enterNode(Node $node)
*/'),
$unsetSecond,
];

yield [
self::nowdoc('
/**
* Text.
*/'),
self::nowdoc('
/**
* Text.
*
* @return int
*/'),
new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
if ($node instanceof PhpDocNode) {
$node->children[] = new PhpDocTextNode('');
$node->children[] = new PhpDocTagNode('@return', new ReturnTagValueNode(new IdentifierTypeNode('int'), ''));
}

return $node;
}

},
];
}

/**
Expand Down
Loading