Skip to content

[DRAFT] [WIP] Initial zend_class_alias #18789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 24 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More tests
  • Loading branch information
DanielEScherzer committed Jun 9, 2025
commit 0ef14f0ef151d2aea7ad479106e6f1c276798dbb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
#[ClassAlias] - attributes parameter validated at compile time (non-object in array)
--FILE--
<?php

#[ClassAlias('Other', [true, new MissingAttribute()])]
class Demo {}

$ref = new ReflectionClass( 'Demo' );
$attrib = $ref->getAttributes()[0];
var_dump( $attrib );

$attrib->newInstance();

?>
--EXPECTF--
Fatal error: Attribute must be declared with `new` in %s on line %d
17 changes: 17 additions & 0 deletions Zend/tests/attributes/class_alias/attributes_array_non-object.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
#[ClassAlias] - attributes parameter validated at compile time (array can be evaluated)
--FILE--
<?php

#[ClassAlias('Other', [true])]
class Demo {}

$ref = new ReflectionClass( 'Demo' );
$attrib = $ref->getAttributes()[0];
var_dump( $attrib );

$attrib->newInstance();

?>
--EXPECTF--
Fatal error: ClassAlias::__construct(): Argument #2 ($attributes) must be an array of objects in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
#[ClassAlias] - attributes parameter validated at runtime (missing class)
--FILE--
<?php

#[ClassAlias('Other', [new MissingAttribute()])]
class Demo {}

$ref = new ReflectionClass( 'Demo' );
$attrib = $ref->getAttributes()[0];
var_dump( $attrib );

$attrib->newInstance();

?>
--EXPECTF--
object(ReflectionAttribute)#%d (1) {
["name"]=>
string(10) "ClassAlias"
}

Fatal error: Uncaught Error: Class "MissingAttribute" not found in %s:%d
Stack trace:
#0 %s(%d): ReflectionAttribute->newInstance()
#1 {main}
thrown in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
#[ClassAlias] - attributes parameter value types not validated when constructing ClassAlias
--FILE--
<?php

class NonAttribute {}

#[ClassAlias('Other', [new NonAttribute()])]
class Demo {}

$ref = new ReflectionClass( 'Demo' );
$attrib = $ref->getAttributes()[0];
var_dump( $attrib );

var_dump( $attrib->newInstance() );

?>
--EXPECTF--
object(ReflectionAttribute)#%d (1) {
["name"]=>
string(10) "ClassAlias"
}
object(ClassAlias)#%d (1) {
["alias"]=>
string(5) "Other"
}
25 changes: 17 additions & 8 deletions Zend/zend_attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,23 @@ static void validate_class_alias(
}
ZEND_ASSERT(nested_attribs != NULL);
if (UNEXPECTED(!Z_OPT_CONSTANT_P(nested_attribs))) {
zend_wrong_parameter_error(
ZPP_ERROR_WRONG_ARG,
2,
"attributes",
Z_EXPECTED_ARRAY,
nested_attribs
);
// Something with an invalid parameter
// If it is an array, then it must be an array that can be evaluated
// already
if (Z_TYPE_P(nested_attribs) == IS_ARRAY) {
zend_argument_type_error(
2,
"must be an array of objects"
);
} else {
zend_wrong_parameter_error(
ZPP_ERROR_WRONG_ARG,
2,
"attributes",
Z_EXPECTED_ARRAY,
nested_attribs
);
// Something with an invalid parameter
}
} else {
zend_ast *attributes_ast = Z_ASTVAL_P(nested_attribs);
compile_alias_attributes(&( alias_obj->attributes), attributes_ast);
Expand Down
Loading