Skip to content

Commit 7192914

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 602a4ac commit 7192914

File tree

7 files changed

+102
-2
lines changed

7 files changed

+102
-2
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ PHP NEWS
3838
casting any non-integer value to a string.
3939
As such, passing invalid types to exit/die may now result in a TypeError
4040
being thrown. (Girgias)
41+
. Added get_declared_enums() function. (Ayesh Karunaratne)
4142

4243
- CURL:
4344
. Added CURLOPT_TCP_KEEPCNT to set the number of probes to send before

UPGRADING

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

398400
- SOAP:
399401
. 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
@@ -1382,7 +1382,7 @@ static inline void get_declared_class_impl(INTERNAL_FUNCTION_PARAMETERS, int fla
13821382
ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
13831383
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(EG(class_table), key, zv) {
13841384
ce = Z_PTR_P(zv);
1385-
if ((ce->ce_flags & (ZEND_ACC_LINKED|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT)) == flags
1385+
if ((ce->ce_flags & (flags|ZEND_ACC_LINKED|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT)) == flags
13861386
&& key
13871387
&& ZSTR_VAL(key)[0] != 0) {
13881388
ZEND_HASH_FILL_GROW();
@@ -1420,6 +1420,13 @@ ZEND_FUNCTION(get_declared_interfaces)
14201420
}
14211421
/* }}} */
14221422

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

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,79 @@
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 = 1;
12+
}
13+
enum MyEnumImplementation implements Countable {
14+
use TraitWithNoProperties;
15+
public function count(): int {
16+
return 0;
17+
}
18+
}
19+
20+
trait TraitWithNoProperties {}
21+
22+
$enumsList = get_declared_enums();
23+
$classesList = get_declared_classes();
24+
var_dump($enumsList);
25+
26+
foreach ($enumsList as $enum) {
27+
if (!enum_exists($enum)) {
28+
echo "Error: $enum is not a valid enum.\n";
29+
}
30+
}
31+
32+
echo "Ensure all enums are included.\n";
33+
34+
var_dump(in_array('MyUnitEnum', $enumsList));
35+
var_dump(in_array('MyStringBackedEnum', $enumsList));
36+
var_dump(in_array('MyIntBackedEnum', $enumsList));
37+
var_dump(in_array('MyEnumImplementation', $enumsList));
38+
39+
echo "Ensure interfaces are not included.\n";
40+
var_dump(!in_array('Throwable', $enumsList));
41+
var_dump(!in_array('IntBackedEnum', $enumsList));
42+
43+
echo "Ensure traits are not included.\n";
44+
var_dump(!in_array('TraitWithNoProperties', $enumsList));
45+
46+
echo "Ensure classes are not included.\n";
47+
var_dump(!in_array('stdClass', $enumsList));
48+
var_dump(!in_array('Exception', $enumsList));
49+
50+
echo "Ensure enums are included in get_declared_classes().\n";
51+
var_dump(in_array('MyUnitEnum', $classesList));
52+
var_dump(in_array('MyStringBackedEnum', $classesList));
53+
var_dump(in_array('MyIntBackedEnum', $classesList));
54+
var_dump(in_array('MyEnumImplementation', $classesList));
55+
echo "Done";
56+
?>
57+
--EXPECTF--
58+
array(%d) {
59+
%a
60+
}
61+
Ensure all enums are included.
62+
bool(true)
63+
bool(true)
64+
bool(true)
65+
bool(true)
66+
Ensure interfaces are not included.
67+
bool(true)
68+
bool(true)
69+
Ensure traits are not included.
70+
bool(true)
71+
Ensure classes are not included.
72+
bool(true)
73+
bool(true)
74+
Ensure enums are included in get_declared_classes().
75+
bool(true)
76+
bool(true)
77+
bool(true)
78+
bool(true)
79+
Done

0 commit comments

Comments
 (0)