Skip to content

Commit 0ef14f0

Browse files
More tests
1 parent 16e4da3 commit 0ef14f0

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
@@ -355,14 +355,23 @@ static void validate_class_alias(
355355
}
356356
ZEND_ASSERT(nested_attribs != NULL);
357357
if (UNEXPECTED(!Z_OPT_CONSTANT_P(nested_attribs))) {
358-
zend_wrong_parameter_error(
359-
ZPP_ERROR_WRONG_ARG,
360-
2,
361-
"attributes",
362-
Z_EXPECTED_ARRAY,
363-
nested_attribs
364-
);
365-
// Something with an invalid parameter
358+
// If it is an array, then it must be an array that can be evaluated
359+
// already
360+
if (Z_TYPE_P(nested_attribs) == IS_ARRAY) {
361+
zend_argument_type_error(
362+
2,
363+
"must be an array of objects"
364+
);
365+
} else {
366+
zend_wrong_parameter_error(
367+
ZPP_ERROR_WRONG_ARG,
368+
2,
369+
"attributes",
370+
Z_EXPECTED_ARRAY,
371+
nested_attribs
372+
);
373+
// Something with an invalid parameter
374+
}
366375
} else {
367376
zend_ast *attributes_ast = Z_ASTVAL_P(nested_attribs);
368377
compile_alias_attributes(&( alias_obj->attributes), attributes_ast);

0 commit comments

Comments
 (0)