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