From a40f33a0631cfad59f140571c359f540dd6bd570 Mon Sep 17 00:00:00 2001 From: Ollie Read Date: Tue, 3 May 2022 13:25:53 +0100 Subject: [PATCH 1/5] Added ReflectionMethod::hasPrototype method --- ext/reflection/php_reflection.c | 16 +++++++++++ ext/reflection/php_reflection.stub.php | 3 ++ ext/reflection/php_reflection_arginfo.h | 6 +++- .../ReflectionMethod_hasPrototype_basic.phpt | 28 +++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 33ec42c75794e..15ef66376ec4c 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -3654,6 +3654,22 @@ ZEND_METHOD(ReflectionMethod, getDeclaringClass) } /* }}} */ +/* {{{ Returns whether a method has a prototype or not */ +ZEND_METHOD(ReflectionMethod, hasPrototype) +{ + reflection_object *intern; + zend_function *mptr; + + GET_REFLECTION_OBJECT_PTR(mptr); + + if (zend_parse_parameters_none() == FAILURE) { + RETURN_THROWS(); + } + + RETURN_BOOL(mptr->common.prototype != NULL); +} +/* }}} */ + /* {{{ Get the prototype */ ZEND_METHOD(ReflectionMethod, getPrototype) { diff --git a/ext/reflection/php_reflection.stub.php b/ext/reflection/php_reflection.stub.php index 8f2ef6ec6f44e..4a1db2e5bae07 100644 --- a/ext/reflection/php_reflection.stub.php +++ b/ext/reflection/php_reflection.stub.php @@ -204,6 +204,9 @@ public function getDeclaringClass(): ReflectionClass {} /** @tentative-return-type */ public function getPrototype(): ReflectionMethod {} + /** @tentative-return-type */ + public function hasPrototype(): bool {} + /** @tentative-return-type */ public function setAccessible(bool $accessible): void {} } diff --git a/ext/reflection/php_reflection_arginfo.h b/ext/reflection/php_reflection_arginfo.h index 12b01c4d264fd..aa4b613c46d2b 100644 --- a/ext/reflection/php_reflection_arginfo.h +++ b/ext/reflection/php_reflection_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 62fcf63d2f3e93537560c3a03e71fda131a31586 */ + * Stub hash: dcd4a469fb0431c98f4c9965a122717006b36415 */ ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_Reflection_getModifierNames, 0, 1, IS_ARRAY, 0) ZEND_ARG_TYPE_INFO(0, modifiers, IS_LONG, 0) @@ -167,6 +167,8 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_INFO_EX(arginfo_class_ReflectionMethod_getPrototype, 0, 0, ReflectionMethod, 0) ZEND_END_ARG_INFO() +#define arginfo_class_ReflectionMethod_hasPrototype arginfo_class_ReflectionFunctionAbstract_inNamespace + ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_ReflectionMethod_setAccessible, 0, 1, IS_VOID, 0) ZEND_ARG_TYPE_INFO(0, accessible, _IS_BOOL, 0) ZEND_END_ARG_INFO() @@ -656,6 +658,7 @@ ZEND_METHOD(ReflectionMethod, invoke); ZEND_METHOD(ReflectionMethod, invokeArgs); ZEND_METHOD(ReflectionMethod, getDeclaringClass); ZEND_METHOD(ReflectionMethod, getPrototype); +ZEND_METHOD(ReflectionMethod, hasPrototype); ZEND_METHOD(ReflectionMethod, setAccessible); ZEND_METHOD(ReflectionClass, __construct); ZEND_METHOD(ReflectionClass, __toString); @@ -914,6 +917,7 @@ static const zend_function_entry class_ReflectionMethod_methods[] = { ZEND_ME(ReflectionMethod, invokeArgs, arginfo_class_ReflectionMethod_invokeArgs, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionMethod, getDeclaringClass, arginfo_class_ReflectionMethod_getDeclaringClass, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionMethod, getPrototype, arginfo_class_ReflectionMethod_getPrototype, ZEND_ACC_PUBLIC) + ZEND_ME(ReflectionMethod, hasPrototype, arginfo_class_ReflectionMethod_hasPrototype, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionMethod, setAccessible, arginfo_class_ReflectionMethod_setAccessible, ZEND_ACC_PUBLIC) ZEND_FE_END }; diff --git a/ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt b/ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt new file mode 100644 index 0000000000000..2b88358cf0b2b --- /dev/null +++ b/ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt @@ -0,0 +1,28 @@ +--TEST-- +public ReflectionMethod ReflectionMethod::hasPrototype ( void ); +--CREDITS-- +ollieread - +--FILE-- +hasPrototype()); + +$reflectionMethod2 = new ReflectionMethod('Hello', 'sayHelloTo'); +var_dump($reflectionMethod2->hasPrototype()); + +?> +--EXPECT-- +bool(true) +bool(false) From 9f1573287444e49de443bb524f0f822edd58caca Mon Sep 17 00:00:00 2001 From: Ollie Read Date: Tue, 3 May 2022 14:20:21 +0100 Subject: [PATCH 2/5] Remove tentative-return-type docblock annotation --- ext/reflection/php_reflection.stub.php | 3 +-- ext/reflection/php_reflection_arginfo.h | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/ext/reflection/php_reflection.stub.php b/ext/reflection/php_reflection.stub.php index 4a1db2e5bae07..bc52937594c44 100644 --- a/ext/reflection/php_reflection.stub.php +++ b/ext/reflection/php_reflection.stub.php @@ -203,8 +203,7 @@ public function getDeclaringClass(): ReflectionClass {} /** @tentative-return-type */ public function getPrototype(): ReflectionMethod {} - - /** @tentative-return-type */ + public function hasPrototype(): bool {} /** @tentative-return-type */ diff --git a/ext/reflection/php_reflection_arginfo.h b/ext/reflection/php_reflection_arginfo.h index aa4b613c46d2b..585a9382fc218 100644 --- a/ext/reflection/php_reflection_arginfo.h +++ b/ext/reflection/php_reflection_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: dcd4a469fb0431c98f4c9965a122717006b36415 */ + * Stub hash: 5b5d4004910f915c0813886eb28bc801962e7f24 */ ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_Reflection_getModifierNames, 0, 1, IS_ARRAY, 0) ZEND_ARG_TYPE_INFO(0, modifiers, IS_LONG, 0) @@ -167,7 +167,7 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_INFO_EX(arginfo_class_ReflectionMethod_getPrototype, 0, 0, ReflectionMethod, 0) ZEND_END_ARG_INFO() -#define arginfo_class_ReflectionMethod_hasPrototype arginfo_class_ReflectionFunctionAbstract_inNamespace +#define arginfo_class_ReflectionMethod_hasPrototype arginfo_class_ReflectionFunctionAbstract_hasTentativeReturnType ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_ReflectionMethod_setAccessible, 0, 1, IS_VOID, 0) ZEND_ARG_TYPE_INFO(0, accessible, _IS_BOOL, 0) From 07092323869a63a0f32fdd98b7c92062f993f9d5 Mon Sep 17 00:00:00 2001 From: Ollie Read Date: Tue, 3 May 2022 15:00:13 +0100 Subject: [PATCH 3/5] Move reflection object retrieval to after param validation --- ext/reflection/php_reflection.c | 3 +-- ext/reflection/php_reflection.stub.php | 2 +- ext/reflection/php_reflection_arginfo.h | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 15ef66376ec4c..696e00136ee70 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -3660,12 +3660,11 @@ ZEND_METHOD(ReflectionMethod, hasPrototype) reflection_object *intern; zend_function *mptr; - GET_REFLECTION_OBJECT_PTR(mptr); - if (zend_parse_parameters_none() == FAILURE) { RETURN_THROWS(); } + GET_REFLECTION_OBJECT_PTR(mptr); RETURN_BOOL(mptr->common.prototype != NULL); } /* }}} */ diff --git a/ext/reflection/php_reflection.stub.php b/ext/reflection/php_reflection.stub.php index bc52937594c44..0307a9e64b477 100644 --- a/ext/reflection/php_reflection.stub.php +++ b/ext/reflection/php_reflection.stub.php @@ -203,7 +203,7 @@ public function getDeclaringClass(): ReflectionClass {} /** @tentative-return-type */ public function getPrototype(): ReflectionMethod {} - + public function hasPrototype(): bool {} /** @tentative-return-type */ diff --git a/ext/reflection/php_reflection_arginfo.h b/ext/reflection/php_reflection_arginfo.h index 585a9382fc218..aa512751b55ce 100644 --- a/ext/reflection/php_reflection_arginfo.h +++ b/ext/reflection/php_reflection_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 5b5d4004910f915c0813886eb28bc801962e7f24 */ + * Stub hash: 7cafe54fd9a94ea020367cd8f9ad090160905716 */ ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_Reflection_getModifierNames, 0, 1, IS_ARRAY, 0) ZEND_ARG_TYPE_INFO(0, modifiers, IS_LONG, 0) From 4340172b7aeea18fd52e579310162436d15cddf9 Mon Sep 17 00:00:00 2001 From: Ollie Read Date: Wed, 4 May 2022 12:20:23 +0100 Subject: [PATCH 4/5] Remove credits from hasPrototype basic test --- ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt | 2 -- 1 file changed, 2 deletions(-) diff --git a/ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt b/ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt index 2b88358cf0b2b..a8ba3e2c7f053 100644 --- a/ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt +++ b/ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt @@ -1,7 +1,5 @@ --TEST-- public ReflectionMethod ReflectionMethod::hasPrototype ( void ); ---CREDITS-- -ollieread - --FILE-- Date: Wed, 4 May 2022 12:21:37 +0100 Subject: [PATCH 5/5] Add test for prototypeless method in a child class --- .../tests/ReflectionMethod_hasPrototype_basic.phpt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt b/ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt index a8ba3e2c7f053..d94c8962e11b3 100644 --- a/ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt +++ b/ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt @@ -12,6 +12,10 @@ class HelloWorld extends Hello { public function sayHelloTo($name) { return 'Hello world: ' . $name; } + + public function sayHiTo($name) { + return 'Hi: ' . $name; + } } $reflectionMethod1 = new ReflectionMethod('HelloWorld', 'sayHelloTo'); @@ -20,7 +24,11 @@ var_dump($reflectionMethod1->hasPrototype()); $reflectionMethod2 = new ReflectionMethod('Hello', 'sayHelloTo'); var_dump($reflectionMethod2->hasPrototype()); +$reflectionMethod3 = new ReflectionMethod('HelloWorld', 'sayHiTo'); +var_dump($reflectionMethod3->hasPrototype()); + ?> --EXPECT-- bool(true) bool(false) +bool(false)