summaryrefslogtreecommitdiff
path: root/Zend
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2020-09-22 14:44:06 +0200
committerNikita Popov <nikita.ppv@gmail.com>2020-09-27 11:35:48 +0200
commit36ed9966ce791676dd435fd3ee5fc67a17bc09b2 (patch)
tree61acdbe5978158b6b2255e42a1fb24685dd9017c /Zend
parent2772751b58ee579a8f1288a0949e5e1fcb554877 (diff)
downloadphp-git-36ed9966ce791676dd435fd3ee5fc67a17bc09b2.tar.gz
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.
Diffstat (limited to 'Zend')
-rw-r--r--Zend/tests/attributes/014_class_const_group.phpt32
-rw-r--r--Zend/tests/attributes/015_property_group.phpt32
-rw-r--r--Zend/zend_compile.c11
3 files changed, 54 insertions, 21 deletions
diff --git a/Zend/tests/attributes/014_class_const_group.phpt b/Zend/tests/attributes/014_class_const_group.phpt
index 9f01d013a7..726483a2dc 100644
--- a/Zend/tests/attributes/014_class_const_group.phpt
+++ b/Zend/tests/attributes/014_class_const_group.phpt
@@ -1,14 +1,36 @@
--TEST--
-Attributes cannot be applied to groups of class constants.
+Attributes can be applied to groups of class constants
--FILE--
<?php
-class C1
+class C
{
- #[A1]
+ #[A(1, X)]
public const A = 1, B = 2;
}
+const X = 2;
+
+$rp1 = new ReflectionClassConstant('C', 'A');
+$ra1 = $rp1->getAttributes()[0];
+var_dump($ra1->getName(), $ra1->getArguments());
+$rp2 = new ReflectionClassConstant('C', 'B');
+$ra2 = $rp2->getAttributes()[0];
+var_dump($ra2->getName(), $ra2->getArguments());
+
?>
---EXPECTF--
-Fatal error: Cannot apply attributes to a group of constants in %s
+--EXPECT--
+string(1) "A"
+array(2) {
+ [0]=>
+ int(1)
+ [1]=>
+ int(2)
+}
+string(1) "A"
+array(2) {
+ [0]=>
+ int(1)
+ [1]=>
+ int(2)
+}
diff --git a/Zend/tests/attributes/015_property_group.phpt b/Zend/tests/attributes/015_property_group.phpt
index b84ded8c38..a448cb80d6 100644
--- a/Zend/tests/attributes/015_property_group.phpt
+++ b/Zend/tests/attributes/015_property_group.phpt
@@ -1,14 +1,36 @@
--TEST--
-Attributes cannot be applied to groups of properties.
+Attributes can be applied to groups of properties
--FILE--
<?php
-class C1
+class C
{
- #[A1]
+ #[A(1, X)]
public $x, $y;
}
+const X = 2;
+
+$rp1 = new ReflectionProperty('C', 'x');
+$ra1 = $rp1->getAttributes()[0];
+var_dump($ra1->getName(), $ra1->getArguments());
+$rp2 = new ReflectionProperty('C', 'y');
+$ra2 = $rp2->getAttributes()[0];
+var_dump($ra2->getName(), $ra2->getArguments());
+
?>
---EXPECTF--
-Fatal error: Cannot apply attributes to a group of properties in %s
+--EXPECT--
+string(1) "A"
+array(2) {
+ [0]=>
+ int(1)
+ [1]=>
+ int(2)
+}
+string(1) "A"
+array(2) {
+ [0]=>
+ int(1)
+ [1]=>
+ int(2)
+}
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index f0019f591c..7debd55acc 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -7069,11 +7069,6 @@ void zend_compile_prop_group(zend_ast *ast) /* {{{ */
zend_ast *prop_ast = ast->child[1];
zend_ast *attr_ast = ast->child[2];
- if (attr_ast && zend_ast_get_list(prop_ast)->children > 1) {
- zend_error_noreturn(E_COMPILE_ERROR, "Cannot apply attributes to a group of properties");
- return;
- }
-
zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
}
/* }}} */
@@ -7130,12 +7125,6 @@ void zend_compile_class_const_group(zend_ast *ast) /* {{{ */
zend_ast *const_ast = ast->child[0];
zend_ast *attr_ast = ast->child[1];
- if (attr_ast && zend_ast_get_list(const_ast)->children > 1) {
- zend_error_noreturn(E_COMPILE_ERROR, "Cannot apply attributes to a group of constants");
-
- return;
- }
-
zend_compile_class_const_decl(const_ast, ast->attr, attr_ast);
}
/* }}} */