Skip to content

Commit dd68320

Browse files
committed
ReflectionEnum::getCase() throws instead of returning false
1 parent 5501a57 commit dd68320

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

ext/reflection/php_reflection.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6596,8 +6596,13 @@ ZEND_METHOD(ReflectionEnum, getCase)
65966596
GET_REFLECTION_OBJECT_PTR(ce);
65976597

65986598
zend_class_constant *constant = zend_hash_find_ptr(&ce->constants_table, name);
6599-
if (constant == NULL || !(Z_ACCESS_FLAGS(constant->value) & ZEND_CLASS_CONST_IS_CASE)) {
6600-
RETURN_FALSE;
6599+
if (constant == NULL) {
6600+
zend_throw_exception_ex(reflection_exception_ptr, 0, "Case %s::%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
6601+
RETURN_THROWS();
6602+
}
6603+
if (!(Z_ACCESS_FLAGS(constant->value) & ZEND_CLASS_CONST_IS_CASE)) {
6604+
zend_throw_exception_ex(reflection_exception_ptr, 0, "%s::%s is not a case", ZSTR_VAL(ce->name), ZSTR_VAL(name));
6605+
RETURN_THROWS();
66016606
}
66026607

66036608
reflection_enum_case_factory(ce, name, constant, return_value);

ext/reflection/php_reflection.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ public function __construct(object|string $objectOrClass) {}
694694

695695
public function hasCase(string $name): bool {}
696696

697-
public function getCase(string $name): ReflectionEnumUnitCase|false {}
697+
public function getCase(string $name): ReflectionEnumUnitCase {}
698698

699699
/** @return ReflectionEnumUnitCase[] */
700700
public function getCases(): array {}

ext/reflection/php_reflection_arginfo.h

Lines changed: 2 additions & 2 deletions
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: b4ec9ccf1844750cdc959918192efb3d22b257ce */
2+
* Stub hash: 3594ec0b0c3ed7266223be9c6b426aac56e3aabe */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Reflection_getModifierNames, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, modifiers, IS_LONG, 0)
@@ -499,7 +499,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_ReflectionEnum_hasCase, 0,
499499
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
500500
ZEND_END_ARG_INFO()
501501

502-
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_ReflectionEnum_getCase, 0, 1, ReflectionEnumUnitCase, MAY_BE_FALSE)
502+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_ReflectionEnum_getCase, 0, 1, ReflectionEnumUnitCase, 0)
503503
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
504504
ZEND_END_ARG_INFO()
505505

ext/reflection/tests/ReflectionEnum_getCase.phpt

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@ enum IntEnum: int {
1313
const Bar = self::Foo;
1414
}
1515

16-
$reflectionEnum = new ReflectionEnum(Enum_::class);
17-
var_dump($reflectionEnum->getCase('Foo'));
18-
var_dump($reflectionEnum->getCase('Bar'));
19-
var_dump($reflectionEnum->getCase('Baz'));
16+
function test(string $enumName, string $caseName) {
17+
try {
18+
$reflectionEnum = new ReflectionEnum($enumName);
19+
var_dump($reflectionEnum->getCase($caseName));
20+
} catch (Throwable $e) {
21+
echo get_class($e) . ': ' . $e->getMessage() . "\n";
22+
}
23+
}
24+
25+
test(Enum_::class, 'Foo');
26+
test(Enum_::class, 'Bar');
27+
test(Enum_::class, 'Baz');
2028

21-
$reflectionEnum = new ReflectionEnum(IntEnum::class);
22-
var_dump($reflectionEnum->getCase('Foo'));
23-
var_dump($reflectionEnum->getCase('Bar'));
24-
var_dump($reflectionEnum->getCase('Baz'));
29+
test(IntEnum::class, 'Foo');
30+
test(IntEnum::class, 'Bar');
31+
test(IntEnum::class, 'Baz');
2532

2633
?>
2734
--EXPECT--
@@ -31,13 +38,13 @@ object(ReflectionEnumUnitCase)#2 (2) {
3138
["class"]=>
3239
string(5) "Enum_"
3340
}
34-
bool(false)
35-
bool(false)
36-
object(ReflectionEnumBackedCase)#1 (2) {
41+
ReflectionException: Enum_::Bar is not a case
42+
ReflectionException: Case Enum_::Baz does not exist
43+
object(ReflectionEnumBackedCase)#2 (2) {
3744
["name"]=>
3845
string(3) "Foo"
3946
["class"]=>
4047
string(7) "IntEnum"
4148
}
42-
bool(false)
43-
bool(false)
49+
ReflectionException: IntEnum::Bar is not a case
50+
ReflectionException: Case IntEnum::Baz does not exist

0 commit comments

Comments
 (0)