Skip to content

Commit 54b9ace

Browse files
More tests
1 parent 1ac9b21 commit 54b9ace

File tree

5 files changed

+103
-8
lines changed

5 files changed

+103
-8
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
#[ClassAlias] - attributes parameter validated at compile time (non-object in array)
3+
--FILE--
4+
<?php
5+
6+
#[ClassAlias('Other', [true, new MissingAttribute()])]
7+
class Demo {}
8+
9+
$ref = new ReflectionClass( 'Demo' );
10+
$attrib = $ref->getAttributes()[0];
11+
var_dump( $attrib );
12+
13+
$attrib->newInstance();
14+
15+
?>
16+
--EXPECTF--
17+
Fatal error: Attribute must be declared with `new` in %s on line %d
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
#[ClassAlias] - attributes parameter validated at compile time (array can be evaluated)
3+
--FILE--
4+
<?php
5+
6+
#[ClassAlias('Other', [true])]
7+
class Demo {}
8+
9+
$ref = new ReflectionClass( 'Demo' );
10+
$attrib = $ref->getAttributes()[0];
11+
var_dump( $attrib );
12+
13+
$attrib->newInstance();
14+
15+
?>
16+
--EXPECTF--
17+
Fatal error: ClassAlias::__construct(): Argument #2 ($attributes) must be an array of objects in %s on line %d
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
#[ClassAlias] - attributes parameter validated at runtime (missing class)
3+
--FILE--
4+
<?php
5+
6+
#[ClassAlias('Other', [new MissingAttribute()])]
7+
class Demo {}
8+
9+
$ref = new ReflectionClass( 'Demo' );
10+
$attrib = $ref->getAttributes()[0];
11+
var_dump( $attrib );
12+
13+
$attrib->newInstance();
14+
15+
?>
16+
--EXPECTF--
17+
object(ReflectionAttribute)#%d (1) {
18+
["name"]=>
19+
string(10) "ClassAlias"
20+
}
21+
22+
Fatal error: Uncaught Error: Class "MissingAttribute" not found in %s:%d
23+
Stack trace:
24+
#0 %s(%d): ReflectionAttribute->newInstance()
25+
#1 {main}
26+
thrown in %s on line %d
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
#[ClassAlias] - attributes parameter value types not validated when constructing ClassAlias
3+
--FILE--
4+
<?php
5+
6+
class NonAttribute {}
7+
8+
#[ClassAlias('Other', [new NonAttribute()])]
9+
class Demo {}
10+
11+
$ref = new ReflectionClass( 'Demo' );
12+
$attrib = $ref->getAttributes()[0];
13+
var_dump( $attrib );
14+
15+
var_dump( $attrib->newInstance() );
16+
17+
?>
18+
--EXPECTF--
19+
object(ReflectionAttribute)#%d (1) {
20+
["name"]=>
21+
string(10) "ClassAlias"
22+
}
23+
object(ClassAlias)#%d (1) {
24+
["alias"]=>
25+
string(5) "Other"
26+
}

Zend/zend_attributes.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,23 @@ static void validate_class_alias(
389389
}
390390
ZEND_ASSERT(nested_attribs != NULL);
391391
if (UNEXPECTED(!Z_OPT_CONSTANT_P(nested_attribs))) {
392-
zend_wrong_parameter_error(
393-
ZPP_ERROR_WRONG_ARG,
394-
2,
395-
"attributes",
396-
Z_EXPECTED_ARRAY,
397-
nested_attribs
398-
);
399-
// Something with an invalid parameter
392+
// If it is an array, then it must be an array that can be evaluated
393+
// already
394+
if (Z_TYPE_P(nested_attribs) == IS_ARRAY) {
395+
zend_argument_type_error(
396+
2,
397+
"must be an array of objects"
398+
);
399+
} else {
400+
zend_wrong_parameter_error(
401+
ZPP_ERROR_WRONG_ARG,
402+
2,
403+
"attributes",
404+
Z_EXPECTED_ARRAY,
405+
nested_attribs
406+
);
407+
// Something with an invalid parameter
408+
}
400409
} else {
401410
zend_ast *attributes_ast = Z_ASTVAL_P(nested_attribs);
402411
compile_alias_attributes(&( alias_obj->attributes), attributes_ast);

0 commit comments

Comments
 (0)