Skip to content

Introduce ReflectionAttribute::getTargetReflector(). #19066

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions Zend/tests/attributes/020_userland_attribute_validation.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ $ref = new \ReflectionFunction(#[A1] function () { });
$attr = $ref->getAttributes()[0];
var_dump($attr->getName(), $attr->getTarget() == Attribute::TARGET_FUNCTION, $attr->isRepeated());
var_dump(get_class($attr->newInstance()));
assert($attr->getTargetReflector() === $ref);

echo "\n";

$ref = new \ReflectionObject(new #[A1] class() { });
$attr = $ref->getAttributes()[0];
var_dump($attr->getName(), $attr->getTarget() == Attribute::TARGET_CLASS, $attr->isRepeated());
assert($attr->getTargetReflector() === $ref);

try {
$attr->newInstance();
Expand All @@ -28,6 +30,7 @@ echo "\n";
$ref = new \ReflectionFunction(#[A1] #[A1] function () { });
$attr = $ref->getAttributes()[0];
var_dump($attr->getName(), $attr->getTarget() == Attribute::TARGET_FUNCTION, $attr->isRepeated());
assert($attr->getTargetReflector() === $ref);

try {
$attr->newInstance();
Expand All @@ -44,6 +47,7 @@ $ref = new \ReflectionObject(new #[A2] #[A2] class() { });
$attr = $ref->getAttributes()[0];
var_dump($attr->getName(), $attr->getTarget() == Attribute::TARGET_CLASS, $attr->isRepeated());
var_dump(get_class($attr->newInstance()));
assert($attr->getTargetReflector() === $ref);

?>
--EXPECT--
Expand Down
45 changes: 33 additions & 12 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ typedef struct _attribute_reference {
zend_class_entry *scope;
zend_string *filename;
uint32_t target;
zval target_reflector;
} attribute_reference;

typedef enum {
Expand Down Expand Up @@ -1236,7 +1237,7 @@ static void _extension_string(smart_str *str, const zend_module_entry *module, c

/* {{{ reflection_attribute_factory */
static void reflection_attribute_factory(zval *object, HashTable *attributes, zend_attribute *data,
zend_class_entry *scope, uint32_t target, zend_string *filename)
zend_class_entry *scope, uint32_t target, zend_string *filename, zval *target_reflector)
{
reflection_object *intern;
attribute_reference *reference;
Expand All @@ -1249,14 +1250,15 @@ static void reflection_attribute_factory(zval *object, HashTable *attributes, ze
reference->scope = scope;
reference->filename = filename ? zend_string_copy(filename) : NULL;
reference->target = target;
ZVAL_COPY(&reference->target_reflector, target_reflector);
intern->ptr = reference;
intern->ref_type = REF_TYPE_ATTRIBUTE;
ZVAL_STR_COPY(reflection_prop_name(object), data->name);
}
/* }}} */

static int read_attributes(zval *ret, HashTable *attributes, zend_class_entry *scope,
uint32_t offset, uint32_t target, zend_string *name, zend_class_entry *base, zend_string *filename) /* {{{ */
uint32_t offset, uint32_t target, zend_string *name, zend_class_entry *base, zend_string *filename, zval *target_reflector) /* {{{ */
{
ZEND_ASSERT(attributes != NULL);

Expand All @@ -1269,7 +1271,7 @@ static int read_attributes(zval *ret, HashTable *attributes, zend_class_entry *s

ZEND_HASH_PACKED_FOREACH_PTR(attributes, attr) {
if (attr->offset == offset && zend_string_equals(attr->lcname, filter)) {
reflection_attribute_factory(&tmp, attributes, attr, scope, target, filename);
reflection_attribute_factory(&tmp, attributes, attr, scope, target, filename, target_reflector);
add_next_index_zval(ret, &tmp);
}
} ZEND_HASH_FOREACH_END();
Expand Down Expand Up @@ -1301,7 +1303,7 @@ static int read_attributes(zval *ret, HashTable *attributes, zend_class_entry *s
}
}

reflection_attribute_factory(&tmp, attributes, attr, scope, target, filename);
reflection_attribute_factory(&tmp, attributes, attr, scope, target, filename, target_reflector);
add_next_index_zval(ret, &tmp);
} ZEND_HASH_FOREACH_END();

Expand All @@ -1310,7 +1312,7 @@ static int read_attributes(zval *ret, HashTable *attributes, zend_class_entry *s
/* }}} */

static void reflect_attributes(INTERNAL_FUNCTION_PARAMETERS, HashTable *attributes,
uint32_t offset, zend_class_entry *scope, uint32_t target, zend_string *filename) /* {{{ */
uint32_t offset, zend_class_entry *scope, uint32_t target, zend_string *filename, zval *target_reflector) /* {{{ */
{
zend_string *name = NULL;
zend_long flags = 0;
Expand Down Expand Up @@ -1343,7 +1345,7 @@ static void reflect_attributes(INTERNAL_FUNCTION_PARAMETERS, HashTable *attribut

array_init(return_value);

if (FAILURE == read_attributes(return_value, attributes, scope, offset, target, name, base, filename)) {
if (FAILURE == read_attributes(return_value, attributes, scope, offset, target, name, base, filename, target_reflector)) {
RETURN_THROWS();
}
}
Expand Down Expand Up @@ -2056,7 +2058,8 @@ ZEND_METHOD(ReflectionFunctionAbstract, getAttributes)

reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
fptr->common.attributes, 0, fptr->common.scope, target,
fptr->type == ZEND_USER_FUNCTION ? fptr->op_array.filename : NULL);
fptr->type == ZEND_USER_FUNCTION ? fptr->op_array.filename : NULL,
ZEND_THIS);
}
/* }}} */

Expand Down Expand Up @@ -2898,7 +2901,8 @@ ZEND_METHOD(ReflectionParameter, getAttributes)

reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
attributes, param->offset + 1, scope, ZEND_ATTRIBUTE_TARGET_PARAMETER,
param->fptr->type == ZEND_USER_FUNCTION ? param->fptr->op_array.filename : NULL);
param->fptr->type == ZEND_USER_FUNCTION ? param->fptr->op_array.filename : NULL,
ZEND_THIS);
}

/* {{{ Returns the index of the parameter, starting from 0 */
Expand Down Expand Up @@ -4020,7 +4024,8 @@ ZEND_METHOD(ReflectionClassConstant, getAttributes)

reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
ref->attributes, 0, ref->ce, ZEND_ATTRIBUTE_TARGET_CLASS_CONST,
ref->ce->type == ZEND_USER_CLASS ? ref->ce->info.user.filename : NULL);
ref->ce->type == ZEND_USER_CLASS ? ref->ce->info.user.filename : NULL,
ZEND_THIS);
}
/* }}} */

Expand Down Expand Up @@ -4425,7 +4430,8 @@ ZEND_METHOD(ReflectionClass, getAttributes)

reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
ce->attributes, 0, ce, ZEND_ATTRIBUTE_TARGET_CLASS,
ce->type == ZEND_USER_CLASS ? ce->info.user.filename : NULL);
ce->type == ZEND_USER_CLASS ? ce->info.user.filename : NULL,
ZEND_THIS);
}
/* }}} */

Expand Down Expand Up @@ -6353,7 +6359,8 @@ ZEND_METHOD(ReflectionProperty, getAttributes)

reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
ref->prop->attributes, 0, ref->prop->ce, ZEND_ATTRIBUTE_TARGET_PROPERTY,
ref->prop->ce->type == ZEND_USER_CLASS ? ref->prop->ce->info.user.filename : NULL);
ref->prop->ce->type == ZEND_USER_CLASS ? ref->prop->ce->info.user.filename : NULL,
ZEND_THIS);
}
/* }}} */

Expand Down Expand Up @@ -7211,6 +7218,19 @@ ZEND_METHOD(ReflectionAttribute, getTarget)
}
/* }}} */

/* {{{ Returns the reflection object that attribute was obtained from of the attribute */
ZEND_METHOD(ReflectionAttribute, getTargetReflector)
{
reflection_object *intern;
attribute_reference *attr;

ZEND_PARSE_PARAMETERS_NONE();
GET_REFLECTION_OBJECT_PTR(attr);

RETURN_COPY(&attr->target_reflector);
}
/* }}} */

/* {{{ Returns true if the attribute is repeated */
ZEND_METHOD(ReflectionAttribute, isRepeated)
{
Expand Down Expand Up @@ -7822,7 +7842,8 @@ ZEND_METHOD(ReflectionConstant, getAttributes)

reflect_attributes(INTERNAL_FUNCTION_PARAM_PASSTHRU,
const_->attributes, 0, NULL, ZEND_ATTRIBUTE_TARGET_CONST,
const_->filename);
const_->filename,
ZEND_THIS);
}

ZEND_METHOD(ReflectionConstant, __toString)
Expand Down
1 change: 1 addition & 0 deletions ext/reflection/php_reflection.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ class ReflectionAttribute implements Reflector

public function getName(): string {}
public function getTarget(): int {}
public function getTargetReflector(): Reflector {}
public function isRepeated(): bool {}
public function getArguments(): array {}
public function newInstance(): object {}
Expand Down
Loading