Skip to content

Commit 1e842af

Browse files
committed
Implement ReflectionEnum::getCase()
1 parent badf89a commit 1e842af

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

ext/reflection/php_reflection.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6510,6 +6510,29 @@ ZEND_METHOD(ReflectionEnum, hasCase)
65106510
RETURN_BOOL(class_const->attr & ZEND_CLASS_CONST_IS_CASE);
65116511
}
65126512

6513+
ZEND_METHOD(ReflectionEnum, getCase)
6514+
{
6515+
reflection_object *intern;
6516+
zend_class_entry *ce;
6517+
zend_string *name;
6518+
6519+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
6520+
RETURN_THROWS();
6521+
}
6522+
6523+
GET_REFLECTION_OBJECT_PTR(ce);
6524+
6525+
zend_class_constant *constant = zend_hash_find_ptr(&ce->constants_table, name);
6526+
if (
6527+
constant == NULL
6528+
|| constant->attr & ZEND_CLASS_CONST_IS_CASE
6529+
) {
6530+
RETURN_FALSE;
6531+
}
6532+
6533+
reflection_enum_case_factory(name, constant, return_value);
6534+
}
6535+
65136536
ZEND_METHOD(ReflectionEnum, getCases)
65146537
{
65156538
reflection_object *intern;

ext/reflection/php_reflection.stub.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,9 @@ final class ReflectionEnum extends ReflectionClass
677677
/** @return bool */
678678
public function hasCase(string $name) {}
679679

680+
/** @return ReflectionEnumCase|false */
681+
public function getCase(string $name) {}
682+
680683
/** @return ReflectionEnumCase[] */
681684
public function getCases() {}
682685

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: 48c4f2eaad51d8a07694748e281b93e6309fe205 */
2+
* Stub hash: 1391f026298e3bfb0e2c5eebcbb7acd8b8c233b3 */
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_getCase arginfo_class_ReflectionClass_hasMethod
499+
498500
#define arginfo_class_ReflectionEnum_getCases arginfo_class_ReflectionFunctionAbstract_inNamespace
499501

500502
#define arginfo_class_ReflectionEnum_hasPrimitiveType arginfo_class_ReflectionFunctionAbstract_inNamespace
@@ -707,6 +709,7 @@ ZEND_METHOD(ReflectionAttribute, newInstance);
707709
ZEND_METHOD(ReflectionAttribute, __clone);
708710
ZEND_METHOD(ReflectionAttribute, __construct);
709711
ZEND_METHOD(ReflectionEnum, hasCase);
712+
ZEND_METHOD(ReflectionEnum, getCase);
710713
ZEND_METHOD(ReflectionEnum, getCases);
711714
ZEND_METHOD(ReflectionEnum, hasPrimitiveType);
712715
ZEND_METHOD(ReflectionEnum, getPrimitiveType);
@@ -1021,6 +1024,7 @@ static const zend_function_entry class_ReflectionAttribute_methods[] = {
10211024

10221025
static const zend_function_entry class_ReflectionEnum_methods[] = {
10231026
ZEND_ME(ReflectionEnum, hasCase, arginfo_class_ReflectionEnum_hasCase, ZEND_ACC_PUBLIC)
1027+
ZEND_ME(ReflectionEnum, getCase, arginfo_class_ReflectionEnum_getCase, ZEND_ACC_PUBLIC)
10241028
ZEND_ME(ReflectionEnum, getCases, arginfo_class_ReflectionEnum_getCases, ZEND_ACC_PUBLIC)
10251029
ZEND_ME(ReflectionEnum, hasPrimitiveType, arginfo_class_ReflectionEnum_hasPrimitiveType, ZEND_ACC_PUBLIC)
10261030
ZEND_ME(ReflectionEnum, getPrimitiveType, arginfo_class_ReflectionEnum_getPrimitiveType, ZEND_ACC_PUBLIC)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
ReflectionEnum::getCases()
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
const Baz = self::Bar;
9+
}
10+
11+
$reflectionEnum = new ReflectionEnum(Foo::class);
12+
13+
var_dump($reflectionEnum->getCase('Bar'));
14+
var_dump($reflectionEnum->getCase('Baz'));
15+
var_dump($reflectionEnum->getCase('Qux'));
16+
17+
?>
18+
--EXPECT--
19+
bool(false)
20+
object(ReflectionEnumCase)#2 (2) {
21+
["name"]=>
22+
string(3) "Baz"
23+
["class"]=>
24+
string(3) "Foo"
25+
}
26+
bool(false)

0 commit comments

Comments
 (0)