From 03f84012b485163c71a15ccb21bc02b211ef268f Mon Sep 17 00:00:00 2001 From: Khaled Alam Date: Sun, 11 Jan 2026 05:22:55 +0400 Subject: [PATCH] ext/reflection: Add ReflectionConstant::inNamespace() method --- ext/reflection/php_reflection.c | 14 +++++++ ext/reflection/php_reflection.stub.php | 2 + ext/reflection/php_reflection_arginfo.h | 6 ++- .../tests/ReflectionConstant_inNamespace.phpt | 39 +++++++++++++++++++ .../tests/ReflectionConstant_ns.phpt | 8 ++++ 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 ext/reflection/tests/ReflectionConstant_inNamespace.phpt diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 64e79651913fb..0b8b3c018c314 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -7714,6 +7714,20 @@ ZEND_METHOD(ReflectionConstant, getName) RETURN_STR_COPY(const_->name); } +ZEND_METHOD(ReflectionConstant, inNamespace) +{ + reflection_object *intern; + zend_constant *const_; + + ZEND_PARSE_PARAMETERS_NONE(); + + GET_REFLECTION_OBJECT_PTR(const_); + + const char *backslash = zend_memrchr(ZSTR_VAL(const_->name), '\\', ZSTR_LEN(const_->name)); + RETURN_BOOL(backslash); +} +/* }}} */ + ZEND_METHOD(ReflectionConstant, getNamespaceName) { reflection_object *intern; diff --git a/ext/reflection/php_reflection.stub.php b/ext/reflection/php_reflection.stub.php index 91c70d6ffdb1a..95f3a37f48421 100644 --- a/ext/reflection/php_reflection.stub.php +++ b/ext/reflection/php_reflection.stub.php @@ -912,6 +912,8 @@ public function __construct(string $name) {} public function getName(): string {} + public function inNamespace(): bool {} + public function getNamespaceName(): string {} public function getShortName(): string {} diff --git a/ext/reflection/php_reflection_arginfo.h b/ext/reflection/php_reflection_arginfo.h index 62275423b3caf..4079264ea8478 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: fd645a0b0db39d94ca25b39ffe64d7f05bad6bea */ + * Stub hash: e0e5136dbded3eddc9238411d7669d76db9423a7 */ 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) @@ -702,6 +702,8 @@ ZEND_END_ARG_INFO() #define arginfo_class_ReflectionConstant_getName arginfo_class_ReflectionFunction___toString +#define arginfo_class_ReflectionConstant_inNamespace arginfo_class_ReflectionFunctionAbstract_hasTentativeReturnType + #define arginfo_class_ReflectionConstant_getNamespaceName arginfo_class_ReflectionFunction___toString #define arginfo_class_ReflectionConstant_getShortName arginfo_class_ReflectionFunction___toString @@ -984,6 +986,7 @@ ZEND_METHOD(ReflectionFiber, getCallable); ZEND_METHOD(ReflectionFiber, getTrace); ZEND_METHOD(ReflectionConstant, __construct); ZEND_METHOD(ReflectionConstant, getName); +ZEND_METHOD(ReflectionConstant, inNamespace); ZEND_METHOD(ReflectionConstant, getNamespaceName); ZEND_METHOD(ReflectionConstant, getShortName); ZEND_METHOD(ReflectionConstant, getValue); @@ -1354,6 +1357,7 @@ static const zend_function_entry class_ReflectionFiber_methods[] = { static const zend_function_entry class_ReflectionConstant_methods[] = { ZEND_ME(ReflectionConstant, __construct, arginfo_class_ReflectionConstant___construct, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionConstant, getName, arginfo_class_ReflectionConstant_getName, ZEND_ACC_PUBLIC) + ZEND_ME(ReflectionConstant, inNamespace, arginfo_class_ReflectionConstant_inNamespace, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionConstant, getNamespaceName, arginfo_class_ReflectionConstant_getNamespaceName, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionConstant, getShortName, arginfo_class_ReflectionConstant_getShortName, ZEND_ACC_PUBLIC) ZEND_ME(ReflectionConstant, getValue, arginfo_class_ReflectionConstant_getValue, ZEND_ACC_PUBLIC) diff --git a/ext/reflection/tests/ReflectionConstant_inNamespace.phpt b/ext/reflection/tests/ReflectionConstant_inNamespace.phpt new file mode 100644 index 0000000000000..16001b5f93178 --- /dev/null +++ b/ext/reflection/tests/ReflectionConstant_inNamespace.phpt @@ -0,0 +1,39 @@ +--TEST-- +ReflectionConstant::inNamespace() +--FILE-- +inNamespace()); + var_dump($rc1->getNamespaceName()); + var_dump($rc1->getShortName()); + + $rc2 = new ReflectionConstant('Foo\Bar\NAMESPACED_CONST'); + var_dump($rc2->inNamespace()); + var_dump($rc2->getNamespaceName()); + var_dump($rc2->getShortName()); + + $rc3 = new ReflectionConstant('E_ERROR'); + var_dump($rc3->inNamespace()); + var_dump($rc3->getNamespaceName()); + var_dump($rc3->getShortName()); +} + +?> +--EXPECT-- +bool(false) +string(0) "" +string(12) "GLOBAL_CONST" +bool(true) +string(7) "Foo\Bar" +string(16) "NAMESPACED_CONST" +bool(false) +string(0) "" +string(7) "E_ERROR" diff --git a/ext/reflection/tests/ReflectionConstant_ns.phpt b/ext/reflection/tests/ReflectionConstant_ns.phpt index b047fc25dcf29..36bf8a08672a4 100644 --- a/ext/reflection/tests/ReflectionConstant_ns.phpt +++ b/ext/reflection/tests/ReflectionConstant_ns.phpt @@ -14,6 +14,10 @@ namespace { var_dump(new \ReflectionConstant('\\C')); var_dump(new \ReflectionConstant('Foo\\C')); var_dump(new \ReflectionConstant('\\Foo\\C')); + var_dump((new \ReflectionConstant('C'))->inNamespace()); + var_dump((new \ReflectionConstant('\\C'))->inNamespace()); + var_dump((new \ReflectionConstant('Foo\\C'))->inNamespace()); + var_dump((new \ReflectionConstant('\\Foo\\C'))->inNamespace()); var_dump((new \ReflectionConstant('C'))->getNamespaceName()); var_dump((new \ReflectionConstant('\\C'))->getNamespaceName()); var_dump((new \ReflectionConstant('Foo\\C'))->getNamespaceName()); @@ -42,6 +46,10 @@ object(ReflectionConstant)#1 (1) { ["name"]=> string(6) "\Foo\C" } +bool(false) +bool(false) +bool(true) +bool(true) string(0) "" string(0) "" string(3) "Foo"