|
1 | 1 | --TEST--
|
2 |
| -Basic attributes usage |
| 2 | +Basic attributes usage on all elements |
3 | 3 | --FILE--
|
4 | 4 | <?php
|
5 |
| -function dump_attributes($attributes) { |
| 5 | +class Attr { |
| 6 | + public $element; |
| 7 | + |
| 8 | + public function __construct(string $element) { |
| 9 | + $this->element = $element; |
| 10 | + } |
| 11 | +} |
| 12 | +class OtherAttr {} |
| 13 | + |
| 14 | +function dump_attributes($reflection) { |
6 | 15 | $arr = [];
|
7 |
| - foreach ($attributes as $attribute) { |
| 16 | + foreach ($reflection->getAttributes() as $attribute) { |
8 | 17 | if (!isset($arr[$attribute->getName()])) {
|
9 | 18 | $arr[$attribute->getName()] = [];
|
10 | 19 | }
|
11 | 20 | $arr[$attribute->getName()][] = $attribute->getArguments();
|
12 | 21 | }
|
13 | 22 | var_dump($arr);
|
14 | 23 | }
|
15 |
| -// No attributes |
16 |
| -function f0() { |
17 |
| -} |
18 |
| -$r = new ReflectionFunction("f0"); |
19 |
| -dump_attributes($r->getAttributes()); |
20 | 24 |
|
21 |
| -// Function attributes |
22 |
| -<<TestFunction>> |
23 |
| -function foo() { |
24 |
| -} |
25 |
| -$r = new ReflectionFunction("foo"); |
26 |
| -dump_attributes($r->getAttributes()); |
| 25 | +function f0() {} |
| 26 | +dump_attributes(new ReflectionFunction("f0")); |
| 27 | + |
| 28 | +<<Attr("function")>> |
| 29 | +function foo() {} |
| 30 | +dump_attributes(new ReflectionFunction("foo")); |
27 | 31 |
|
28 | 32 | // Class attributes
|
29 |
| -<<TestClass>> |
| 33 | +<<Attr("class")>> |
30 | 34 | class Bar {
|
31 |
| - <<TestConst>> |
| 35 | + <<Attr("const")>> |
32 | 36 | const C = 2;
|
33 |
| - <<TestProp>> |
| 37 | + <<Attr("property")>> |
34 | 38 | public $x = 3;
|
35 | 39 |
|
36 | 40 | }
|
37 |
| -$r = new ReflectionClass("Bar"); |
38 |
| -dump_attributes($r->getAttributes()); |
39 |
| -$r1 = $r->getReflectionConstant("C"); |
40 |
| -dump_attributes($r1->getAttributes()); |
41 |
| -$r2 = $r->getProperty("x"); |
42 |
| -dump_attributes($r2->getAttributes()); |
| 41 | +$reflectionClass = new ReflectionClass("Bar"); |
| 42 | +dump_attributes($reflectionClass); |
| 43 | +dump_attributes($reflectionClass->getReflectionConstant("C")); |
| 44 | +dump_attributes($reflectionClass->getProperty("x")); |
| 45 | + |
| 46 | +$foo = <<Attr("closure")>>function () {}; |
| 47 | +dump_attributes(new ReflectionFunction($foo)); |
| 48 | + |
| 49 | +$foo = <<Attr("short closure")>>fn ($x) => $x; |
| 50 | +dump_attributes(new ReflectionFunction($foo)); |
| 51 | + |
| 52 | +$anonClass = new <<Attr("anon class")>>class {}; |
| 53 | +dump_attributes(new ReflectionClass($anonClass)); |
43 | 54 |
|
44 | 55 | // Multiple attributes with multiple values
|
45 |
| -<<a1>> |
46 |
| -<<a2(1)>> |
47 |
| -<<a3(1,2)>> |
| 56 | +<<Attr()>> |
| 57 | +<<OtherAttr()>> |
48 | 58 | function f1() {}
|
49 |
| -$r = new ReflectionFunction("f1"); |
50 |
| -dump_attributes($r->getAttributes()); |
| 59 | +dump_attributes(new ReflectionFunction("f1")); |
51 | 60 |
|
52 |
| -// Attributes with AST |
53 |
| -<<a1>> |
54 |
| -<<a2(1+1)>> |
55 |
| -<<a3(1+3,2+2)>> |
56 |
| -<<a4(["a"=>1,"b"=>2])>> |
| 61 | +// multiple instances of same attribute |
| 62 | +<<Attr("foo")>> |
| 63 | +<<Attr("bar")>> |
57 | 64 | function f2() {}
|
58 |
| -$r = new ReflectionFunction("f2"); |
59 |
| -dump_attributes($r->getAttributes()); |
60 |
| - |
61 |
| -// Attributes with namespaces |
62 |
| -<<Foo\Bar>> |
63 |
| -function f4() { |
64 |
| -} |
65 |
| -$r = new ReflectionFunction("f4"); |
66 |
| -dump_attributes($r->getAttributes()); |
| 65 | +dump_attributes(new ReflectionFunction("f2")); |
67 | 66 | ?>
|
68 | 67 | --EXPECT--
|
69 | 68 | array(0) {
|
70 | 69 | }
|
71 | 70 | array(1) {
|
72 |
| - ["TestFunction"]=> |
| 71 | + ["Attr"]=> |
73 | 72 | array(1) {
|
74 | 73 | [0]=>
|
75 |
| - array(0) { |
| 74 | + array(1) { |
| 75 | + [0]=> |
| 76 | + string(8) "function" |
76 | 77 | }
|
77 | 78 | }
|
78 | 79 | }
|
79 | 80 | array(1) {
|
80 |
| - ["TestClass"]=> |
| 81 | + ["Attr"]=> |
81 | 82 | array(1) {
|
82 | 83 | [0]=>
|
83 |
| - array(0) { |
| 84 | + array(1) { |
| 85 | + [0]=> |
| 86 | + string(5) "class" |
84 | 87 | }
|
85 | 88 | }
|
86 | 89 | }
|
87 | 90 | array(1) {
|
88 |
| - ["TestConst"]=> |
| 91 | + ["Attr"]=> |
89 | 92 | array(1) {
|
90 | 93 | [0]=>
|
91 |
| - array(0) { |
| 94 | + array(1) { |
| 95 | + [0]=> |
| 96 | + string(5) "const" |
92 | 97 | }
|
93 | 98 | }
|
94 | 99 | }
|
95 | 100 | array(1) {
|
96 |
| - ["TestProp"]=> |
| 101 | + ["Attr"]=> |
97 | 102 | array(1) {
|
98 | 103 | [0]=>
|
99 |
| - array(0) { |
| 104 | + array(1) { |
| 105 | + [0]=> |
| 106 | + string(8) "property" |
100 | 107 | }
|
101 | 108 | }
|
102 | 109 | }
|
103 |
| -array(3) { |
104 |
| - ["a1"]=> |
| 110 | +array(1) { |
| 111 | + ["Attr"]=> |
105 | 112 | array(1) {
|
106 | 113 | [0]=>
|
107 |
| - array(0) { |
| 114 | + array(1) { |
| 115 | + [0]=> |
| 116 | + string(7) "closure" |
108 | 117 | }
|
109 | 118 | }
|
110 |
| - ["a2"]=> |
| 119 | +} |
| 120 | +array(1) { |
| 121 | + ["Attr"]=> |
111 | 122 | array(1) {
|
112 | 123 | [0]=>
|
113 | 124 | array(1) {
|
114 | 125 | [0]=>
|
115 |
| - int(1) |
| 126 | + string(13) "short closure" |
116 | 127 | }
|
117 | 128 | }
|
118 |
| - ["a3"]=> |
| 129 | +} |
| 130 | +array(1) { |
| 131 | + ["Attr"]=> |
119 | 132 | array(1) {
|
120 | 133 | [0]=>
|
121 |
| - array(2) { |
| 134 | + array(1) { |
122 | 135 | [0]=>
|
123 |
| - int(1) |
124 |
| - [1]=> |
125 |
| - int(2) |
| 136 | + string(10) "anon class" |
126 | 137 | }
|
127 | 138 | }
|
128 | 139 | }
|
129 |
| -array(4) { |
130 |
| - ["a1"]=> |
| 140 | +array(2) { |
| 141 | + ["Attr"]=> |
131 | 142 | array(1) {
|
132 | 143 | [0]=>
|
133 | 144 | array(0) {
|
134 | 145 | }
|
135 | 146 | }
|
136 |
| - ["a2"]=> |
| 147 | + ["OtherAttr"]=> |
137 | 148 | array(1) {
|
138 | 149 | [0]=>
|
139 |
| - array(1) { |
140 |
| - [0]=> |
141 |
| - int(2) |
| 150 | + array(0) { |
142 | 151 | }
|
143 | 152 | }
|
144 |
| - ["a3"]=> |
145 |
| - array(1) { |
| 153 | +} |
| 154 | +array(1) { |
| 155 | + ["Attr"]=> |
| 156 | + array(2) { |
146 | 157 | [0]=>
|
147 |
| - array(2) { |
| 158 | + array(1) { |
148 | 159 | [0]=>
|
149 |
| - int(4) |
150 |
| - [1]=> |
151 |
| - int(4) |
| 160 | + string(3) "foo" |
152 | 161 | }
|
153 |
| - } |
154 |
| - ["a4"]=> |
155 |
| - array(1) { |
156 |
| - [0]=> |
| 162 | + [1]=> |
157 | 163 | array(1) {
|
158 | 164 | [0]=>
|
159 |
| - array(2) { |
160 |
| - ["a"]=> |
161 |
| - int(1) |
162 |
| - ["b"]=> |
163 |
| - int(2) |
164 |
| - } |
165 |
| - } |
166 |
| - } |
167 |
| -} |
168 |
| -array(1) { |
169 |
| - ["Foo\Bar"]=> |
170 |
| - array(1) { |
171 |
| - [0]=> |
172 |
| - array(0) { |
| 165 | + string(3) "bar" |
173 | 166 | }
|
174 | 167 | }
|
175 | 168 | }
|
0 commit comments