Skip to content

Commit 9c8f154

Browse files
Ayeshiluuu1994
andcommitted
ext/standard: Add get_declared_enums function
Adds a `get_declared_enums()` function that returns a list of declared Enums. This function is similar to the existing `get_declared_(classes|interfaces|traits)` functions. Co-Authored-By: Ilija Tovilo <[email protected]>
1 parent 8853cf3 commit 9c8f154

File tree

7 files changed

+89
-2
lines changed

7 files changed

+89
-2
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ PHP NEWS
1313
casting any non-integer value to a string.
1414
As such, passing invalid types to exit/die may now result in a TypeError
1515
being thrown. (Girgias)
16+
. Added get_declared_enums() function. (Ayesh Karunaratne)
1617

1718
- Hash:
1819
. Fix GH-15384 (Build fails on Alpine / Musl for amd64). (timwolla)

UPGRADING

+2
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,8 @@ PHP 8.4 UPGRADE NOTES
391391
. Added a new RoundingMode enum with clearer naming and improved discoverability
392392
compared to the PHP_ROUND_* constants.
393393
RFC: https://wiki.php.net/rfc/correctly_name_the_rounding_mode_and_make_it_an_enum
394+
. Added get_declared_enums() function that returns a list of declared enums. The
395+
existing get_declared_classes() function continues to include enums.
394396

395397
- SOAP:
396398
. Added support for clark notation for namespaces in class map.

Zend/Optimizer/zend_func_infos.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ static const func_info_t func_infos[] = {
1111
F1("get_declared_classes", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
1212
F1("get_declared_traits", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
1313
F1("get_declared_interfaces", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
14+
F1("get_declared_enums", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
1415
F1("get_defined_functions", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ARRAY),
1516
F1("get_defined_vars", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF),
1617
F1("get_resource_type", MAY_BE_STRING),

Zend/zend_builtin_functions.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ static inline void get_declared_class_impl(INTERNAL_FUNCTION_PARAMETERS, int fla
13811381
ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
13821382
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(EG(class_table), key, zv) {
13831383
ce = Z_PTR_P(zv);
1384-
if ((ce->ce_flags & (ZEND_ACC_LINKED|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT)) == flags
1384+
if ((ce->ce_flags & (flags|ZEND_ACC_LINKED|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT)) == flags
13851385
&& key
13861386
&& ZSTR_VAL(key)[0] != 0) {
13871387
ZEND_HASH_FILL_GROW();
@@ -1419,6 +1419,13 @@ ZEND_FUNCTION(get_declared_interfaces)
14191419
}
14201420
/* }}} */
14211421

1422+
/* {{{ Returns an array of all declared enums. */
1423+
ZEND_FUNCTION(get_declared_enums)
1424+
{
1425+
get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED | ZEND_ACC_ENUM);
1426+
}
1427+
/* }}} */
1428+
14221429
/* {{{ Returns an array of all defined functions */
14231430
ZEND_FUNCTION(get_defined_functions)
14241431
{

Zend/zend_builtin_functions.stub.php

+6
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ function get_declared_traits(): array {}
140140
*/
141141
function get_declared_interfaces(): array {}
142142

143+
/**
144+
* @return array<int, string>
145+
* @refcount 1
146+
*/
147+
function get_declared_enums(): array {}
148+
143149
/**
144150
* @return array<string, array>
145151
* @refcount 1

Zend/zend_builtin_functions_arginfo.h

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
--TEST--
2+
Test get_declared_enums() function
3+
--FILE--
4+
<?php
5+
6+
enum MyUnitEnum {}
7+
enum MyStringBackedEnum: string {
8+
case FOO = 'test';
9+
}
10+
enum MyIntBackedEnum: int {
11+
case FOO = 'test';
12+
}
13+
enum MyEnumImplementation implements Countable {
14+
public function count(): int {
15+
return 0;
16+
}
17+
}
18+
19+
var_dump(get_declared_enums());
20+
21+
foreach (get_declared_enums() as $enum) {
22+
if (!enum_exists($enum)) {
23+
echo "Error: $enum is not a valid enum.\n";
24+
}
25+
}
26+
27+
echo "Ensure all enums are included.\n";
28+
29+
var_dump(in_array('MyUnitEnum', get_declared_enums()));
30+
var_dump(in_array('MyStringBackedEnum', get_declared_enums()));
31+
var_dump(in_array('MyIntBackedEnum', get_declared_enums()));
32+
var_dump(in_array('MyEnumImplementation', get_declared_enums()));
33+
34+
echo "Ensure interfaces are not included.\n";
35+
var_dump(in_array('Throwable', get_declared_enums()));
36+
var_dump(in_array('IntBackedEnum', get_declared_enums()));
37+
38+
echo "Ensure classes are not included.\n";
39+
var_dump(in_array('stdClass', get_declared_enums()));
40+
echo "Ensure enums are included in get_declared_classes().\n";
41+
var_dump(in_array('MyUnitEnum', get_declared_classes()));
42+
var_dump(in_array('MyStringBackedEnum', get_declared_classes()));
43+
var_dump(in_array('MyIntBackedEnum', get_declared_classes()));
44+
var_dump(in_array('MyEnumImplementation', get_declared_classes()));
45+
echo "Done";
46+
?>
47+
--EXPECTF--
48+
array(%d) {
49+
%a
50+
}
51+
Ensure all enums are included.
52+
bool(true)
53+
bool(true)
54+
bool(true)
55+
bool(true)
56+
Ensure interfaces are not included.
57+
bool(false)
58+
bool(false)
59+
Ensure classes are not included.
60+
bool(false)
61+
Ensure enums are included in get_declared_classes().
62+
bool(true)
63+
bool(true)
64+
bool(true)
65+
bool(true)
66+
Done

0 commit comments

Comments
 (0)