Skip to content

Commit 36ed996

Browse files
committed
Allow attributes to be applied to property/constant groups
Remove arbitrary restriction that attributes cannot be applied to property/constant groups. The attribute applies to all elements of the group, just like modifiers and types do. See also https://externals.io/message/111914. Closes GH-6186.
1 parent 2772751 commit 36ed996

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed
Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
--TEST--
2-
Attributes cannot be applied to groups of class constants.
2+
Attributes can be applied to groups of class constants
33
--FILE--
44
<?php
55

6-
class C1
6+
class C
77
{
8-
#[A1]
8+
#[A(1, X)]
99
public const A = 1, B = 2;
1010
}
1111

12+
const X = 2;
13+
14+
$rp1 = new ReflectionClassConstant('C', 'A');
15+
$ra1 = $rp1->getAttributes()[0];
16+
var_dump($ra1->getName(), $ra1->getArguments());
17+
$rp2 = new ReflectionClassConstant('C', 'B');
18+
$ra2 = $rp2->getAttributes()[0];
19+
var_dump($ra2->getName(), $ra2->getArguments());
20+
1221
?>
13-
--EXPECTF--
14-
Fatal error: Cannot apply attributes to a group of constants in %s
22+
--EXPECT--
23+
string(1) "A"
24+
array(2) {
25+
[0]=>
26+
int(1)
27+
[1]=>
28+
int(2)
29+
}
30+
string(1) "A"
31+
array(2) {
32+
[0]=>
33+
int(1)
34+
[1]=>
35+
int(2)
36+
}
Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
--TEST--
2-
Attributes cannot be applied to groups of properties.
2+
Attributes can be applied to groups of properties
33
--FILE--
44
<?php
55

6-
class C1
6+
class C
77
{
8-
#[A1]
8+
#[A(1, X)]
99
public $x, $y;
1010
}
1111

12+
const X = 2;
13+
14+
$rp1 = new ReflectionProperty('C', 'x');
15+
$ra1 = $rp1->getAttributes()[0];
16+
var_dump($ra1->getName(), $ra1->getArguments());
17+
$rp2 = new ReflectionProperty('C', 'y');
18+
$ra2 = $rp2->getAttributes()[0];
19+
var_dump($ra2->getName(), $ra2->getArguments());
20+
1221
?>
13-
--EXPECTF--
14-
Fatal error: Cannot apply attributes to a group of properties in %s
22+
--EXPECT--
23+
string(1) "A"
24+
array(2) {
25+
[0]=>
26+
int(1)
27+
[1]=>
28+
int(2)
29+
}
30+
string(1) "A"
31+
array(2) {
32+
[0]=>
33+
int(1)
34+
[1]=>
35+
int(2)
36+
}

Zend/zend_compile.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7069,11 +7069,6 @@ void zend_compile_prop_group(zend_ast *ast) /* {{{ */
70697069
zend_ast *prop_ast = ast->child[1];
70707070
zend_ast *attr_ast = ast->child[2];
70717071

7072-
if (attr_ast && zend_ast_get_list(prop_ast)->children > 1) {
7073-
zend_error_noreturn(E_COMPILE_ERROR, "Cannot apply attributes to a group of properties");
7074-
return;
7075-
}
7076-
70777072
zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
70787073
}
70797074
/* }}} */
@@ -7130,12 +7125,6 @@ void zend_compile_class_const_group(zend_ast *ast) /* {{{ */
71307125
zend_ast *const_ast = ast->child[0];
71317126
zend_ast *attr_ast = ast->child[1];
71327127

7133-
if (attr_ast && zend_ast_get_list(const_ast)->children > 1) {
7134-
zend_error_noreturn(E_COMPILE_ERROR, "Cannot apply attributes to a group of constants");
7135-
7136-
return;
7137-
}
7138-
71397128
zend_compile_class_const_decl(const_ast, ast->attr, attr_ast);
71407129
}
71417130
/* }}} */

0 commit comments

Comments
 (0)