Skip to content

Commit d75a289

Browse files
committed
Implement ReflectionProperty::hasHook[s]
Closes GH-15844
1 parent b438e2b commit d75a289

File tree

7 files changed

+90
-2
lines changed

7 files changed

+90
-2
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.4.0RC1
44

5+
- Core:
6+
. Add missing ReflectionProperty::hasHook[s]() methods. (ilutov)
7+
58
- DOM:
69
. Fix XML serializer errata: xmlns="" serialization should be allowed.
710
(nielsdos)

ext/reflection/php_reflection.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6509,6 +6509,18 @@ ZEND_METHOD(ReflectionProperty, getDefaultValue)
65096509
}
65106510
/* }}} */
65116511

6512+
ZEND_METHOD(ReflectionProperty, hasHooks)
6513+
{
6514+
reflection_object *intern;
6515+
property_reference *ref;
6516+
6517+
ZEND_PARSE_PARAMETERS_NONE();
6518+
6519+
GET_REFLECTION_OBJECT_PTR(ref);
6520+
6521+
RETURN_BOOL(ref->prop && ref->prop->hooks);
6522+
}
6523+
65126524
ZEND_METHOD(ReflectionProperty, getHooks)
65136525
{
65146526
reflection_object *intern;
@@ -6538,6 +6550,29 @@ ZEND_METHOD(ReflectionProperty, getHooks)
65386550
}
65396551
}
65406552

6553+
ZEND_METHOD(ReflectionProperty, hasHook)
6554+
{
6555+
6556+
reflection_object *intern;
6557+
property_reference *ref;
6558+
zend_object *type;
6559+
6560+
ZEND_PARSE_PARAMETERS_START(1, 1)
6561+
Z_PARAM_OBJ_OF_CLASS(type, reflection_property_hook_type_ptr)
6562+
ZEND_PARSE_PARAMETERS_END();
6563+
6564+
GET_REFLECTION_OBJECT_PTR(ref);
6565+
6566+
zend_property_hook_kind kind;
6567+
if (zend_string_equals_literal(Z_STR_P(zend_enum_fetch_case_name(type)), "Get")) {
6568+
kind = ZEND_PROPERTY_HOOK_GET;
6569+
} else {
6570+
kind = ZEND_PROPERTY_HOOK_SET;
6571+
}
6572+
6573+
RETURN_BOOL(ref->prop && ref->prop->hooks && ref->prop->hooks[kind]);
6574+
}
6575+
65416576
ZEND_METHOD(ReflectionProperty, getHook)
65426577
{
65436578
reflection_object *intern;

ext/reflection/php_reflection.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,13 @@ public function getDefaultValue(): mixed {}
557557

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

560+
public function hasHooks(): bool {}
561+
560562
/** @return array<string, ReflectionMethod> */
561563
public function getHooks(): array {}
562564

565+
public function hasHook(PropertyHookType $type): bool {}
566+
563567
public function getHook(PropertyHookType $type): ?ReflectionMethod {}
564568
}
565569

ext/reflection/php_reflection_arginfo.h

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/reflection/tests/property_hooks/ReflectionProperty_getHook_inheritance.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
ReflectionClass::get{Get,Set}() inheritance
2+
ReflectionClass::getHook() inheritance
33
--FILE--
44
<?php
55

ext/reflection/tests/property_hooks/ReflectionProperty_getHooks.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ class Test {
1212

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

1819
?>
1920
--EXPECT--
21+
bool(false)
2022
array(0) {
2123
}
24+
bool(true)
2225
array(2) {
2326
["get"]=>
2427
object(ReflectionMethod)#1 (2) {
@@ -35,6 +38,7 @@ array(2) {
3538
string(4) "Test"
3639
}
3740
}
41+
bool(true)
3842
array(1) {
3943
["get"]=>
4044
object(ReflectionMethod)#2 (2) {
@@ -44,6 +48,7 @@ array(1) {
4448
string(4) "Test"
4549
}
4650
}
51+
bool(true)
4752
array(1) {
4853
["set"]=>
4954
object(ReflectionMethod)#3 (2) {

ext/reflection/tests/property_hooks/basics.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Test {
1010
get { echo "get\n"; }
1111
set { echo "set($value)\n"; }
1212
}
13+
public $prop5 { get { echo "get\n"; } }
14+
public $prop6 { set { echo "set($value)\n"; } }
1315
}
1416
abstract class Test2 {
1517
abstract public $prop4 { get; set; }
@@ -27,13 +29,17 @@ function dumpFlags(ReflectionProperty $rp) {
2729
$test = new Test;
2830

2931
$rp1 = new ReflectionProperty(Test::class, 'prop1');
32+
var_dump($rp1->hasHook(PropertyHookType::Get));
3033
var_dump($rp1->getHook(PropertyHookType::Get));
34+
var_dump($rp1->hasHook(PropertyHookType::Set));
3135
var_dump($rp1->getHook(PropertyHookType::Set));
3236
dumpFlags($rp1);
3337
echo "\n";
3438

3539
$rp2 = new ReflectionProperty(Test::class, 'prop2');
40+
var_dump($rp2->hasHook(PropertyHookType::Get));
3641
var_dump($g = $rp2->getHook(PropertyHookType::Get));
42+
var_dump($rp2->hasHook(PropertyHookType::Set));
3743
var_dump($s = $rp2->getHook(PropertyHookType::Set));
3844
var_dump($g->invoke($test));
3945
try {
@@ -48,7 +54,9 @@ dumpFlags($rp2);
4854
echo "\n";
4955

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

5866
$rp4 = new ReflectionProperty(Test2::class, 'prop4');
5967
dumpFlags($rp4);
68+
echo "\n";
69+
70+
$rp5 = new ReflectionProperty(Test::class, 'prop5');
71+
var_dump($rp5->hasHook(PropertyHookType::Get));
72+
var_dump($rp5->hasHook(PropertyHookType::Set));
73+
echo "\n";
74+
75+
$rp6 = new ReflectionProperty(Test::class, 'prop6');
76+
var_dump($rp6->hasHook(PropertyHookType::Get));
77+
var_dump($rp6->hasHook(PropertyHookType::Set));
78+
echo "\n";
6079

6180
?>
6281
--EXPECT--
82+
bool(false)
6383
NULL
84+
bool(false)
6485
NULL
6586
Abstract: false false
6687

88+
bool(true)
6789
object(ReflectionMethod)#6 (2) {
6890
["name"]=>
6991
string(11) "$prop2::get"
7092
["class"]=>
7193
string(4) "Test"
7294
}
95+
bool(true)
7396
object(ReflectionMethod)#7 (2) {
7497
["name"]=>
7598
string(11) "$prop2::set"
@@ -80,12 +103,14 @@ NULL
80103
NULL
81104
Abstract: false false
82105

106+
bool(true)
83107
object(ReflectionMethod)#9 (2) {
84108
["name"]=>
85109
string(11) "$prop3::get"
86110
["class"]=>
87111
string(4) "Test"
88112
}
113+
bool(true)
89114
object(ReflectionMethod)#6 (2) {
90115
["name"]=>
91116
string(11) "$prop3::set"
@@ -97,3 +122,9 @@ set(42)
97122
Abstract: false false
98123

99124
Abstract: true true
125+
126+
bool(true)
127+
bool(false)
128+
129+
bool(false)
130+
bool(true)

0 commit comments

Comments
 (0)