Skip to content

Commit badf89a

Browse files
committed
Implement ReflectionEnum::getCases()
1 parent 80747d0 commit badf89a

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

ext/reflection/php_reflection.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,21 @@ static void reflection_class_constant_factory(zend_string *name_str, zend_class_
14321432
}
14331433
/* }}} */
14341434

1435+
static void reflection_enum_case_factory(zend_string *name_str, zend_class_constant *constant, zval *object)
1436+
{
1437+
reflection_object *intern;
1438+
1439+
reflection_instantiate(reflection_enum_case_ptr, object);
1440+
intern = Z_REFLECTION_P(object);
1441+
intern->ptr = constant;
1442+
intern->ref_type = REF_TYPE_CLASS_CONSTANT;
1443+
intern->ce = constant->ce;
1444+
intern->ignore_visibility = 0;
1445+
1446+
ZVAL_STR_COPY(reflection_prop_name(object), name_str);
1447+
ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
1448+
}
1449+
14351450
static int get_parameter_default(zval *result, parameter_reference *param) {
14361451
if (param->fptr->type == ZEND_INTERNAL_FUNCTION) {
14371452
if (param->fptr->common.fn_flags & ZEND_ACC_USER_ARG_INFO) {
@@ -6495,6 +6510,29 @@ ZEND_METHOD(ReflectionEnum, hasCase)
64956510
RETURN_BOOL(class_const->attr & ZEND_CLASS_CONST_IS_CASE);
64966511
}
64976512

6513+
ZEND_METHOD(ReflectionEnum, getCases)
6514+
{
6515+
reflection_object *intern;
6516+
zend_class_entry *ce;
6517+
zend_string *name;
6518+
zend_class_constant *constant;
6519+
6520+
if (zend_parse_parameters_none() == FAILURE) {
6521+
RETURN_THROWS();
6522+
}
6523+
6524+
GET_REFLECTION_OBJECT_PTR(ce);
6525+
6526+
array_init(return_value);
6527+
ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, name, constant) {
6528+
if (constant->attr & ZEND_CLASS_CONST_IS_CASE) {
6529+
zval class_const;
6530+
reflection_enum_case_factory(name, constant, &class_const);
6531+
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &class_const);
6532+
}
6533+
} ZEND_HASH_FOREACH_END();
6534+
}
6535+
64986536
ZEND_METHOD(ReflectionEnum, hasPrimitiveType)
64996537
{
65006538
reflection_object *intern;
@@ -6712,7 +6750,7 @@ PHP_MINIT_FUNCTION(reflection) /* {{{ */
67126750
INIT_CLASS_ENTRY(_reflection_entry, "ReflectionEnumCase", class_ReflectionEnumCase_methods);
67136751
reflection_init_class_handlers(&_reflection_entry);
67146752
_reflection_entry.ce_flags |= ZEND_ACC_FINAL;
6715-
reflection_enum_ptr = zend_register_internal_class_ex(&_reflection_entry, reflection_class_constant_ptr);
6753+
reflection_enum_case_ptr = zend_register_internal_class_ex(&_reflection_entry, reflection_class_constant_ptr);
67166754

67176755
REGISTER_REFLECTION_CLASS_CONST_LONG(attribute, "IS_INSTANCEOF", REFLECTION_ATTRIBUTE_IS_INSTANCEOF);
67186756

ext/reflection/php_reflection.stub.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,10 @@ private function __construct() {}
675675
final class ReflectionEnum extends ReflectionClass
676676
{
677677
/** @return bool */
678-
public function hasCase(string $name);
678+
public function hasCase(string $name) {}
679+
680+
/** @return ReflectionEnumCase[] */
681+
public function getCases() {}
679682

680683
/** @return bool */
681684
public function hasPrimitiveType() {}

ext/reflection/php_reflection_arginfo.h

Lines changed: 5 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: 07fee3b12c632e12a377bac90ee662a752e9e65e */
2+
* Stub hash: 48c4f2eaad51d8a07694748e281b93e6309fe205 */
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_hasCase arginfo_class_ReflectionClass_hasMethod
497497

498+
#define arginfo_class_ReflectionEnum_getCases arginfo_class_ReflectionFunctionAbstract_inNamespace
499+
498500
#define arginfo_class_ReflectionEnum_hasPrimitiveType arginfo_class_ReflectionFunctionAbstract_inNamespace
499501

500502
#define arginfo_class_ReflectionEnum_getPrimitiveType arginfo_class_ReflectionFunctionAbstract_inNamespace
@@ -705,6 +707,7 @@ ZEND_METHOD(ReflectionAttribute, newInstance);
705707
ZEND_METHOD(ReflectionAttribute, __clone);
706708
ZEND_METHOD(ReflectionAttribute, __construct);
707709
ZEND_METHOD(ReflectionEnum, hasCase);
710+
ZEND_METHOD(ReflectionEnum, getCases);
708711
ZEND_METHOD(ReflectionEnum, hasPrimitiveType);
709712
ZEND_METHOD(ReflectionEnum, getPrimitiveType);
710713
ZEND_METHOD(ReflectionEnumCase, getScalar);
@@ -1018,6 +1021,7 @@ static const zend_function_entry class_ReflectionAttribute_methods[] = {
10181021

10191022
static const zend_function_entry class_ReflectionEnum_methods[] = {
10201023
ZEND_ME(ReflectionEnum, hasCase, arginfo_class_ReflectionEnum_hasCase, ZEND_ACC_PUBLIC)
1024+
ZEND_ME(ReflectionEnum, getCases, arginfo_class_ReflectionEnum_getCases, ZEND_ACC_PUBLIC)
10211025
ZEND_ME(ReflectionEnum, hasPrimitiveType, arginfo_class_ReflectionEnum_hasPrimitiveType, ZEND_ACC_PUBLIC)
10221026
ZEND_ME(ReflectionEnum, getPrimitiveType, arginfo_class_ReflectionEnum_getPrimitiveType, ZEND_ACC_PUBLIC)
10231027
ZEND_FE_END
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
ReflectionEnum::getCases()
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
case Baz;
9+
const Qux = self::Bar;
10+
}
11+
12+
var_dump((new ReflectionEnum(Foo::class))->getCases());
13+
14+
?>
15+
--EXPECT--
16+
array(2) {
17+
[0]=>
18+
object(ReflectionEnumCase)#2 (2) {
19+
["name"]=>
20+
string(3) "Bar"
21+
["class"]=>
22+
string(3) "Foo"
23+
}
24+
[1]=>
25+
object(ReflectionEnumCase)#3 (2) {
26+
["name"]=>
27+
string(3) "Baz"
28+
["class"]=>
29+
string(3) "Foo"
30+
}
31+
}

0 commit comments

Comments
 (0)