Skip to content

Commit 34a8904

Browse files
committed
Fixed property attributes. Added attribute placement a test case.
1 parent 214c435 commit 34a8904

File tree

5 files changed

+116
-4
lines changed

5 files changed

+116
-4
lines changed

Zend/zend_ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ zend_ast * ZEND_FASTCALL zend_ast_with_attributes(zend_ast *ast, zend_ast *attr)
21422142
ast->lineno = ast->child[0]->lineno;
21432143
break;
21442144
default:
2145-
zend_ast_destroy(attr);
2145+
zend_error_noreturn(E_COMPILE_ERROR, "Invalid use of attributes");
21462146
}
21472147

21482148
return ast;

Zend/zend_ast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ enum _zend_ast_kind {
149149

150150
ZEND_AST_TRY,
151151
ZEND_AST_CATCH,
152+
ZEND_AST_PROP_GROUP,
152153
ZEND_AST_PROP_ELEM,
153154
ZEND_AST_CONST_ELEM,
154155

155156
/* 4 child nodes */
156157
ZEND_AST_FOR = 4 << ZEND_AST_NUM_CHILDREN_SHIFT,
157158
ZEND_AST_FOREACH,
158-
ZEND_AST_PROP_GROUP,
159159
ZEND_AST_PARAM,
160160
};
161161

Zend/zend_compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6526,7 +6526,7 @@ void zend_compile_prop_group(zend_ast *list) /* {{{ */
65266526
zend_ast *type_ast = list->child[0];
65276527
zend_ast *prop_ast = list->child[1];
65286528

6529-
attributes = list->child[3] ? zend_compile_attributes(list->child[3]) : NULL;
6529+
attributes = list->child[2] ? zend_compile_attributes(list->child[2]) : NULL;
65306530

65316531
zend_compile_prop_decl(prop_ast, type_ast, list->attr, attributes);
65326532

Zend/zend_language_parser.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ class_statement_list:
782782

783783
annotated_class_statement:
784784
variable_modifiers optional_type_without_static property_list ';'
785-
{ $$ = zend_ast_create(ZEND_AST_PROP_GROUP, $2, $3, NULL, NULL);
785+
{ $$ = zend_ast_create(ZEND_AST_PROP_GROUP, $2, $3, NULL);
786786
$$->attr = $1; }
787787
| method_modifiers T_CONST class_const_list ';'
788788
{ $$ = $3; $$->attr = $1; }
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
--TEST--
2+
Attributes can be placed on all supported elements.
3+
--FILE--
4+
<?php
5+
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) { }
17+
}
18+
19+
$object = new <<A1(7)>> class () { };
20+
21+
<<A1(8)>>
22+
function f1() { }
23+
24+
$f2 = <<A1(9)>> function () { };
25+
26+
$f3 = <<A1(10)>> fn () => 1;
27+
28+
$ref = new \ReflectionClass(Foo::class);
29+
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+
];
44+
45+
foreach ($sources as $r) {
46+
foreach ($r->getAttributes() as $attr) {
47+
var_dump($attr->getName(), $attr->getArguments());
48+
}
49+
}
50+
51+
?>
52+
--EXPECT--
53+
string(2) "A1"
54+
array(1) {
55+
[0]=>
56+
int(1)
57+
}
58+
string(2) "A1"
59+
array(1) {
60+
[0]=>
61+
int(2)
62+
}
63+
string(2) "A1"
64+
array(1) {
65+
[0]=>
66+
int(2)
67+
}
68+
string(2) "A1"
69+
array(1) {
70+
[0]=>
71+
int(3)
72+
}
73+
string(2) "A1"
74+
array(1) {
75+
[0]=>
76+
int(3)
77+
}
78+
string(2) "A1"
79+
array(1) {
80+
[0]=>
81+
int(4)
82+
}
83+
string(2) "A1"
84+
array(1) {
85+
[0]=>
86+
int(5)
87+
}
88+
string(2) "A1"
89+
array(1) {
90+
[0]=>
91+
int(6)
92+
}
93+
string(2) "A1"
94+
array(1) {
95+
[0]=>
96+
int(7)
97+
}
98+
string(2) "A1"
99+
array(1) {
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)
112+
}

0 commit comments

Comments
 (0)