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
30 changes: 30 additions & 0 deletions src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,22 @@ private function printNodeFormatPreserving(Node $node, TokenIterator $originalTo
continue;
}

if ($this->isRenamedTag($node, $subNodeName, $originalTokens, $startPos)) {
// the tag name is the first token of the tag; keep the format of the value
$result .= $subNode;
$pos = $startPos + 1;
continue;
}

if (
$node instanceof DoctrineAnnotation
&& $subNodeName === 'name'
&& $node->getAttribute(Attribute::ORIGINAL_NODE)->name !== $originalTokens->getContentBetween($startPos, $startPos + 1)
) {
// the tokens of the annotation of a Doctrine tag do not contain the name, the tag prints it
continue;
}

if (is_array($subNode) && is_array($origSubNode)) {
// Array subnode changed, we might be able to reconstruct it
$listResult = $this->printArrayFormatPreserving(
Expand Down Expand Up @@ -1053,6 +1069,20 @@ private function printNodeFormatPreserving(Node $node, TokenIterator $originalTo
return $result . $originalTokens->getContentBetween($pos, $endPos + 1);
}

private function isRenamedTag(Node $node, string $subNodeName, TokenIterator $originalTokens, int $startPos): bool
{
if (!$node instanceof PhpDocTagNode || $subNodeName !== 'name') {
return false;
}

$originalNode = $node->getAttribute(Attribute::ORIGINAL_NODE);
if (!$originalNode instanceof PhpDocTagNode) {
return false;
}

return $originalTokens->getContentBetween($startPos, $startPos + 1) === $originalNode->name;
}

/**
* Prints a node that replaces the tokens of the original node.
*
Expand Down
53 changes: 53 additions & 0 deletions tests/PHPStan/Printer/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,59 @@ public function enterNode(Node $node)
},
];

$renameDoctrineTagVisitor = new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
if ($node instanceof PhpDocTagNode && $node->value instanceof DoctrineTagValueNode) {
$node->name = '@Bar';
$node->value->annotation->name = '@Bar';
}

return $node;
}

};

yield [
'/**' . PHP_EOL .
' * @\Foo\Bar(' . PHP_EOL .
' * name="x",' . PHP_EOL .
' * cols={@\Foo\Col("a")}' . PHP_EOL .
' * )' . PHP_EOL .
' */',
'/**' . PHP_EOL .
' * @Bar(' . PHP_EOL .
' * name="x",' . PHP_EOL .
' * cols={@\Foo\Col("a")}' . PHP_EOL .
' * )' . PHP_EOL .
' */',
$renameDoctrineTagVisitor,
];

yield [
'/** @\Foo\Bar("a") some description */',
'/** @Bar("a") some description */',
$renameDoctrineTagVisitor,
];

yield [
'/** @param int $a */',
'/** @phpstan-param int $a */',
new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
if ($node instanceof PhpDocTagNode) {
$node->name = '@phpstan-param';
}

return $node;
}

},
];

yield [
'/**' . PHP_EOL .
' * @X({' . PHP_EOL .
Expand Down
Loading