Skip to content

Commit c911341

Browse files
GH-16571: add ReflectionConstant::getExtension() and ::getExtensionName()
1 parent 1e08c15 commit c911341

File tree

5 files changed

+139
-8
lines changed

5 files changed

+139
-8
lines changed

ext/reflection/php_reflection.c

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,10 +1321,21 @@ PHPAPI void zend_reflection_class_factory(zend_class_entry *ce, zval *object)
13211321
}
13221322
/* }}} */
13231323

1324+
/* {{{ reflection_extension_factory_ex */
1325+
static void reflection_extension_factory_ex(zval *object, struct _zend_module_entry *module)
1326+
{
1327+
reflection_instantiate(reflection_extension_ptr, object);
1328+
reflection_object *intern = Z_REFLECTION_P(object);
1329+
intern->ptr = module;
1330+
intern->ref_type = REF_TYPE_OTHER;
1331+
intern->ce = NULL;
1332+
ZVAL_STRINGL(reflection_prop_name(object), module->name, strlen(module->name));
1333+
}
1334+
/* }}} */
1335+
13241336
/* {{{ reflection_extension_factory */
13251337
static void reflection_extension_factory(zval *object, const char *name_str)
13261338
{
1327-
reflection_object *intern;
13281339
size_t name_len = strlen(name_str);
13291340
zend_string *lcname;
13301341
struct _zend_module_entry *module;
@@ -1337,12 +1348,7 @@ static void reflection_extension_factory(zval *object, const char *name_str)
13371348
return;
13381349
}
13391350

1340-
reflection_instantiate(reflection_extension_ptr, object);
1341-
intern = Z_REFLECTION_P(object);
1342-
intern->ptr = module;
1343-
intern->ref_type = REF_TYPE_OTHER;
1344-
intern->ce = NULL;
1345-
ZVAL_STRINGL(reflection_prop_name(object), module->name, name_len);
1351+
reflection_extension_factory_ex(object, module);
13461352
}
13471353
/* }}} */
13481354

@@ -7835,6 +7841,59 @@ ZEND_METHOD(ReflectionConstant, isDeprecated)
78357841
RETURN_BOOL(ZEND_CONSTANT_FLAGS(const_) & CONST_DEPRECATED);
78367842
}
78377843

7844+
static void reflection_constant_find_ext(INTERNAL_FUNCTION_PARAMETERS, bool only_name)
7845+
{
7846+
reflection_object *intern;
7847+
zend_constant *const_;
7848+
7849+
ZEND_PARSE_PARAMETERS_NONE();
7850+
7851+
GET_REFLECTION_OBJECT_PTR(const_);
7852+
int module_number = ZEND_CONSTANT_MODULE_NUMBER(const_);
7853+
if (module_number == PHP_USER_CONSTANT) {
7854+
// For user constants, ReflectionConstant::getExtension() returns null,
7855+
// ReflectionConstant::getExtensionName() returns false
7856+
if (only_name) {
7857+
RETURN_FALSE;
7858+
}
7859+
RETURN_NULL();
7860+
}
7861+
zend_module_entry *module;
7862+
ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
7863+
if (module->module_number != module_number) {
7864+
continue;
7865+
}
7866+
if (only_name) {
7867+
RETURN_STRING(module->name);
7868+
}
7869+
reflection_extension_factory_ex(return_value, module);
7870+
return;
7871+
} ZEND_HASH_FOREACH_END();
7872+
7873+
zend_throw_exception_ex(
7874+
reflection_exception_ptr,
7875+
0,
7876+
"Unable to locate extension with module_number %d that provides constant %s",
7877+
module_number,
7878+
ZSTR_VAL(const_->name)
7879+
);
7880+
RETURN_THROWS();
7881+
}
7882+
7883+
/* {{{ Returns NULL or the extension the constant belongs to */
7884+
ZEND_METHOD(ReflectionConstant, getExtension)
7885+
{
7886+
reflection_constant_find_ext(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
7887+
}
7888+
/* }}} */
7889+
7890+
/* {{{ Returns false or the name of the extension the constant belongs to */
7891+
ZEND_METHOD(ReflectionConstant, getExtensionName)
7892+
{
7893+
reflection_constant_find_ext(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
7894+
}
7895+
/* }}} */
7896+
78387897
ZEND_METHOD(ReflectionConstant, __toString)
78397898
{
78407899
reflection_object *intern;

ext/reflection/php_reflection.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,5 +914,9 @@ public function getValue(): mixed {}
914914

915915
public function isDeprecated(): bool {}
916916

917+
public function getExtension(): ?ReflectionExtension {}
918+
919+
public function getExtensionName(): string|false {}
920+
917921
public function __toString(): string {}
918922
}

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.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
ReflectionConstant::getExtension()
3+
--EXTENSIONS--
4+
json
5+
--FILE--
6+
<?php
7+
8+
$reflectionConstant = new ReflectionConstant('PHP_VERSION');
9+
var_dump($reflectionConstant->getExtension());
10+
11+
$reflectionConstant = new ReflectionConstant('JSON_ERROR_NONE');
12+
var_dump($reflectionConstant->getExtension());
13+
14+
const CT_CONST = 5;
15+
$reflectionConstant = new ReflectionConstant('CT_CONST');
16+
var_dump($reflectionConstant->getExtension());
17+
18+
define('RT_CONST', 6);
19+
$reflectionConstant = new ReflectionConstant('RT_CONST');
20+
var_dump($reflectionConstant->getExtension());
21+
?>
22+
--EXPECTF--
23+
object(ReflectionExtension)#%d (1) {
24+
["name"]=>
25+
string(4) "Core"
26+
}
27+
object(ReflectionExtension)#%d (1) {
28+
["name"]=>
29+
string(4) "json"
30+
}
31+
NULL
32+
NULL
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
ReflectionConstant::getExtensionName()
3+
--EXTENSIONS--
4+
json
5+
--FILE--
6+
<?php
7+
8+
$reflectionConstant = new ReflectionConstant('PHP_VERSION');
9+
var_dump($reflectionConstant->getExtensionName());
10+
11+
$reflectionConstant = new ReflectionConstant('JSON_ERROR_NONE');
12+
var_dump($reflectionConstant->getExtensionName());
13+
14+
const CT_CONST = 5;
15+
$reflectionConstant = new ReflectionConstant('CT_CONST');
16+
var_dump($reflectionConstant->getExtensionName());
17+
18+
define('RT_CONST', 6);
19+
$reflectionConstant = new ReflectionConstant('RT_CONST');
20+
var_dump($reflectionConstant->getExtensionName());
21+
?>
22+
--EXPECT--
23+
string(4) "Core"
24+
string(4) "json"
25+
bool(false)
26+
bool(false)

0 commit comments

Comments
 (0)