Skip to content

Commit 7bd0f86

Browse files
committed
Implement ReflectionEnumCase::getScalar()
1 parent a05fd38 commit 7bd0f86

File tree

6 files changed

+77
-2
lines changed

6 files changed

+77
-2
lines changed

ext/reflection/php_reflection.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ PHPAPI zend_class_entry *reflection_zend_extension_ptr;
8888
PHPAPI zend_class_entry *reflection_reference_ptr;
8989
PHPAPI zend_class_entry *reflection_attribute_ptr;
9090
PHPAPI zend_class_entry *reflection_enum_ptr;
91+
PHPAPI zend_class_entry *reflection_enum_case_ptr;
9192

9293
/* Exception throwing macro */
9394
#define _DO_THROW(msg) \
@@ -6496,6 +6497,28 @@ ZEND_METHOD(ReflectionEnum, getPrimitiveType)
64966497
}
64976498
}
64986499

6500+
ZEND_METHOD(ReflectionEnumCase, getScalar)
6501+
{
6502+
reflection_object *intern;
6503+
zend_class_constant *ref;
6504+
6505+
if (zend_parse_parameters_none() == FAILURE) {
6506+
RETURN_THROWS();
6507+
}
6508+
GET_REFLECTION_OBJECT_PTR(ref);
6509+
6510+
if (Z_TYPE(ref->value) == IS_CONSTANT_AST) {
6511+
zval_update_constant_ex(&ref->value, ref->ce);
6512+
}
6513+
6514+
zend_string *value_name = zend_string_init("value", strlen("value"), 0);
6515+
zval rv;
6516+
zval *member_p = zend_read_property_ex(intern->ce, Z_OBJ(ref->value), value_name, 1, &rv);
6517+
zend_string_release(value_name);
6518+
6519+
ZVAL_COPY_OR_DUP(return_value, member_p);
6520+
}
6521+
64996522
/* {{{ _reflection_write_property */
65006523
static zval *_reflection_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
65016524
{
@@ -6656,6 +6679,11 @@ PHP_MINIT_FUNCTION(reflection) /* {{{ */
66566679
_reflection_entry.ce_flags |= ZEND_ACC_FINAL;
66576680
reflection_enum_ptr = zend_register_internal_class_ex(&_reflection_entry, reflection_class_ptr);
66586681

6682+
INIT_CLASS_ENTRY(_reflection_entry, "ReflectionEnumCase", class_ReflectionEnumCase_methods);
6683+
reflection_init_class_handlers(&_reflection_entry);
6684+
_reflection_entry.ce_flags |= ZEND_ACC_FINAL;
6685+
reflection_enum_ptr = zend_register_internal_class_ex(&_reflection_entry, reflection_class_constant_ptr);
6686+
66596687
REGISTER_REFLECTION_CLASS_CONST_LONG(attribute, "IS_INSTANCEOF", REFLECTION_ATTRIBUTE_IS_INSTANCEOF);
66606688

66616689
REFLECTION_G(key_initialized) = 0;

ext/reflection/php_reflection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extern PHPAPI zend_class_entry *reflection_zend_extension_ptr;
4444
extern PHPAPI zend_class_entry *reflection_reference_ptr;
4545
extern PHPAPI zend_class_entry *reflection_attribute_ptr;
4646
extern PHPAPI zend_class_entry *reflection_enum_ptr;
47+
extern PHPAPI zend_class_entry *reflection_enum_case_ptr;
4748

4849
PHPAPI void zend_reflection_class_factory(zend_class_entry *ce, zval *object);
4950

ext/reflection/php_reflection.stub.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,3 +677,9 @@ public function hasPrimitiveType() {}
677677
/** @return ReflectionType|null */
678678
public function getPrimitiveType() {}
679679
}
680+
681+
final class ReflectionEnumCase extends ReflectionClassConstant
682+
{
683+
/** @return int|string|null */
684+
public function getScalar() {}
685+
}

ext/reflection/php_reflection_arginfo.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: e17df8167053899d826e74665993b2e0329b18ea */
2+
* Stub hash: c2f68146b8e3a10808080c2a27e7710e819efeab */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Reflection_getModifierNames, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, modifiers, IS_LONG, 0)
@@ -495,6 +495,8 @@ ZEND_END_ARG_INFO()
495495

496496
#define arginfo_class_ReflectionEnum_getPrimitiveType arginfo_class_ReflectionFunctionAbstract_inNamespace
497497

498+
#define arginfo_class_ReflectionEnumCase_getScalar arginfo_class_ReflectionFunctionAbstract_inNamespace
499+
498500

499501
ZEND_METHOD(Reflection, getModifierNames);
500502
ZEND_METHOD(ReflectionClass, __clone);
@@ -699,6 +701,7 @@ ZEND_METHOD(ReflectionAttribute, __clone);
699701
ZEND_METHOD(ReflectionAttribute, __construct);
700702
ZEND_METHOD(ReflectionEnum, hasPrimitiveType);
701703
ZEND_METHOD(ReflectionEnum, getPrimitiveType);
704+
ZEND_METHOD(ReflectionEnumCase, getScalar);
702705

703706

704707
static const zend_function_entry class_ReflectionException_methods[] = {
@@ -1011,3 +1014,9 @@ static const zend_function_entry class_ReflectionEnum_methods[] = {
10111014
ZEND_ME(ReflectionEnum, getPrimitiveType, arginfo_class_ReflectionEnum_getPrimitiveType, ZEND_ACC_PUBLIC)
10121015
ZEND_FE_END
10131016
};
1017+
1018+
1019+
static const zend_function_entry class_ReflectionEnumCase_methods[] = {
1020+
ZEND_ME(ReflectionEnumCase, getScalar, arginfo_class_ReflectionEnumCase_getScalar, ZEND_ACC_PUBLIC)
1021+
ZEND_FE_END
1022+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
ReflectionEnumCase::getScalar()
3+
--FILE--
4+
<?php
5+
6+
enum Enum_ {
7+
case Foo;
8+
}
9+
10+
enum IntEnum: int {
11+
case Foo = 0;
12+
}
13+
14+
enum StringEnum: string {
15+
case Foo = 'Foo';
16+
}
17+
18+
var_dump((new ReflectionEnumCase(Enum_::class, 'Foo'))->getScalar());
19+
var_dump((new ReflectionEnumCase(IntEnum::class, 'Foo'))->getScalar());
20+
var_dump((new ReflectionEnumCase(StringEnum::class, 'Foo'))->getScalar());
21+
22+
?>
23+
--EXPECT--
24+
NULL
25+
int(0)
26+
string(3) "Foo"

ext/reflection/tests/ReflectionExtension_getClasses_basic.phpt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ $ext = new ReflectionExtension('reflection');
88
var_dump($ext->getClasses());
99
?>
1010
--EXPECT--
11-
array(20) {
11+
array(21) {
1212
["ReflectionException"]=>
1313
object(ReflectionClass)#2 (1) {
1414
["name"]=>
@@ -109,4 +109,9 @@ array(20) {
109109
["name"]=>
110110
string(14) "ReflectionEnum"
111111
}
112+
["ReflectionEnumCase"]=>
113+
object(ReflectionClass)#22 (1) {
114+
["name"]=>
115+
string(18) "ReflectionEnumCase"
116+
}
112117
}

0 commit comments

Comments
 (0)