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
39 changes: 39 additions & 0 deletions src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
use function implode;
use function in_array;
use function is_array;
use function preg_match;
use function preg_match_all;
use function preg_replace_callback;
use function sprintf;
Expand Down Expand Up @@ -608,6 +609,11 @@ private function printArrayFormatPreserving(array $nodes, array $originalNodes,
[$isMultiline, $beforeAsteriskIndent, $afterAsteriskIndent] = $this->isMultiline($tokenIndex, $originalNodes, $originalTokens);

if ($insertStr === "\n * ") {
$lastChildIndent = $this->detectLastChildIndent($originalNodes, $originalTokens);
if ($lastChildIndent !== null) {
[$beforeAsteriskIndent, $afterAsteriskIndent] = $lastChildIndent;
}

$insertStr = sprintf('%s%s*%s', $originalTokens->getDetectedNewline() ?? "\n", $beforeAsteriskIndent, $afterAsteriskIndent);
}

Expand Down Expand Up @@ -869,6 +875,39 @@ private function isMultiline(int $initialIndex, array $nodes, TokenIterator $ori
return [$isMultiline, $before, $after];
}

/**
* Indentation before and after the asterisk on the line where the last child of a PHPDoc starts.
* Other lines, e.g. the next lines of a multi-line description, can have a different indentation.
*
* @param Node[] $originalNodes
* @return array{string, string}|null
*/
private function detectLastChildIndent(array $originalNodes, TokenIterator $originalTokens): ?array
{
$tokens = $originalTokens->getTokens();
for ($i = count($originalNodes) - 1; $i >= 0; $i--) {
$index = $originalNodes[$i]->getAttribute(Attribute::START_INDEX) - 1;
$lineStart = '';
while (isset($tokens[$index]) && $tokens[$index][Lexer::TYPE_OFFSET] === Lexer::TOKEN_HORIZONTAL_WS) {
$lineStart = $tokens[$index][Lexer::VALUE_OFFSET] . $lineStart;
$index--;
}

if (!isset($tokens[$index]) || $tokens[$index][Lexer::TYPE_OFFSET] !== Lexer::TOKEN_PHPDOC_EOL) {
continue;
}

$lineStart = $tokens[$index][Lexer::VALUE_OFFSET] . $lineStart;
if (preg_match('~\n(?<before>[\x09\x20]*)\*(?<after>[\x09\x20]*)$~', $lineStart, $match) !== 1) {
continue;
}

return [$match['before'], $match['after']];
}

return null;
}

private function printNodeFormatPreserving(Node $node, TokenIterator $originalTokens): string
{
/** @var Node|null $originalNode */
Expand Down
46 changes: 46 additions & 0 deletions tests/PHPStan/Printer/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PureUnlessParameterIsPassedTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasImportTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
Expand Down Expand Up @@ -195,6 +196,51 @@ public function enterNode(Node $node)
$removeFirst,
];

$addThrows = new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
if ($node instanceof PhpDocNode) {
$node->children[] = new PhpDocTagNode('@throws', new ThrowsTagValueNode(new IdentifierTypeNode('Exception'), ''));
}

return $node;
}

};

yield [
self::nowdoc('
/**
* @param int $foo multi
* line description
*/'),
self::nowdoc('
/**
* @param int $foo multi
* line description
* @throws Exception
*/'),
$addThrows,
];

yield [
self::nowdoc('
/**
* @param int $foo multi
* line description
* @return void
*/'),
self::nowdoc('
/**
* @param int $foo multi
* line description
* @return void
* @throws Exception
*/'),
$addThrows,
];

$removeLast = new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
Expand Down
Loading