From f590782b60a4da31487e00a3d0accfaf5d406b1d Mon Sep 17 00:00:00 2001 From: Ollie Read Date: Wed, 4 May 2022 13:21:37 +0200 Subject: [PATCH] Add ReflectionMethod::hasPrototype method Closes GH-8487. --- NEWS | 1 + UPGRADING | 1 + ext/reflection/php_reflection.c | 15 ++++++++ ext/reflection/php_reflection.stub.php | 2 ++ ext/reflection/php_reflection_arginfo.h | 6 +++- .../ReflectionMethod_hasPrototype_basic.phpt | 34 +++++++++++++++++++ 6 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt diff --git a/NEWS b/NEWS index 498072b49fc..713620b2845 100644 --- a/NEWS +++ b/NEWS @@ -36,6 +36,7 @@ PHP NEWS - Reflection: . Added ReflectionFunction::isAnonymous(). (Nicolas Grekas) + . Added ReflectionMethod::hasPrototype(). (Ollie Read) - Sodium: . Added sodium_crypto_stream_xchacha20_xor_ic(). (Scott) diff --git a/UPGRADING b/UPGRADING index f8896587693..215c5edbd85 100644 --- a/UPGRADING +++ b/UPGRADING @@ -140,6 +140,7 @@ PHP 8.2 UPGRADE NOTES - Reflection: . ReflectionFunction::isAnonymous() + . ReflectionMethod::hasPrototype() - Sodium: . sodium_crypto_stream_xchacha20_xor_ic() diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index c3d695b0b00..5ca89f0d2a7 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -3667,6 +3667,21 @@ ZEND_METHOD(ReflectionMethod, getDeclaringClass) } /* }}} */ +/* {{{ Returns whether a method has a prototype or not */ +ZEND_METHOD(ReflectionMethod, hasPrototype) +{ + reflection_object *intern; + zend_function *mptr; + + if (zend_parse_parameters_none() == FAILURE) { + RETURN_THROWS(); + } + + GET_REFLECTION_OBJECT_PTR(mptr); + 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 240015f751d..22d01b69051 100644 --- a/ext/reflection/php_reflection.stub.php +++ b/ext/reflection/php_reflection.stub.php @@ -206,6 +206,8 @@ class ReflectionMethod extends ReflectionFunctionAbstract /** @tentative-return-type */ public function getPrototype(): ReflectionMethod {} + 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 92ff5329d5f..25a750f2bdc 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: f06163b02a76ee7fa526e4662f015201e243743d */ + * Stub hash: c9656b23db965e890e73d0064005b981ee1991cf */ 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) @@ -169,6 +169,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_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) ZEND_END_ARG_INFO() @@ -659,6 +661,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); @@ -918,6 +921,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 00000000000..d94c8962e11 --- /dev/null +++ b/ext/reflection/tests/ReflectionMethod_hasPrototype_basic.phpt @@ -0,0 +1,34 @@ +--TEST-- +public ReflectionMethod ReflectionMethod::hasPrototype ( void ); +--FILE-- +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)