Skip to content

Commit 2039638

Browse files
committed
Implement ReflectionConstant::getType()
Fixes GH-16553.
1 parent 207883e commit 2039638

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

ext/reflection/php_reflection.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7809,6 +7809,26 @@ ZEND_METHOD(ReflectionConstant, getShortName)
78097809
}
78107810
}
78117811

7812+
ZEND_METHOD(ReflectionConstant, getType)
7813+
{
7814+
reflection_object *intern;
7815+
zend_constant *const_;
7816+
7817+
if (zend_parse_parameters_none() == FAILURE) {
7818+
RETURN_THROWS();
7819+
}
7820+
7821+
GET_REFLECTION_OBJECT_PTR(const_);
7822+
7823+
zend_type fake_type;
7824+
if (Z_TYPE(const_->value) == IS_OBJECT) {
7825+
fake_type = (zend_type) ZEND_TYPE_INIT_CLASS(Z_OBJCE(const_->value)->name, false, 0);
7826+
} else {
7827+
fake_type = (zend_type) ZEND_TYPE_INIT_CODE(Z_TYPE(const_->value), false, 0);
7828+
}
7829+
reflection_type_factory(fake_type, return_value, 1);
7830+
}
7831+
78127832
ZEND_METHOD(ReflectionConstant, getValue)
78137833
{
78147834
reflection_object *intern;

ext/reflection/php_reflection.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,8 @@ public function getNamespaceName(): string {}
910910

911911
public function getShortName(): string {}
912912

913+
public function getType(): ?ReflectionType {}
914+
913915
public function getValue(): mixed {}
914916

915917
public function isDeprecated(): bool {}

ext/reflection/php_reflection_arginfo.h

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
ReflectionConstant::getType()
3+
--FILE--
4+
<?php
5+
6+
const C_OBJ = new stdClass;
7+
const C_NULL = null;
8+
const C_BOOL = true;
9+
const C_STRING = "string";
10+
const C_ARRAY = ["a", "b", "c"];
11+
const C_INT = 1234;
12+
const C_FLOAT = 0.1234;
13+
const C_OBJ2 = C_OBJ;
14+
15+
$list = [
16+
"C_OBJ",
17+
"C_NULL",
18+
"C_BOOL",
19+
"C_STRING",
20+
"C_ARRAY",
21+
"C_INT",
22+
"C_FLOAT",
23+
"C_OBJ2",
24+
];
25+
26+
foreach ($list as $name) {
27+
$rc = new ReflectionConstant($name);
28+
echo $rc->getType(), "\n";
29+
}
30+
31+
?>
32+
--EXPECT--
33+
stdClass
34+
null
35+
true
36+
string
37+
array
38+
int
39+
float
40+
stdClass

0 commit comments

Comments
 (0)