Skip to content

Commit 61aa04a

Browse files
Save reflection work
Need to actually add zend_class_alias->attributes but ran out of time for now
1 parent 481f1fc commit 61aa04a

17 files changed

+371
-10
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Class alias listed in valid targets when used wrong (internal attribute)
3+
--FILE--
4+
<?php
5+
6+
#[Deprecated]
7+
class Example {}
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Attribute "Deprecated" cannot target class (allowed targets: function, method, class constant, constant, class alias) in %s on line %d
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Class alias listed in valid targets when used wrong (userland attribute)
3+
--FILE--
4+
<?php
5+
6+
#[Attribute(Attribute::TARGET_CLASS_ALIAS)]
7+
class MyAliasAttribute {}
8+
9+
#[MyAliasAttribute]
10+
class Example {}
11+
12+
$ref = new ReflectionClass(Example::class);
13+
$attribs = $ref->getAttributes();
14+
var_dump($attribs);
15+
$attribs[0]->newInstance();
16+
17+
?>
18+
--EXPECTF--
19+
array(1) {
20+
[0]=>
21+
object(ReflectionAttribute)#%d (1) {
22+
["name"]=>
23+
string(16) "MyAliasAttribute"
24+
}
25+
}
26+
27+
Fatal error: Uncaught Error: Attribute "MyAliasAttribute" cannot target class (allowed targets: class alias) in %s:%d
28+
Stack trace:
29+
#0 %s(%d): ReflectionAttribute->newInstance()
30+
#1 {main}
31+
thrown in %s on line %d
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Error when attribute does not target class alias (internal attribute)
3+
--FILE--
4+
<?php
5+
6+
#[ClassAlias('Other', [new Override()])]
7+
class Demo {}
8+
9+
?>
10+
--EXPECT--
11+
NO
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Error when attribute does not target class alias (useland attribute)
3+
--FILE--
4+
<?php
5+
6+
#[Attribute(Attribute::TARGET_FUNCTION)]
7+
class MyFunctionAttribute {}
8+
9+
#[ClassAlias('Other', [new MyFunctionAttribute()])]
10+
class Demo {}
11+
12+
$ref = new ReflectionClassAlias('Other');
13+
$attribs = $ref->getAttributes();
14+
var_dump($attribs);
15+
$attribs[0]->newInstance();
16+
17+
?>
18+
--EXPECTF--
19+
array(1) {
20+
[0]=>
21+
object(ReflectionAttribute)#%d (1) {
22+
["name"]=>
23+
string(10) "ClassAlias"
24+
}
25+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Validation of attribute repetition (not allowed; internal attribute)
3+
--FILE--
4+
<?php
5+
6+
#[ClassAlias('Other', [new Deprecated(), new Deprecated()])]
7+
class Demo {}
8+
9+
?>
10+
--EXPECT--
11+
12+
e
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Validation of attribute repetition (not allowed; userland attribute)
3+
--FILE--
4+
<?php
5+
6+
#[Attribute]
7+
class MyAttribute {}
8+
9+
#[ClassAlias('Other', [new MyAttribute(), new MyAttribute()])]
10+
class Demo {}
11+
12+
$attributes = new ReflectionClassAlias('Other')->getAttributes();
13+
var_dump($attributes);
14+
$attributes[0]->newInstance();
15+
16+
?>
17+
--EXPECTF--
18+
array(1) {
19+
[0]=>
20+
object(ReflectionAttribute)#%d (1) {
21+
["name"]=>
22+
string(10) "ClassAlias"
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Validation of attribute repetition (is allowed; internal attribute)
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
#[ClassAlias('Other', [new ZendTestRepeatableAttribute(), new ZendTestRepeatableAttribute()])]
9+
class Demo {}
10+
11+
echo "Done\n";
12+
13+
?>
14+
--EXPECT--
15+
Done
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Validation of attribute repetition (is allowed; userland attribute)
3+
--FILE--
4+
<?php
5+
6+
#[Attribute(Attribute::TARGET_ALL|Attribute::IS_REPEATABLE)]
7+
class MyAttribute {}
8+
9+
#[ClassAlias('Other', [new MyAttribute(), new MyAttribute()])]
10+
class Demo {}
11+
12+
$attributes = new ReflectionClassAlias('Other')->getAttributes();
13+
var_dump($attributes);
14+
$attributes[0]->newInstance();
15+
16+
?>
17+
--EXPECTF--
18+
array(1) {
19+
[0]=>
20+
object(ReflectionAttribute)#%d (1) {
21+
["name"]=>
22+
string(10) "ClassAlias"
23+
}
24+
}
25+
php: /usr/src/php/Zend/zend_variables.c:143: zval_copy_ctor_func: Assertion `!(zval_gc_flags(((*(zvalue)).value.str)->gc.u.type_info) & (1<<6))' failed.
26+
Aborted (core dumped)
27+
28+
Termsig=6
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Attributes with TARGET_ALL (from the default) can target class aliases
3+
--FILE--
4+
<?php
5+
6+
#[Attribute]
7+
class MyAttribute {}
8+
9+
#[ClassAlias('Other', [new MyAttribute()])]
10+
class Demo {}
11+
12+
$ref = new ReflectionClassAlias('Other');
13+
$attribs = $ref->getAttributes();
14+
var_dump($attribs);
15+
$attribs[0]->newInstance();
16+
17+
?>
18+
--EXPECTF--
19+
array(1) {
20+
[0]=>
21+
object(ReflectionAttribute)#%d (1) {
22+
["name"]=>
23+
string(10) "ClassAlias"
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Attributes with TARGET_ALL (from an explicit parameter) can target class aliases
3+
--FILE--
4+
<?php
5+
6+
#[Attribute(Attribute::TARGET_ALL)]
7+
class MyAttribute {}
8+
9+
#[ClassAlias('Other', [new MyAttribute()])]
10+
class Demo {}
11+
12+
$ref = new ReflectionClassAlias('Other');
13+
$attribs = $ref->getAttributes();
14+
var_dump($attribs);
15+
$attribs[0]->newInstance();
16+
17+
?>
18+
--EXPECTF--
19+
array(1) {
20+
[0]=>
21+
object(ReflectionAttribute)#%d (1) {
22+
["name"]=>
23+
string(10) "ClassAlias"
24+
}
25+
}

0 commit comments

Comments
 (0)