Skip to content

Implement ReflectionProperty::hasHook[s] #15844

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

Closed
wants to merge 1 commit into from
Closed
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
35 changes: 35 additions & 0 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -6509,6 +6509,18 @@ ZEND_METHOD(ReflectionProperty, getDefaultValue)
}
/* }}} */

ZEND_METHOD(ReflectionProperty, hasHooks)
{
reflection_object *intern;
property_reference *ref;

ZEND_PARSE_PARAMETERS_NONE();

GET_REFLECTION_OBJECT_PTR(ref);

RETURN_BOOL(ref->prop && ref->prop->hooks);
}

ZEND_METHOD(ReflectionProperty, getHooks)
{
reflection_object *intern;
Expand Down Expand Up @@ -6538,6 +6550,29 @@ ZEND_METHOD(ReflectionProperty, getHooks)
}
}

ZEND_METHOD(ReflectionProperty, hasHook)
{

reflection_object *intern;
property_reference *ref;
zend_object *type;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJ_OF_CLASS(type, reflection_property_hook_type_ptr)
ZEND_PARSE_PARAMETERS_END();

GET_REFLECTION_OBJECT_PTR(ref);

zend_property_hook_kind kind;
if (zend_string_equals_literal(Z_STR_P(zend_enum_fetch_case_name(type)), "Get")) {
kind = ZEND_PROPERTY_HOOK_GET;
} else {
kind = ZEND_PROPERTY_HOOK_SET;
}

RETURN_BOOL(ref->prop && ref->prop->hooks && ref->prop->hooks[kind]);
}

ZEND_METHOD(ReflectionProperty, getHook)
{
reflection_object *intern;
Expand Down
4 changes: 4 additions & 0 deletions ext/reflection/php_reflection.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,13 @@ public function getDefaultValue(): mixed {}

public function getAttributes(?string $name = null, int $flags = 0): array {}

public function hasHooks(): bool {}

/** @return array<string, ReflectionMethod> */
public function getHooks(): array {}

public function hasHook(PropertyHookType $type): bool {}

public function getHook(PropertyHookType $type): ?ReflectionMethod {}
}

Expand Down
12 changes: 11 additions & 1 deletion ext/reflection/php_reflection_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
ReflectionClass::get{Get,Set}() inheritance
ReflectionClass::getHook() inheritance
--FILE--
<?php

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ class Test {

for ($i = 1; $i <= 4; $i++) {
$rp = new ReflectionProperty(Test::class, 'prop' . $i);
var_dump($rp->hasHooks());
var_dump($rp->getHooks());
}

?>
--EXPECT--
bool(false)
array(0) {
}
bool(true)
array(2) {
["get"]=>
object(ReflectionMethod)#1 (2) {
Expand All @@ -35,6 +38,7 @@ array(2) {
string(4) "Test"
}
}
bool(true)
array(1) {
["get"]=>
object(ReflectionMethod)#2 (2) {
Expand All @@ -44,6 +48,7 @@ array(1) {
string(4) "Test"
}
}
bool(true)
array(1) {
["set"]=>
object(ReflectionMethod)#3 (2) {
Expand Down
31 changes: 31 additions & 0 deletions ext/reflection/tests/property_hooks/basics.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Test {
get { echo "get\n"; }
set { echo "set($value)\n"; }
}
public $prop5 { get { echo "get\n"; } }
public $prop6 { set { echo "set($value)\n"; } }
}
abstract class Test2 {
abstract public $prop4 { get; set; }
Expand All @@ -27,13 +29,17 @@ function dumpFlags(ReflectionProperty $rp) {
$test = new Test;

$rp1 = new ReflectionProperty(Test::class, 'prop1');
var_dump($rp1->hasHook(PropertyHookType::Get));
var_dump($rp1->getHook(PropertyHookType::Get));
var_dump($rp1->hasHook(PropertyHookType::Set));
var_dump($rp1->getHook(PropertyHookType::Set));
dumpFlags($rp1);
echo "\n";

$rp2 = new ReflectionProperty(Test::class, 'prop2');
var_dump($rp2->hasHook(PropertyHookType::Get));
var_dump($g = $rp2->getHook(PropertyHookType::Get));
var_dump($rp2->hasHook(PropertyHookType::Set));
var_dump($s = $rp2->getHook(PropertyHookType::Set));
var_dump($g->invoke($test));
try {
Expand All @@ -48,7 +54,9 @@ dumpFlags($rp2);
echo "\n";

$rp3 = new ReflectionProperty(Test::class, 'prop3');
var_dump($rp3->hasHook(PropertyHookType::Get));
var_dump($g = $rp3->getHook(PropertyHookType::Get));
var_dump($rp3->hasHook(PropertyHookType::Set));
var_dump($s = $rp3->getHook(PropertyHookType::Set));
$g->invoke($test);
$s->invoke($test, 42);
Expand All @@ -57,19 +65,34 @@ echo "\n";

$rp4 = new ReflectionProperty(Test2::class, 'prop4');
dumpFlags($rp4);
echo "\n";

$rp5 = new ReflectionProperty(Test::class, 'prop5');
var_dump($rp5->hasHook(PropertyHookType::Get));
var_dump($rp5->hasHook(PropertyHookType::Set));
echo "\n";

$rp6 = new ReflectionProperty(Test::class, 'prop6');
var_dump($rp6->hasHook(PropertyHookType::Get));
var_dump($rp6->hasHook(PropertyHookType::Set));
echo "\n";

?>
--EXPECT--
bool(false)
NULL
bool(false)
NULL
Abstract: false false

bool(true)
object(ReflectionMethod)#6 (2) {
["name"]=>
string(11) "$prop2::get"
["class"]=>
string(4) "Test"
}
bool(true)
object(ReflectionMethod)#7 (2) {
["name"]=>
string(11) "$prop2::set"
Expand All @@ -80,12 +103,14 @@ NULL
NULL
Abstract: false false

bool(true)
object(ReflectionMethod)#9 (2) {
["name"]=>
string(11) "$prop3::get"
["class"]=>
string(4) "Test"
}
bool(true)
object(ReflectionMethod)#6 (2) {
["name"]=>
string(11) "$prop3::set"
Expand All @@ -97,3 +122,9 @@ set(42)
Abstract: false false

Abstract: true true

bool(true)
bool(false)

bool(false)
bool(true)
Loading