Skip to content

Add ReflectionConstant::getExtension() and ::getExtensionName() #16603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ PHP 8.5 UPGRADE NOTES

- Reflection:
. ReflectionConstant::getFileName() was introduced.
. ReflectionConstant::getExtension() and
ReflectionConstant::getExtensionName() were introduced.

========================================
7. New Classes and Interfaces
Expand Down
73 changes: 66 additions & 7 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1319,10 +1319,21 @@ PHPAPI void zend_reflection_class_factory(zend_class_entry *ce, zval *object)
}
/* }}} */

/* {{{ reflection_extension_factory_ex */
static void reflection_extension_factory_ex(zval *object, zend_module_entry *module)
{
reflection_instantiate(reflection_extension_ptr, object);
reflection_object *intern = Z_REFLECTION_P(object);
intern->ptr = module;
intern->ref_type = REF_TYPE_OTHER;
intern->ce = NULL;
ZVAL_STRING(reflection_prop_name(object), module->name);
}
/* }}} */

/* {{{ reflection_extension_factory */
static void reflection_extension_factory(zval *object, const char *name_str)
{
reflection_object *intern;
size_t name_len = strlen(name_str);
zend_string *lcname;
struct _zend_module_entry *module;
Expand All @@ -1335,12 +1346,7 @@ static void reflection_extension_factory(zval *object, const char *name_str)
return;
}

reflection_instantiate(reflection_extension_ptr, object);
intern = Z_REFLECTION_P(object);
intern->ptr = module;
intern->ref_type = REF_TYPE_OTHER;
intern->ce = NULL;
ZVAL_STRINGL(reflection_prop_name(object), module->name, name_len);
reflection_extension_factory_ex(object, module);
}
/* }}} */

Expand Down Expand Up @@ -7567,6 +7573,59 @@ ZEND_METHOD(ReflectionConstant, getFileName)
RETURN_FALSE;
}

static void reflection_constant_find_ext(INTERNAL_FUNCTION_PARAMETERS, bool only_name)
{
reflection_object *intern;
zend_constant *const_;

ZEND_PARSE_PARAMETERS_NONE();

GET_REFLECTION_OBJECT_PTR(const_);
int module_number = ZEND_CONSTANT_MODULE_NUMBER(const_);
if (module_number == PHP_USER_CONSTANT) {
// For user constants, ReflectionConstant::getExtension() returns null,
// ReflectionConstant::getExtensionName() returns false
if (only_name) {
RETURN_FALSE;
}
RETURN_NULL();
}
zend_module_entry *module;
ZEND_HASH_MAP_FOREACH_PTR(&module_registry, module) {
if (module->module_number != module_number) {
continue;
}
if (only_name) {
RETURN_STRING(module->name);
}
reflection_extension_factory_ex(return_value, module);
return;
} ZEND_HASH_FOREACH_END();

zend_throw_exception_ex(
reflection_exception_ptr,
0,
"Unable to locate extension with module_number %d that provides constant %s",
module_number,
ZSTR_VAL(const_->name)
);
RETURN_THROWS();
}

/* {{{ Returns NULL or the extension the constant belongs to */
ZEND_METHOD(ReflectionConstant, getExtension)
{
reflection_constant_find_ext(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
}
/* }}} */

/* {{{ Returns false or the name of the extension the constant belongs to */
ZEND_METHOD(ReflectionConstant, getExtensionName)
{
reflection_constant_find_ext(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
}
/* }}} */

ZEND_METHOD(ReflectionConstant, __toString)
{
reflection_object *intern;
Expand Down
4 changes: 4 additions & 0 deletions ext/reflection/php_reflection.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -918,5 +918,9 @@ public function isDeprecated(): bool {}

public function getFileName(): string|false {}

public function getExtension(): ?ReflectionExtension {}

public function getExtensionName(): string|false {}

public function __toString(): string {}
}
11 changes: 10 additions & 1 deletion ext/reflection/php_reflection_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions ext/reflection/tests/ReflectionConstant_getExtension.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
ReflectionConstant::getExtension()
--EXTENSIONS--
json
--FILE--
<?php

$reflectionConstant = new ReflectionConstant('PHP_VERSION');
var_dump($reflectionConstant->getExtension());

$reflectionConstant = new ReflectionConstant('JSON_ERROR_NONE');
var_dump($reflectionConstant->getExtension());

const CT_CONST = 5;
$reflectionConstant = new ReflectionConstant('CT_CONST');
var_dump($reflectionConstant->getExtension());

define('RT_CONST', 6);
$reflectionConstant = new ReflectionConstant('RT_CONST');
var_dump($reflectionConstant->getExtension());
?>
--EXPECTF--
object(ReflectionExtension)#%d (1) {
["name"]=>
string(4) "Core"
}
object(ReflectionExtension)#%d (1) {
["name"]=>
string(4) "json"
}
NULL
NULL
26 changes: 26 additions & 0 deletions ext/reflection/tests/ReflectionConstant_getExtensionName.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
ReflectionConstant::getExtensionName()
--EXTENSIONS--
json
--FILE--
<?php

$reflectionConstant = new ReflectionConstant('PHP_VERSION');
var_dump($reflectionConstant->getExtensionName());

$reflectionConstant = new ReflectionConstant('JSON_ERROR_NONE');
var_dump($reflectionConstant->getExtensionName());

const CT_CONST = 5;
$reflectionConstant = new ReflectionConstant('CT_CONST');
var_dump($reflectionConstant->getExtensionName());

define('RT_CONST', 6);
$reflectionConstant = new ReflectionConstant('RT_CONST');
var_dump($reflectionConstant->getExtensionName());
?>
--EXPECT--
string(4) "Core"
string(4) "json"
bool(false)
bool(false)
Loading