Skip to content

Commit 18ead6a

Browse files
committed
Add getNamespaceName() and getShortName()
1 parent c8abc66 commit 18ead6a

File tree

4 files changed

+88
-5
lines changed

4 files changed

+88
-5
lines changed

ext/reflection/php_reflection.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7261,6 +7261,47 @@ ZEND_METHOD(ReflectionConstant, getName)
72617261
RETURN_STR_COPY(const_->name);
72627262
}
72637263

7264+
ZEND_METHOD(ReflectionConstant, getNamespaceName)
7265+
{
7266+
reflection_object *intern;
7267+
zend_constant *const_;
7268+
7269+
if (zend_parse_parameters_none() == FAILURE) {
7270+
RETURN_THROWS();
7271+
}
7272+
7273+
GET_REFLECTION_OBJECT_PTR(const_);
7274+
7275+
const char *backslash = zend_memrchr(ZSTR_VAL(const_->name), '\\', ZSTR_LEN(const_->name));
7276+
if (backslash) {
7277+
size_t length = backslash - ZSTR_VAL(const_->name);
7278+
RETURN_STRINGL(ZSTR_VAL(const_->name), length);
7279+
} else {
7280+
RETURN_EMPTY_STRING();
7281+
}
7282+
}
7283+
7284+
ZEND_METHOD(ReflectionConstant, getShortName)
7285+
{
7286+
reflection_object *intern;
7287+
zend_constant *const_;
7288+
7289+
if (zend_parse_parameters_none() == FAILURE) {
7290+
RETURN_THROWS();
7291+
}
7292+
7293+
GET_REFLECTION_OBJECT_PTR(const_);
7294+
7295+
const char *backslash = zend_memrchr(ZSTR_VAL(const_->name), '\\', ZSTR_LEN(const_->name));
7296+
if (backslash) {
7297+
size_t prefix = backslash - ZSTR_VAL(const_->name) + 1;
7298+
size_t length = ZSTR_LEN(const_->name) - prefix;
7299+
RETURN_STRINGL(ZSTR_VAL(const_->name) + prefix, length);
7300+
} else {
7301+
RETURN_STR_COPY(const_->name);
7302+
}
7303+
}
7304+
72647305
ZEND_METHOD(ReflectionConstant, getValue)
72657306
{
72667307
reflection_object *intern;

ext/reflection/php_reflection.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,10 @@ public function __construct(string $name) {}
842842

843843
public function getName(): string {}
844844

845+
public function getNamespaceName(): string {}
846+
847+
public function getShortName(): string {}
848+
845849
public function getValue(): mixed {}
846850

847851
public function isDeprecated(): bool {}

ext/reflection/php_reflection_arginfo.h

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/reflection/tests/ReflectionConstant_ns.phpt

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,37 @@ ReflectionConstant with namespace
33
--FILE--
44
<?php
55

6-
namespace Foo;
6+
namespace Foo {
7+
const C = 42;
8+
}
79

8-
const C = 42;
10+
namespace {
11+
const C = 43;
912

10-
var_dump(new \ReflectionConstant('Foo\\C'));
11-
var_dump(new \ReflectionConstant('\\Foo\\C'));
13+
var_dump(new \ReflectionConstant('C'));
14+
var_dump(new \ReflectionConstant('\\C'));
15+
var_dump(new \ReflectionConstant('Foo\\C'));
16+
var_dump(new \ReflectionConstant('\\Foo\\C'));
17+
var_dump((new \ReflectionConstant('C'))->getNamespaceName());
18+
var_dump((new \ReflectionConstant('\\C'))->getNamespaceName());
19+
var_dump((new \ReflectionConstant('Foo\\C'))->getNamespaceName());
20+
var_dump((new \ReflectionConstant('\\Foo\\C'))->getNamespaceName());
21+
var_dump((new \ReflectionConstant('C'))->getShortName());
22+
var_dump((new \ReflectionConstant('\\C'))->getShortName());
23+
var_dump((new \ReflectionConstant('Foo\\C'))->getShortName());
24+
var_dump((new \ReflectionConstant('\\Foo\\C'))->getShortName());
25+
}
1226

1327
?>
1428
--EXPECT--
29+
object(ReflectionConstant)#1 (1) {
30+
["name"]=>
31+
string(1) "C"
32+
}
33+
object(ReflectionConstant)#1 (1) {
34+
["name"]=>
35+
string(2) "\C"
36+
}
1537
object(ReflectionConstant)#1 (1) {
1638
["name"]=>
1739
string(5) "Foo\C"
@@ -20,3 +42,11 @@ object(ReflectionConstant)#1 (1) {
2042
["name"]=>
2143
string(6) "\Foo\C"
2244
}
45+
string(0) ""
46+
string(0) ""
47+
string(3) "Foo"
48+
string(3) "Foo"
49+
string(1) "C"
50+
string(1) "C"
51+
string(1) "C"
52+
string(1) "C"

0 commit comments

Comments
 (0)