Skip to content

Commit af8f94b

Browse files
committed
Fix Printer
1 parent a61e41d commit af8f94b

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

Diff for: src/Printer/Printer.php

+4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ final class Printer
100100
GenericTypeNode::class . '->genericTypes' => ', ',
101101
ConstExprArrayNode::class . '->items' => ', ',
102102
MethodTagValueNode::class . '->parameters' => ', ',
103+
DoctrineArray::class . '->items' => ', ',
104+
DoctrineAnnotation::class . '->arguments' => ', ',
103105
];
104106

105107
/**
@@ -111,6 +113,8 @@ final class Printer
111113
CallableTypeNode::class . '->parameters' => ['(', '', ''],
112114
ArrayShapeNode::class . '->items' => ['{', '', ''],
113115
ObjectShapeNode::class . '->items' => ['{', '', ''],
116+
DoctrineArray::class . '->items' => ['{', '', ''],
117+
DoctrineAnnotation::class . '->arguments' => ['(', '', ''],
114118
];
115119

116120
/** @var array<string, list<class-string<TypeNode>>> */

Diff for: tests/PHPStan/Parser/PhpDocParserTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -5980,6 +5980,22 @@ public function dataDeepNodesLinesAndIndexes(): iterable
59805980
[6, 6, 18, 18], // ConstExprIntegerNode
59815981
],
59825982
];
5983+
5984+
yield [
5985+
'/**' . PHP_EOL .
5986+
' * @\Foo\Bar({' . PHP_EOL .
5987+
' * }' . PHP_EOL .
5988+
' * )' . PHP_EOL .
5989+
' */',
5990+
[
5991+
[1, 5, 0, 10], // PhpDocNode
5992+
[2, 4, 2, 8], // PhpDocTagNode
5993+
[2, 4, 3, 8], // DoctrineTagValueNode
5994+
[2, 4, 3, 8], // DoctrineAnnotation
5995+
[2, 4, 4, 6], // DoctrineArgument
5996+
[2, 4, 4, 6], // DoctrineArray
5997+
],
5998+
];
59835999
}
59846000

59856001

Diff for: tests/PHPStan/Printer/PrinterTest.php

+103
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use PHPStan\PhpDocParser\Ast\Node;
1313
use PHPStan\PhpDocParser\Ast\NodeTraverser;
1414
use PHPStan\PhpDocParser\Ast\NodeVisitor;
15+
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineAnnotation;
16+
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineArgument;
17+
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineArray;
1518
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineArrayItem;
1619
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
1720
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
@@ -46,6 +49,7 @@
4649
use function array_unshift;
4750
use function array_values;
4851
use function count;
52+
use const PHP_EOL;
4953

5054
class PrinterTest extends TestCase
5155
{
@@ -1532,6 +1536,105 @@ public function enterNode(Node $node)
15321536

15331537
},
15341538
];
1539+
1540+
yield [
1541+
'/** @Foo() */',
1542+
'/** @Foo(1, 2, 3) */',
1543+
new class extends AbstractNodeVisitor {
1544+
1545+
public function enterNode(Node $node)
1546+
{
1547+
if ($node instanceof DoctrineAnnotation) {
1548+
$node->arguments = [
1549+
new DoctrineArgument(null, new ConstExprIntegerNode('1')),
1550+
new DoctrineArgument(null, new ConstExprIntegerNode('2')),
1551+
new DoctrineArgument(null, new ConstExprIntegerNode('3')),
1552+
];
1553+
}
1554+
1555+
return $node;
1556+
}
1557+
1558+
},
1559+
];
1560+
1561+
yield [
1562+
'/** @Foo(
1563+
* 1,
1564+
* 2,
1565+
* ) */',
1566+
'/** @Foo(
1567+
* 1,
1568+
* 2,
1569+
* 3,
1570+
* ) */',
1571+
new class extends AbstractNodeVisitor {
1572+
1573+
public function enterNode(Node $node)
1574+
{
1575+
if ($node instanceof DoctrineAnnotation) {
1576+
$node->arguments[] = new DoctrineArgument(null, new ConstExprIntegerNode('3'));
1577+
}
1578+
1579+
return $node;
1580+
}
1581+
1582+
},
1583+
];
1584+
1585+
yield [
1586+
'/**' . PHP_EOL .
1587+
' * @X({' . PHP_EOL .
1588+
' * 1,' . PHP_EOL .
1589+
' * 2' . PHP_EOL .
1590+
' * , ' . PHP_EOL .
1591+
' * 3,' . PHP_EOL .
1592+
' * }' . PHP_EOL .
1593+
' * )' . PHP_EOL .
1594+
' */',
1595+
'/**' . PHP_EOL .
1596+
' * @X({' . PHP_EOL .
1597+
' * 1,' . PHP_EOL .
1598+
' * 2' . PHP_EOL .
1599+
' * , ' . PHP_EOL .
1600+
' * 3,' . PHP_EOL .
1601+
' * 4,' . PHP_EOL .
1602+
' * }' . PHP_EOL .
1603+
' * )' . PHP_EOL .
1604+
' */',
1605+
new class extends AbstractNodeVisitor {
1606+
1607+
public function enterNode(Node $node)
1608+
{
1609+
if ($node instanceof DoctrineArray) {
1610+
$node->items[] = new DoctrineArrayItem(null, new ConstExprIntegerNode('4'));
1611+
}
1612+
1613+
return $node;
1614+
}
1615+
1616+
},
1617+
];
1618+
1619+
yield [
1620+
'/** @Foo() */',
1621+
'/** @Bar() */',
1622+
new class extends AbstractNodeVisitor {
1623+
1624+
public function enterNode(Node $node)
1625+
{
1626+
if ($node instanceof PhpDocTagNode) {
1627+
$node->name = '@Bar';
1628+
}
1629+
if ($node instanceof DoctrineAnnotation) {
1630+
$node->name = '@Bar';
1631+
}
1632+
1633+
return $node;
1634+
}
1635+
1636+
},
1637+
];
15351638
}
15361639

15371640
/**

0 commit comments

Comments
 (0)