Skip to content

ext/standard: Add get_declared_enums function #15443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ PHP 8.5 UPGRADE NOTES
========================================

- Zlib:
. The "use_include_path" argument for the
. The "use_include_path" argument for the
gzfile, gzopen and readgzfile functions had been changed
from int to boolean.

Expand All @@ -83,6 +83,10 @@ PHP 8.5 UPGRADE NOTES
statement from the DEALLOCATE sql command in that we can reuse
its name afterwards.

- Standard:
. Added get_declared_enums() function that returns a list of declared enums. The
existing get_declared_classes() function continues to include enums.

========================================
7. New Classes and Interfaces
========================================
Expand Down
1 change: 1 addition & 0 deletions Zend/Optimizer/zend_func_infos.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ static const func_info_t func_infos[] = {
F1("get_declared_classes", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
F1("get_declared_traits", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
F1("get_declared_interfaces", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
F1("get_declared_enums", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
F1("get_defined_functions", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ARRAY),
F1("get_defined_vars", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF),
F1("get_resource_type", MAY_BE_STRING),
Expand Down
9 changes: 8 additions & 1 deletion Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@ static inline void get_declared_class_impl(INTERNAL_FUNCTION_PARAMETERS, int fla
ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(EG(class_table), key, zv) {
ce = Z_PTR_P(zv);
if ((ce->ce_flags & (ZEND_ACC_LINKED|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT)) == flags
if ((ce->ce_flags & (flags|ZEND_ACC_LINKED|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT)) == flags
&& key
&& ZSTR_VAL(key)[0] != 0) {
ZEND_HASH_FILL_GROW();
Expand Down Expand Up @@ -1420,6 +1420,13 @@ ZEND_FUNCTION(get_declared_interfaces)
}
/* }}} */

/* {{{ Returns an array of all declared enums. */
ZEND_FUNCTION(get_declared_enums)
{
get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED | ZEND_ACC_ENUM);
}
/* }}} */

/* {{{ Returns an array of all defined functions */
ZEND_FUNCTION(get_defined_functions)
{
Expand Down
6 changes: 6 additions & 0 deletions Zend/zend_builtin_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ function get_declared_traits(): array {}
*/
function get_declared_interfaces(): array {}

/**
* @return array<int, string>
* @refcount 1
*/
function get_declared_enums(): array {}

/**
* @return array<string, array>
* @refcount 1
Expand Down
6 changes: 5 additions & 1 deletion Zend/zend_builtin_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions ext/standard/tests/class_object/get_declared_enums_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
--TEST--
Test get_declared_enums() function
--FILE--
<?php

enum MyUnitEnum {}
enum MyStringBackedEnum: string {
case FOO = 'test';
}
enum MyIntBackedEnum: int {
case FOO = 1;
}
enum MyEnumImplementation implements Countable {
use TraitWithNoProperties;
public function count(): int {
return 0;
}
}

trait TraitWithNoProperties {}

$enumsList = get_declared_enums();
$classesList = get_declared_classes();
var_dump($enumsList);

foreach ($enumsList as $enum) {
if (!enum_exists($enum)) {
echo "Error: $enum is not a valid enum.\n";
}
}

echo "Ensure all enums are included.\n";

var_dump(in_array('MyUnitEnum', $enumsList));
var_dump(in_array('MyStringBackedEnum', $enumsList));
var_dump(in_array('MyIntBackedEnum', $enumsList));
var_dump(in_array('MyEnumImplementation', $enumsList));

echo "Ensure interfaces are not included.\n";
var_dump(!in_array('Throwable', $enumsList));
var_dump(!in_array('IntBackedEnum', $enumsList));

echo "Ensure traits are not included.\n";
var_dump(!in_array('TraitWithNoProperties', $enumsList));

echo "Ensure classes are not included.\n";
var_dump(!in_array('stdClass', $enumsList));
var_dump(!in_array('Exception', $enumsList));

echo "Ensure enums are included in get_declared_classes().\n";
var_dump(in_array('MyUnitEnum', $classesList));
var_dump(in_array('MyStringBackedEnum', $classesList));
var_dump(in_array('MyIntBackedEnum', $classesList));
var_dump(in_array('MyEnumImplementation', $classesList));
echo "Done";
?>
--EXPECTF--
array(%d) {
%a
}
Ensure all enums are included.
bool(true)
bool(true)
bool(true)
bool(true)
Ensure interfaces are not included.
bool(true)
bool(true)
Ensure traits are not included.
bool(true)
Ensure classes are not included.
bool(true)
bool(true)
Ensure enums are included in get_declared_classes().
bool(true)
bool(true)
bool(true)
bool(true)
Done