Skip to content

Commit 59949e3

Browse files
Add ReflectionConstant::getExtension() and ::getExtensionName()
1 parent 063de1f commit 59949e3

6 files changed

+140
-8
lines changed

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ PHP 8.5 UPGRADE NOTES
9090

9191
- Reflection:
9292
. ReflectionConstant::getFileName() was introduced.
93+
. ReflectionConstant::getExtension() and
94+
ReflectionConstant::getExtensionName() were introduced.
9395

9496
========================================
9597
7. New Classes and Interfaces

ext/reflection/php_reflection.c

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

1322+
/* {{{ reflection_extension_factory_ex */
1323+
static void reflection_extension_factory_ex(zval *object, zend_module_entry *module)
1324+
{
1325+
reflection_instantiate(reflection_extension_ptr, object);
1326+
reflection_object *intern = Z_REFLECTION_P(object);
1327+
intern->ptr = module;
1328+
intern->ref_type = REF_TYPE_OTHER;
1329+
intern->ce = NULL;
1330+
ZVAL_STRING(reflection_prop_name(object), module->name);
1331+
}
1332+
/* }}} */
1333+
13221334
/* {{{ reflection_extension_factory */
13231335
static void reflection_extension_factory(zval *object, const char *name_str)
13241336
{
1325-
reflection_object *intern;
13261337
size_t name_len = strlen(name_str);
13271338
zend_string *lcname;
13281339
struct _zend_module_entry *module;
@@ -1335,12 +1346,7 @@ static void reflection_extension_factory(zval *object, const char *name_str)
13351346
return;
13361347
}
13371348

1338-
reflection_instantiate(reflection_extension_ptr, object);
1339-
intern = Z_REFLECTION_P(object);
1340-
intern->ptr = module;
1341-
intern->ref_type = REF_TYPE_OTHER;
1342-
intern->ce = NULL;
1343-
ZVAL_STRINGL(reflection_prop_name(object), module->name, name_len);
1349+
reflection_extension_factory_ex(object, module);
13441350
}
13451351
/* }}} */
13461352

@@ -7567,6 +7573,59 @@ ZEND_METHOD(ReflectionConstant, getFileName)
75677573
RETURN_FALSE;
75687574
}
75697575

7576+
static void reflection_constant_find_ext(INTERNAL_FUNCTION_PARAMETERS, bool only_name)
7577+
{
7578+
reflection_object *intern;
7579+
zend_constant *const_;
7580+
7581+
ZEND_PARSE_PARAMETERS_NONE();
7582+
7583+
GET_REFLECTION_OBJECT_PTR(const_);
7584+
int module_number = ZEND_CONSTANT_MODULE_NUMBER(const_);
7585+
if (module_number == PHP_USER_CONSTANT) {
7586+
// For user constants, ReflectionConstant::getExtension() returns null,
7587+
// ReflectionConstant::getExtensionName() returns false
7588+
if (only_name) {
7589+
RETURN_FALSE;
7590+
}
7591+
RETURN_NULL();
7592+
}
7593+
zend_module_entry *module;
7594+
ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
7595+
if (module->module_number != module_number) {
7596+
continue;
7597+
}
7598+
if (only_name) {
7599+
RETURN_STRING(module->name);
7600+
}
7601+
reflection_extension_factory_ex(return_value, module);
7602+
return;
7603+
} ZEND_HASH_FOREACH_END();
7604+
7605+
zend_throw_exception_ex(
7606+
reflection_exception_ptr,
7607+
0,
7608+
"Unable to locate extension with module_number %d that provides constant %s",
7609+
module_number,
7610+
ZSTR_VAL(const_->name)
7611+
);
7612+
RETURN_THROWS();
7613+
}
7614+
7615+
/* {{{ Returns NULL or the extension the constant belongs to */
7616+
ZEND_METHOD(ReflectionConstant, getExtension)
7617+
{
7618+
reflection_constant_find_ext(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
7619+
}
7620+
/* }}} */
7621+
7622+
/* {{{ Returns false or the name of the extension the constant belongs to */
7623+
ZEND_METHOD(ReflectionConstant, getExtensionName)
7624+
{
7625+
reflection_constant_find_ext(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
7626+
}
7627+
/* }}} */
7628+
75707629
ZEND_METHOD(ReflectionConstant, __toString)
75717630
{
75727631
reflection_object *intern;

ext/reflection/php_reflection.stub.php

Lines changed: 4 additions & 0 deletions
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

Lines changed: 10 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)