Skip to content

Commit ab67e04

Browse files
authored
Stabilize enum interface error handling and tests (#1954)
1 parent 15e343c commit ab67e04

4 files changed

Lines changed: 14 additions & 17 deletions

File tree

src/BSON/Persistable.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,7 @@
2121

2222
zend_class_entry* php_phongo_persistable_ce;
2323

24-
static int php_phongo_implement_persistable(zend_class_entry* interface, zend_class_entry* class_type)
25-
{
26-
if (class_type->ce_flags & ZEND_ACC_ENUM) {
27-
zend_error_noreturn(E_ERROR, "Enum class %s cannot implement interface %s", ZSTR_VAL(class_type->name), ZSTR_VAL(interface->name));
28-
return FAILURE;
29-
}
30-
31-
return SUCCESS;
32-
}
33-
3424
void php_phongo_persistable_init_ce(INIT_FUNC_ARGS)
3525
{
36-
php_phongo_persistable_ce = register_class_MongoDB_BSON_Persistable(php_phongo_serializable_ce, php_phongo_unserializable_ce);
37-
php_phongo_persistable_ce->interface_gets_implemented = php_phongo_implement_persistable;
26+
php_phongo_persistable_ce = register_class_MongoDB_BSON_Persistable(php_phongo_serializable_ce, php_phongo_unserializable_ce);
3827
}

src/BSON/Unserializable.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ zend_class_entry* php_phongo_unserializable_ce;
2424
static int php_phongo_implement_unserializable(zend_class_entry* interface, zend_class_entry* class_type)
2525
{
2626
if (class_type->ce_flags & ZEND_ACC_ENUM) {
27-
zend_error_noreturn(E_ERROR, "Enum class %s cannot implement interface %s", ZSTR_VAL(class_type->name), ZSTR_VAL(interface->name));
27+
zend_class_entry* error_interface = interface;
28+
29+
/* Persistable extends Unserializable, so this handler may be called for
30+
* Persistable enums; prefer Persistable in the fatal message when applicable. */
31+
if (interface == php_phongo_unserializable_ce && php_phongo_persistable_ce && instanceof_function(class_type, php_phongo_persistable_ce)) {
32+
error_interface = php_phongo_persistable_ce;
33+
}
34+
35+
zend_error_noreturn(E_ERROR, "Enum class %s cannot implement interface %s", ZSTR_VAL(class_type->name), ZSTR_VAL(error_interface->name));
2836
return FAILURE;
2937
}
3038

tests/bson/bson-enum_error-003.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ enum MyEnum implements MongoDB\BSON\Persistable
1515
?>
1616
===DONE===
1717
<?php exit(0); ?>
18-
--EXPECTREGEX--
19-
Fatal error: Enum class MyEnum cannot implement interface MongoDB\\BSON\\(Persistable|Unserializable) in .+ on line \d+
18+
--EXPECTF--
19+
Fatal error: Enum class MyEnum cannot implement interface MongoDB\BSON\Persistable in %s on line %d

tests/bson/bson-enum_error-004.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ enum MyBackedEnum: int implements MongoDB\BSON\Persistable
1515
?>
1616
===DONE===
1717
<?php exit(0); ?>
18-
--EXPECTREGEX--
19-
Fatal error: Enum class MyBackedEnum cannot implement interface MongoDB\\BSON\\(Persistable|Unserializable) in .+ on line \d+
18+
--EXPECTF--
19+
Fatal error: Enum class MyBackedEnum cannot implement interface MongoDB\BSON\Persistable in %s on line %d

0 commit comments

Comments
 (0)