From c531e5da5801eecbd3fcb0ae63bb07f1eec85f44 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 5 May 2022 15:51:20 +0200 Subject: [PATCH] Add ReflectionFunction::isAnonymous() --- ext/reflection/php_reflection.c | 13 +++++++++++++ ext/reflection/php_reflection.stub.php | 2 ++ ext/reflection/php_reflection_arginfo.h | 6 +++++- .../tests/ReflectionFunction_isAnonymous.phpt | 17 +++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 ext/reflection/tests/ReflectionFunction_isAnonymous.phpt diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 6fb85154a767a..c3d695b0b0030 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -1790,6 +1790,19 @@ ZEND_METHOD(ReflectionFunctionAbstract, isUserDefined) } /* }}} */ +/* {{{ Returns whether this function is an anonymous closure or not */ +ZEND_METHOD(ReflectionFunction, isAnonymous) +{ + reflection_object *intern; + zend_function *fptr; + + ZEND_PARSE_PARAMETERS_NONE(); + + GET_REFLECTION_OBJECT_PTR(fptr); + RETURN_BOOL((fptr->common.fn_flags & (ZEND_ACC_CLOSURE | ZEND_ACC_FAKE_CLOSURE)) == ZEND_ACC_CLOSURE); +} +/* }}} */ + /* {{{ Returns whether this function has been disabled or not */ ZEND_METHOD(ReflectionFunction, isDisabled) { diff --git a/ext/reflection/php_reflection.stub.php b/ext/reflection/php_reflection.stub.php index 8f2ef6ec6f44e..240015f751d28 100644 --- a/ext/reflection/php_reflection.stub.php +++ b/ext/reflection/php_reflection.stub.php @@ -117,6 +117,8 @@ public function __construct(Closure|string $function) {} public function __toString(): string {} + public function isAnonymous(): bool {} + /** * @tentative-return-type * @deprecated ReflectionFunction can no longer be constructed for disabled functions diff --git a/ext/reflection/php_reflection_arginfo.h b/ext/reflection/php_reflection_arginfo.h index 12b01c4d264fd..92ff5329d5fbc 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: f06163b02a76ee7fa526e4662f015201e243743d */ 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) @@ -91,6 +91,8 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_ReflectionFunction___toString, 0, 0, IS_STRING, 0) ZEND_END_ARG_INFO() +#define arginfo_class_ReflectionFunction_isAnonymous arginfo_class_ReflectionFunctionAbstract_hasTentativeReturnType + #define arginfo_class_ReflectionFunction_isDisabled arginfo_class_ReflectionFunctionAbstract_inNamespace ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_ReflectionFunction_invoke, 0, 0, IS_MIXED, 0) @@ -630,6 +632,7 @@ ZEND_METHOD(ReflectionFunctionAbstract, getTentativeReturnType); ZEND_METHOD(ReflectionFunctionAbstract, getAttributes); ZEND_METHOD(ReflectionFunction, __construct); ZEND_METHOD(ReflectionFunction, __toString); +ZEND_METHOD(ReflectionFunction, isAnonymous); ZEND_METHOD(ReflectionFunction, isDisabled); ZEND_METHOD(ReflectionFunction, invoke); ZEND_METHOD(ReflectionFunction, invokeArgs); @@ -878,6 +881,7 @@ static const zend_function_entry class_ReflectionFunctionAbstract_methods[] = { static const zend_function_entry class_ReflectionFunction_methods[] = { ZEND_ME(ReflectionFunction, __construct, arginfo_class_ReflectionFunction___construct, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionFunction, __toString, arginfo_class_ReflectionFunction___toString, ZEND_ACC_PUBLIC) + ZEND_ME(ReflectionFunction, isAnonymous, arginfo_class_ReflectionFunction_isAnonymous, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionFunction, isDisabled, arginfo_class_ReflectionFunction_isDisabled, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED) ZEND_ME(ReflectionFunction, invoke, arginfo_class_ReflectionFunction_invoke, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionFunction, invokeArgs, arginfo_class_ReflectionFunction_invokeArgs, ZEND_ACC_PUBLIC) diff --git a/ext/reflection/tests/ReflectionFunction_isAnonymous.phpt b/ext/reflection/tests/ReflectionFunction_isAnonymous.phpt new file mode 100644 index 0000000000000..004e0b09af0e0 --- /dev/null +++ b/ext/reflection/tests/ReflectionFunction_isAnonymous.phpt @@ -0,0 +1,17 @@ +--TEST-- +ReflectionFunction::isAnonymous +--FILE-- +isAnonymous()); + +$rf = new ReflectionFunction('strlen'); +var_dump($rf->isAnonymous()); + +$rf = new ReflectionFunction(strlen(...)); +var_dump($rf->isAnonymous()); +?> +--EXPECT-- +bool(true) +bool(false) +bool(false)