Skip to content

Commit 3b14e2c

Browse files
[RFC] Allow __debugInfo() on enums
https://wiki.php.net/rfc/debugable-enums
1 parent a026333 commit 3b14e2c

9 files changed

Lines changed: 114 additions & 19 deletions

File tree

Zend/tests/enum/__debugInfo.phpt

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Enum with __debugInfo() magic method - backed enum value accessible
3+
--FILE--
4+
<?php
5+
6+
enum Foo: string {
7+
case Bar = "Baz";
8+
9+
public function __debugInfo() {
10+
return [ __CLASS__ . '::' . $this->name . ' = ' . $this->value ];
11+
}
12+
}
13+
14+
var_dump(Foo::Bar);
15+
16+
?>
17+
--EXPECTF--
18+
object(Foo)#%d (1) {
19+
[0]=>
20+
string(14) "Foo::Bar = Baz"
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Enum supports __debugInfo() magic method
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
9+
public function __debugInfo() {
10+
return [$this->name . ' is a case of the ' . __CLASS__ . ' enum'];
11+
}
12+
}
13+
14+
var_dump(Foo::Bar);
15+
16+
?>
17+
--EXPECTF--
18+
object(Foo)#%d (1) {
19+
[0]=>
20+
string(29) "Bar is a case of the Foo enum"
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
When an enum does not have __debugInfo() it is printed nicely
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
}
9+
10+
var_dump(Foo::Bar);
11+
12+
?>
13+
--EXPECT--
14+
enum(Foo::Bar)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Enum with __debugInfo() magic method - param validation
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
9+
public function __debugInfo(mixed $arg): array {
10+
return [];
11+
}
12+
}
13+
14+
var_dump(Foo::Bar);
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Method Foo::__debugInfo() cannot take arguments in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Enum with __debugInfo() magic method - return validation
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
9+
public function __debugInfo(): int {
10+
return 5;
11+
}
12+
}
13+
14+
var_dump(Foo::Bar);
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Foo::__debugInfo(): Return type must be ?array when declared in %s on line %d
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Enum with __debugInfo() magic method - visibility validation
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar;
8+
9+
private function __debugInfo(): array {
10+
return [];
11+
}
12+
}
13+
14+
var_dump(Foo::Bar);
15+
16+
?>
17+
--EXPECTF--
18+
Warning: The magic method Foo::__debugInfo() must have public visibility in %s on line %d
19+
object(Foo)#%d (0) {
20+
}

Zend/zend_enum.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static void zend_verify_enum_properties(const zend_class_entry *ce)
9191

9292
static void zend_verify_enum_magic_methods(const zend_class_entry *ce)
9393
{
94-
// Only __get, __call and __invoke are allowed
94+
// Only __get, __call, __debugInfo and __invoke are allowed
9595

9696
ZEND_ENUM_DISALLOW_MAGIC_METHOD(constructor, "__construct");
9797
ZEND_ENUM_DISALLOW_MAGIC_METHOD(destructor, "__destruct");
@@ -101,7 +101,6 @@ static void zend_verify_enum_magic_methods(const zend_class_entry *ce)
101101
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__unset, "__unset");
102102
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__isset, "__isset");
103103
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__tostring, "__toString");
104-
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__debugInfo, "__debugInfo");
105104
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__serialize, "__serialize");
106105
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__unserialize, "__unserialize");
107106

ext/standard/var.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ PHPAPI void php_var_dump(zval *struc, int level) /* {{{ */
168168
break;
169169
case IS_OBJECT: {
170170
zend_class_entry *ce = Z_OBJCE_P(struc);
171-
if (ce->ce_flags & ZEND_ACC_ENUM) {
171+
if (ce->ce_flags & ZEND_ACC_ENUM && ce->__debugInfo == NULL) {
172172
zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc));
173173
php_printf("%senum(%s::%s)\n", COMMON, ZSTR_VAL(ce->name), Z_STRVAL_P(case_name_zval));
174174
return;

0 commit comments

Comments
 (0)