Skip to content

Commit 08b2528

Browse files
committed
Added additional tests.
1 parent 08eca9c commit 08b2528

File tree

4 files changed

+143
-1
lines changed

4 files changed

+143
-1
lines changed

Zend/tests/attributes/003_ast_nodes.phpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,37 @@ var_dump(count($args));
5050
var_dump($args[0] === 'foo');
5151
var_dump($args[1] === C1::BAR);
5252

53+
echo "\n";
54+
5355
<<ExampleWithShift(4 >> 1)>>
5456
class C4 {}
5557
$ref = new \ReflectionClass(C4::class);
5658
var_dump($ref->getAttributes()[0]->getArguments());
5759

60+
echo "\n";
61+
62+
<<PhpAttribute>>
63+
class C5
64+
{
65+
public function __construct() { }
66+
}
67+
68+
$ref = new \ReflectionFunction(<<C5(MissingClass::SOME_CONST)>> function () { });
69+
$attr = $ref->getAttributes();
70+
var_dump(count($attr));
71+
72+
try {
73+
$attr[0]->getArguments();
74+
} catch (\Error $e) {
75+
var_dump($e->getMessage());
76+
}
77+
78+
try {
79+
$attr[0]->newInstance();
80+
} catch (\Error $e) {
81+
var_dump($e->getMessage());
82+
}
83+
5884
?>
5985
--EXPECT--
6086
int(1)
@@ -71,7 +97,13 @@ int(1)
7197
int(2)
7298
bool(true)
7399
bool(true)
100+
74101
array(1) {
75102
[0]=>
76103
int(2)
77104
}
105+
106+
int(1)
107+
string(30) "Class 'MissingClass' not found"
108+
string(30) "Class 'MissingClass' not found"
109+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Attribute arguments support only const expressions.
3+
--FILE--
4+
<?php
5+
6+
<<A1(foo())>>
7+
class C1 { }
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Constant expression contains invalid operations in %s
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
--TEST--
2+
Attributes comply with inheritance rules.
3+
--FILE--
4+
<?php
5+
6+
<<A2>>
7+
class C1
8+
{
9+
<<A1>>
10+
public function foo() { }
11+
}
12+
13+
class C2 extends C1
14+
{
15+
public function foo() { }
16+
}
17+
18+
class C3 extends C1
19+
{
20+
<<A1>>
21+
public function bar() { }
22+
}
23+
24+
$ref = new \ReflectionClass(C1::class);
25+
print_r(array_map(fn ($a) => $a->getName(), $ref->getAttributes()));
26+
print_r(array_map(fn ($a) => $a->getName(), $ref->getMethod('foo')->getAttributes()));
27+
28+
$ref = new \ReflectionClass(C2::class);
29+
print_r(array_map(fn ($a) => $a->getName(), $ref->getAttributes()));
30+
print_r(array_map(fn ($a) => $a->getName(), $ref->getMethod('foo')->getAttributes()));
31+
32+
$ref = new \ReflectionClass(C3::class);
33+
print_r(array_map(fn ($a) => $a->getName(), $ref->getAttributes()));
34+
print_r(array_map(fn ($a) => $a->getName(), $ref->getMethod('foo')->getAttributes()));
35+
36+
echo "\n";
37+
38+
trait T1
39+
{
40+
<<A2>>
41+
public $a;
42+
}
43+
44+
class C4
45+
{
46+
use T1;
47+
}
48+
49+
class C5
50+
{
51+
use T1;
52+
53+
public $a;
54+
}
55+
56+
$ref = new \ReflectionClass(T1::class);
57+
print_r(array_map(fn ($a) => $a->getName(), $ref->getProperty('a')->getAttributes()));
58+
59+
$ref = new \ReflectionClass(C4::class);
60+
print_r(array_map(fn ($a) => $a->getName(), $ref->getProperty('a')->getAttributes()));
61+
62+
$ref = new \ReflectionClass(C5::class);
63+
print_r(array_map(fn ($a) => $a->getName(), $ref->getProperty('a')->getAttributes()));
64+
65+
?>
66+
--EXPECT--
67+
Array
68+
(
69+
[0] => A2
70+
)
71+
Array
72+
(
73+
[0] => A1
74+
)
75+
Array
76+
(
77+
)
78+
Array
79+
(
80+
)
81+
Array
82+
(
83+
)
84+
Array
85+
(
86+
[0] => A1
87+
)
88+
89+
Array
90+
(
91+
[0] => A2
92+
)
93+
Array
94+
(
95+
[0] => A2
96+
)
97+
Array
98+
(
99+
)

ext/reflection/php_reflection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6451,7 +6451,7 @@ ZEND_METHOD(ReflectionAttribute, getName)
64516451
}
64526452
/* }}} */
64536453

6454-
static int import_attribute_value(zval *ret, zval *val, zend_class_entry *scope) /* {{{ */
6454+
static zend_always_inline int import_attribute_value(zval *ret, zval *val, zend_class_entry *scope) /* {{{ */
64556455
{
64566456
if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
64576457
*ret = *val;

0 commit comments

Comments
 (0)