Skip to content

Commit 6af2b82

Browse files
committed
Printer: keep Doctrine annotation name and missing parentheses
A Doctrine annotation without arguments, e.g. @\Foo\Id, has no tokens. The format-preserving printer reprinted the tag and added "()", also when nothing changed. The tokens of DoctrineTagValueNode do not contain the annotation name. When the printer reprinted only the value, the name was printed twice, e.g. @route@Route("/path") other description. Amp-Thread-ID: https://ampcode.com/threads/T-01a17930-0285-73c5-a957-4325c77aa1b2
1 parent 3fcdcf0 commit 6af2b82

2 files changed

Lines changed: 89 additions & 6 deletions

File tree

‎src/Printer/Printer.php‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ private function printNodeFormatPreserving(Node $node, TokenIterator $originalTo
855855
/** @var Node|null $originalNode */
856856
$originalNode = $node->getAttribute(Attribute::ORIGINAL_NODE);
857857
if ($originalNode === null) {
858-
return $this->print($node);
858+
return $this->printReplacement($node);
859859
}
860860

861861
$class = get_class($node);
@@ -897,14 +897,14 @@ private function printNodeFormatPreserving(Node $node, TokenIterator $originalTo
897897
);
898898

899899
if ($listResult === null) {
900-
return $this->print($node);
900+
return $this->printReplacement($node);
901901
}
902902

903903
$result .= $listResult;
904904
continue;
905905
}
906906

907-
return $this->print($node);
907+
return $this->printReplacement($node);
908908
}
909909

910910
if ($origSubNode === null) {
@@ -913,7 +913,7 @@ private function printNodeFormatPreserving(Node $node, TokenIterator $originalTo
913913
continue;
914914
}
915915

916-
return $this->print($node);
916+
return $this->printReplacement($node);
917917
}
918918

919919
$subStartPos = $origSubNode->getAttribute(Attribute::START_INDEX);
@@ -923,11 +923,16 @@ private function printNodeFormatPreserving(Node $node, TokenIterator $originalTo
923923
}
924924

925925
if ($subEndPos < $subStartPos) {
926-
return $this->print($node);
926+
// the original sub-node has no tokens, e.g. a Doctrine annotation without arguments
927+
if ($subNode !== null && $this->print($subNode) === $this->print($origSubNode)) {
928+
continue;
929+
}
930+
931+
return $this->printReplacement($node);
927932
}
928933

929934
if ($subNode === null) {
930-
return $this->print($node);
935+
return $this->printReplacement($node);
931936
}
932937

933938
$result .= $originalTokens->getContentBetween($pos, $subStartPos);
@@ -956,4 +961,30 @@ private function printNodeFormatPreserving(Node $node, TokenIterator $originalTo
956961
return $result . $originalTokens->getContentBetween($pos, $endPos + 1);
957962
}
958963

964+
/**
965+
* Prints a node that replaces the tokens of the original node.
966+
*
967+
* The tokens of a Doctrine tag value do not contain the annotation name.
968+
* The parent tag prints the name from its own tokens.
969+
*/
970+
private function printReplacement(Node $node): string
971+
{
972+
if (!$node instanceof DoctrineTagValueNode) {
973+
return $this->print($node);
974+
}
975+
976+
$arguments = '(' . implode(', ', $node->annotation->arguments) . ')';
977+
if ($node->annotation->arguments === []) {
978+
$originalNode = $node->getAttribute(Attribute::ORIGINAL_NODE);
979+
if (
980+
$originalNode instanceof DoctrineTagValueNode
981+
&& $originalNode->annotation->getAttribute(Attribute::END_INDEX) < $originalNode->annotation->getAttribute(Attribute::START_INDEX)
982+
) {
983+
$arguments = '';
984+
}
985+
}
986+
987+
return trim($arguments . ' ' . $node->description);
988+
}
989+
959990
}

‎tests/PHPStan/Printer/PrinterTest.php‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineArgument;
1818
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineArray;
1919
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineArrayItem;
20+
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineTagValueNode;
2021
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
2122
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamClosureThisTagValueNode;
2223
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamImmediatelyInvokedCallableTagValueNode;
@@ -1987,6 +1988,57 @@ public function enterNode(Node $node)
19871988
},
19881989
];
19891990

1991+
yield [
1992+
'/** @\Foo\Id */',
1993+
'/** @\Foo\Id */',
1994+
$noopVisitor,
1995+
];
1996+
1997+
yield [
1998+
self::nowdoc('
1999+
/**
2000+
* @ORM\Column(type="string")
2001+
* @\Foo\Id
2002+
*/'),
2003+
self::nowdoc('
2004+
/**
2005+
* @ORM\Column(type="string")
2006+
* @\Foo\Id
2007+
*/'),
2008+
$noopVisitor,
2009+
];
2010+
2011+
$changeDoctrineDescriptionVisitor = new class extends AbstractNodeVisitor {
2012+
2013+
public function enterNode(Node $node)
2014+
{
2015+
if ($node instanceof DoctrineTagValueNode) {
2016+
$node->description = 'other description';
2017+
}
2018+
2019+
return $node;
2020+
}
2021+
2022+
};
2023+
2024+
yield [
2025+
'/** @Route("/path", name="x") some description */',
2026+
'/** @Route("/path", name="x") other description */',
2027+
$changeDoctrineDescriptionVisitor,
2028+
];
2029+
2030+
yield [
2031+
'/** @\Foo\Route("/path") some description */',
2032+
'/** @\Foo\Route("/path") other description */',
2033+
$changeDoctrineDescriptionVisitor,
2034+
];
2035+
2036+
yield [
2037+
'/** @\Foo\Id some description */',
2038+
'/** @\Foo\Id other description */',
2039+
$changeDoctrineDescriptionVisitor,
2040+
];
2041+
19902042
yield [
19912043
'/** @Foo() */',
19922044
'/** @Foo(1, 2, 3) */',

0 commit comments

Comments
 (0)