Skip to content
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
16 changes: 0 additions & 16 deletions Zend/tests/enum/__debugInfo.phpt

This file was deleted.

21 changes: 21 additions & 0 deletions Zend/tests/enum/debugInfo/backed_enum_value.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Enum with __debugInfo() magic method - backed enum value accessible
--FILE--
<?php

enum Foo: string {
case Bar = "Baz";

public function __debugInfo() {
return [ __CLASS__ . '::' . $this->name . ' = ' . $this->value ];
}
}

var_dump(Foo::Bar);

?>
--EXPECTF--
object(Foo)#%d (1) {
[0]=>
string(14) "Foo::Bar = Baz"
}
21 changes: 21 additions & 0 deletions Zend/tests/enum/debugInfo/magic_method.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Enum supports __debugInfo() magic method
--FILE--
<?php

enum Foo {
case Bar;

public function __debugInfo() {
return [$this->name . ' is a case of the ' . __CLASS__ . ' enum'];
}
}

var_dump(Foo::Bar);

?>
--EXPECTF--
object(Foo)#%d (1) {
[0]=>
string(29) "Bar is a case of the Foo enum"
}
14 changes: 14 additions & 0 deletions Zend/tests/enum/debugInfo/missing_magic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
When an enum does not have __debugInfo() it is printed nicely
--FILE--
<?php

enum Foo {
case Bar;
}

var_dump(Foo::Bar);

?>
--EXPECT--
enum(Foo::Bar)
18 changes: 18 additions & 0 deletions Zend/tests/enum/debugInfo/param_validation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Enum with __debugInfo() magic method - param validation
--FILE--
<?php

enum Foo {
case Bar;

public function __debugInfo(mixed $arg): array {
return [];
}
}

var_dump(Foo::Bar);

?>
--EXPECTF--
Fatal error: Method Foo::__debugInfo() cannot take arguments in %s on line %d
18 changes: 18 additions & 0 deletions Zend/tests/enum/debugInfo/return_validation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Enum with __debugInfo() magic method - return validation
--FILE--
<?php

enum Foo {
case Bar;

public function __debugInfo(): int {
return 5;
}
}

var_dump(Foo::Bar);

?>
--EXPECTF--
Fatal error: Foo::__debugInfo(): Return type must be ?array when declared in %s on line %d
20 changes: 20 additions & 0 deletions Zend/tests/enum/debugInfo/visibility_validation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Enum with __debugInfo() magic method - visibility validation
--FILE--
<?php

enum Foo {
case Bar;

private function __debugInfo(): array {
return [];
}
}

var_dump(Foo::Bar);

?>
--EXPECTF--
Warning: The magic method Foo::__debugInfo() must have public visibility in %s on line %d
object(Foo)#%d (0) {
}
3 changes: 1 addition & 2 deletions Zend/zend_enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static void zend_verify_enum_properties(const zend_class_entry *ce)

static void zend_verify_enum_magic_methods(const zend_class_entry *ce)
{
// Only __get, __call and __invoke are allowed
// Only __get, __call, __debugInfo and __invoke are allowed

ZEND_ENUM_DISALLOW_MAGIC_METHOD(constructor, "__construct");
ZEND_ENUM_DISALLOW_MAGIC_METHOD(destructor, "__destruct");
Expand All @@ -101,7 +101,6 @@ static void zend_verify_enum_magic_methods(const zend_class_entry *ce)
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__unset, "__unset");
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__isset, "__isset");
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__tostring, "__toString");
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__debugInfo, "__debugInfo");
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__serialize, "__serialize");
ZEND_ENUM_DISALLOW_MAGIC_METHOD(__unserialize, "__unserialize");

Expand Down
2 changes: 1 addition & 1 deletion ext/standard/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ PHPAPI void php_var_dump(zval *struc, int level) /* {{{ */
break;
case IS_OBJECT: {
zend_class_entry *ce = Z_OBJCE_P(struc);
if (ce->ce_flags & ZEND_ACC_ENUM) {
if (ce->ce_flags & ZEND_ACC_ENUM && ce->__debugInfo == NULL) {
zval *case_name_zval = zend_enum_fetch_case_name(Z_OBJ_P(struc));
php_printf("%senum(%s::%s)\n", COMMON, ZSTR_VAL(ce->name), Z_STRVAL_P(case_name_zval));
return;
Expand Down
Loading