Skip to content

Commit c7dc3ce

Browse files
committed
Add basic FPPP support for attributes
1 parent 9f6ad68 commit c7dc3ce

File tree

3 files changed

+198
-3
lines changed

3 files changed

+198
-3
lines changed

lib/PhpParser/Internal/PrintableNewAnonClassNode.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
class PrintableNewAnonClassNode extends Expr
1919
{
20+
/** @var Node\AttributeGroup[] PHP attribute groups */
21+
public $attrGroups;
2022
/** @var Node\Arg[] Arguments */
2123
public $args;
2224
/** @var null|Node\Name Name of extended class */
@@ -27,9 +29,11 @@ class PrintableNewAnonClassNode extends Expr
2729
public $stmts;
2830

2931
public function __construct(
30-
array $args, Node\Name $extends = null, array $implements, array $stmts, array $attributes
32+
array $attrGroups, array $args, Node\Name $extends = null, array $implements,
33+
array $stmts, array $attributes
3134
) {
3235
parent::__construct($attributes);
36+
$this->attrGroups = $attrGroups;
3337
$this->args = $args;
3438
$this->extends = $extends;
3539
$this->implements = $implements;
@@ -42,7 +46,7 @@ public static function fromNewNode(Expr\New_ $newNode) {
4246
// We don't assert that $class->name is null here, to allow consumers to assign unique names
4347
// to anonymous classes for their own purposes. We simplify ignore the name here.
4448
return new self(
45-
$newNode->args, $class->extends, $class->implements,
49+
$class->attrGroups, $newNode->args, $class->extends, $class->implements,
4650
$class->stmts, $newNode->getAttributes()
4751
);
4852
}
@@ -52,6 +56,6 @@ public function getType() : string {
5256
}
5357

5458
public function getSubNodeNames() : array {
55-
return ['args', 'extends', 'implements', 'stmts'];
59+
return ['attrGroups', 'args', 'extends', 'implements', 'stmts'];
5660
}
5761
}

lib/PhpParser/PrettyPrinterAbstract.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,7 @@ protected function initializeListInsertionMap() {
13751375
'Stmt_Unset->vars' => ', ',
13761376
'Stmt_Use->uses' => ', ',
13771377
'MatchArm->conds' => ', ',
1378+
'AttributeGroup->attrs' => ', ',
13781379

13791380
// statement lists
13801381
'Expr_Closure->stmts' => "\n",
@@ -1395,6 +1396,17 @@ protected function initializeListInsertionMap() {
13951396
'Stmt_Function->stmts' => "\n",
13961397
'Stmt_If->stmts' => "\n",
13971398
'Stmt_Namespace->stmts' => "\n",
1399+
'Stmt_Class->attrGroups' => "\n",
1400+
'Stmt_Interface->attrGroups' => "\n",
1401+
'Stmt_Trait->attrGroups' => "\n",
1402+
'Stmt_Function->attrGroups' => "\n",
1403+
'Stmt_ClassMethod->attrGroups' => "\n",
1404+
'Stmt_ClassConst->attrGroups' => "\n",
1405+
'Stmt_Property->attrGroups' => "\n",
1406+
'Expr_PrintableNewAnonClass->attrGroups' => ' ',
1407+
'Expr_Closure->attrGroups' => ' ',
1408+
'Expr_ArrowFunction->attrGroups' => ' ',
1409+
'Param->attrGroups' => ' ',
13981410
'Stmt_Switch->cases' => "\n",
13991411
'Stmt_TraitUse->adaptations' => "\n",
14001412
'Stmt_TryCatch->stmts' => "\n",
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
Attributes
2+
-----
3+
<?php
4+
#[A]
5+
class X {
6+
#[A]
7+
public function m(#[A] & $p) {}
8+
9+
#[A]
10+
public
11+
$prop;
12+
13+
#[A]
14+
const
15+
X = 42;
16+
}
17+
18+
#[A]
19+
trait X {}
20+
21+
#[A]
22+
interface X {}
23+
24+
#[A]
25+
function f() {}
26+
27+
new #[A] class {};
28+
#[A] function() {};
29+
#[A] fn()
30+
=> 42;
31+
-----
32+
$attrGroup = new Node\AttributeGroup([
33+
new Node\Attribute(new Node\Name('B'), []),
34+
]);
35+
$stmts[0]->attrGroups[] = $attrGroup;
36+
$stmts[0]->stmts[0]->attrGroups[] = $attrGroup;
37+
$stmts[0]->stmts[0]->params[0]->attrGroups[] = $attrGroup;
38+
$stmts[0]->stmts[1]->attrGroups[] = $attrGroup;
39+
$stmts[0]->stmts[2]->attrGroups[] = $attrGroup;
40+
$stmts[1]->attrGroups[] = $attrGroup;
41+
$stmts[2]->attrGroups[] = $attrGroup;
42+
$stmts[3]->attrGroups[] = $attrGroup;
43+
$stmts[4]->expr->class->attrGroups[] = $attrGroup;
44+
$stmts[5]->expr->attrGroups[] = $attrGroup;
45+
$stmts[6]->expr->attrGroups[] = $attrGroup;
46+
-----
47+
<?php
48+
#[A]
49+
#[B]
50+
class X {
51+
#[A]
52+
#[B]
53+
public function m(#[A] #[B] & $p) {}
54+
55+
#[A]
56+
#[B]
57+
public
58+
$prop;
59+
60+
#[A]
61+
#[B]
62+
const
63+
X = 42;
64+
}
65+
66+
#[A]
67+
#[B]
68+
trait X {}
69+
70+
#[A]
71+
#[B]
72+
interface X {}
73+
74+
#[A]
75+
#[B]
76+
function f() {}
77+
78+
new #[A] #[B] class {};
79+
#[A] #[B] function() {};
80+
#[A] #[B] fn()
81+
=> 42;
82+
-----
83+
<?php
84+
class X {
85+
public function m() {}
86+
87+
public
88+
$prop;
89+
90+
const
91+
X = 42;
92+
}
93+
94+
trait X {}
95+
96+
interface X {}
97+
98+
function f() {}
99+
100+
new class {};
101+
function() {};
102+
fn()
103+
=> 42;
104+
-----
105+
// TODO: Currently we lose formatting for this case.
106+
$attrGroup = new Node\AttributeGroup([
107+
new Node\Attribute(new Node\Name('A'), []),
108+
]);
109+
$stmts[0]->attrGroups[] = $attrGroup;
110+
$stmts[0]->stmts[0]->attrGroups[] = $attrGroup;
111+
$stmts[0]->stmts[1]->attrGroups[] = $attrGroup;
112+
$stmts[0]->stmts[2]->attrGroups[] = $attrGroup;
113+
$stmts[1]->attrGroups[] = $attrGroup;
114+
$stmts[2]->attrGroups[] = $attrGroup;
115+
$stmts[3]->attrGroups[] = $attrGroup;
116+
$stmts[4]->expr->class->attrGroups[] = $attrGroup;
117+
$stmts[5]->expr->attrGroups[] = $attrGroup;
118+
$stmts[6]->expr->attrGroups[] = $attrGroup;
119+
-----
120+
<?php
121+
#[A]
122+
class X
123+
{
124+
#[A]
125+
public function m()
126+
{
127+
}
128+
#[A]
129+
public $prop;
130+
#[A]
131+
const X = 42;
132+
}
133+
134+
#[A]
135+
trait X
136+
{
137+
}
138+
139+
#[A]
140+
interface X
141+
{
142+
}
143+
144+
#[A]
145+
function f()
146+
{
147+
}
148+
149+
new #[A] class
150+
{
151+
};
152+
#[A] function () {
153+
};
154+
#[A] fn() => 42;
155+
-----
156+
<?php
157+
158+
#[ A, B]
159+
class X {};
160+
#[
161+
A,
162+
B,
163+
]
164+
class X {};
165+
-----
166+
$attr = new Node\Attribute(new Node\Name('C'), []);
167+
$stmts[0]->attrGroups[0]->attrs[] = $attr;
168+
$stmts[1]->attrGroups[0]->attrs[] = $attr;
169+
-----
170+
<?php
171+
172+
#[ A, B, C]
173+
class X {};
174+
#[
175+
A,
176+
B,
177+
C,
178+
]
179+
class X {};

0 commit comments

Comments
 (0)