Skip to content

Commit 10f1f92

Browse files
Add ReflectionConstant::getExtension() and ::getExtensionName() (php#16603)
1 parent f8f9ac8 commit 10f1f92

6 files changed

+140
-8
lines changed

UPGRADING

+2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ PHP 8.5 UPGRADE NOTES
102102

103103
- Reflection:
104104
. ReflectionConstant::getFileName() was introduced.
105+
. ReflectionConstant::getExtension() and
106+
ReflectionConstant::getExtensionName() were introduced.
105107

106108
========================================
107109
7. New Classes and Interfaces

ext/reflection/php_reflection.c

+66-7
Original file line numberDiff line numberDiff line change
@@ -1325,10 +1325,21 @@ PHPAPI void zend_reflection_class_factory(zend_class_entry *ce, zval *object)
13251325
}
13261326
/* }}} */
13271327

1328+
/* {{{ reflection_extension_factory_ex */
1329+
static void reflection_extension_factory_ex(zval *object, zend_module_entry *module)
1330+
{
1331+
reflection_instantiate(reflection_extension_ptr, object);
1332+
reflection_object *intern = Z_REFLECTION_P(object);
1333+
intern->ptr = module;
1334+
intern->ref_type = REF_TYPE_OTHER;
1335+
intern->ce = NULL;
1336+
ZVAL_STRING(reflection_prop_name(object), module->name);
1337+
}
1338+
/* }}} */
1339+
13281340
/* {{{ reflection_extension_factory */
13291341
static void reflection_extension_factory(zval *object, const char *name_str)
13301342
{
1331-
reflection_object *intern;
13321343
size_t name_len = strlen(name_str);
13331344
zend_string *lcname;
13341345
struct _zend_module_entry *module;
@@ -1341,12 +1352,7 @@ static void reflection_extension_factory(zval *object, const char *name_str)
13411352
return;
13421353
}
13431354

1344-
reflection_instantiate(reflection_extension_ptr, object);
1345-
intern = Z_REFLECTION_P(object);
1346-
intern->ptr = module;
1347-
intern->ref_type = REF_TYPE_OTHER;
1348-
intern->ce = NULL;
1349-
ZVAL_STRINGL(reflection_prop_name(object), module->name, name_len);
1355+
reflection_extension_factory_ex(object, module);
13501356
}
13511357
/* }}} */
13521358

@@ -7592,6 +7598,59 @@ ZEND_METHOD(ReflectionConstant, getFileName)
75927598
RETURN_FALSE;
75937599
}
75947600

7601+
static void reflection_constant_find_ext(INTERNAL_FUNCTION_PARAMETERS, bool only_name)
7602+
{
7603+
reflection_object *intern;
7604+
zend_constant *const_;
7605+
7606+
ZEND_PARSE_PARAMETERS_NONE();
7607+
7608+
GET_REFLECTION_OBJECT_PTR(const_);
7609+
int module_number = ZEND_CONSTANT_MODULE_NUMBER(const_);
7610+
if (module_number == PHP_USER_CONSTANT) {
7611+
// For user constants, ReflectionConstant::getExtension() returns null,
7612+
// ReflectionConstant::getExtensionName() returns false
7613+
if (only_name) {
7614+
RETURN_FALSE;
7615+
}
7616+
RETURN_NULL();
7617+
}
7618+
zend_module_entry *module;
7619+
ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
7620+
if (module->module_number != module_number) {
7621+
continue;
7622+
}
7623+
if (only_name) {
7624+
RETURN_STRING(module->name);
7625+
}
7626+
reflection_extension_factory_ex(return_value, module);
7627+
return;
7628+
} ZEND_HASH_FOREACH_END();
7629+
7630+
zend_throw_exception_ex(
7631+
reflection_exception_ptr,
7632+
0,
7633+
"Unable to locate extension with module_number %d that provides constant %s",
7634+
module_number,
7635+
ZSTR_VAL(const_->name)
7636+
);
7637+
RETURN_THROWS();
7638+
}
7639+
7640+
/* {{{ Returns NULL or the extension the constant belongs to */
7641+
ZEND_METHOD(ReflectionConstant, getExtension)
7642+
{
7643+
reflection_constant_find_ext(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
7644+
}
7645+
/* }}} */
7646+
7647+
/* {{{ Returns false or the name of the extension the constant belongs to */
7648+
ZEND_METHOD(ReflectionConstant, getExtensionName)
7649+
{
7650+
reflection_constant_find_ext(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
7651+
}
7652+
/* }}} */
7653+
75957654
ZEND_METHOD(ReflectionConstant, __toString)
75967655
{
75977656
reflection_object *intern;

ext/reflection/php_reflection.stub.php

+4
Original file line numberDiff line numberDiff line change
@@ -918,5 +918,9 @@ public function isDeprecated(): bool {}
918918

919919
public function getFileName(): string|false {}
920920

921+
public function getExtension(): ?ReflectionExtension {}
922+
923+
public function getExtensionName(): string|false {}
924+
921925
public function __toString(): string {}
922926
}

ext/reflection/php_reflection_arginfo.h

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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
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)