Skip to content
Merged
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
13 changes: 1 addition & 12 deletions src/BSON/Persistable.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,7 @@

zend_class_entry* php_phongo_persistable_ce;

static int php_phongo_implement_persistable(zend_class_entry* interface, zend_class_entry* class_type)
{
if (class_type->ce_flags & ZEND_ACC_ENUM) {
zend_error_noreturn(E_ERROR, "Enum class %s cannot implement interface %s", ZSTR_VAL(class_type->name), ZSTR_VAL(interface->name));
return FAILURE;
}

return SUCCESS;
}

void php_phongo_persistable_init_ce(INIT_FUNC_ARGS)
{
php_phongo_persistable_ce = register_class_MongoDB_BSON_Persistable(php_phongo_serializable_ce, php_phongo_unserializable_ce);
php_phongo_persistable_ce->interface_gets_implemented = php_phongo_implement_persistable;
php_phongo_persistable_ce = register_class_MongoDB_BSON_Persistable(php_phongo_serializable_ce, php_phongo_unserializable_ce);
}
10 changes: 9 additions & 1 deletion src/BSON/Unserializable.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ zend_class_entry* php_phongo_unserializable_ce;
static int php_phongo_implement_unserializable(zend_class_entry* interface, zend_class_entry* class_type)
{
if (class_type->ce_flags & ZEND_ACC_ENUM) {
zend_error_noreturn(E_ERROR, "Enum class %s cannot implement interface %s", ZSTR_VAL(class_type->name), ZSTR_VAL(interface->name));
zend_class_entry* error_interface = interface;

/* Persistable extends Unserializable, so this handler may be called for
* Persistable enums; prefer Persistable in the fatal message when applicable. */
if (interface == php_phongo_unserializable_ce && php_phongo_persistable_ce && instanceof_function(class_type, php_phongo_persistable_ce)) {
error_interface = php_phongo_persistable_ce;
}
Comment on lines +29 to +33
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not convinced to keep this part. That's the Unserializable behavior that is not supported.

Copy link
Copy Markdown
Member Author

@GromNaN GromNaN Mar 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Unserializable::bsonUnserialize() was static, then it could return an enum case and this would not be an issue.


zend_error_noreturn(E_ERROR, "Enum class %s cannot implement interface %s", ZSTR_VAL(class_type->name), ZSTR_VAL(error_interface->name));
return FAILURE;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/bson/bson-enum_error-003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ enum MyEnum implements MongoDB\BSON\Persistable
?>
===DONE===
<?php exit(0); ?>
--EXPECTREGEX--
Fatal error: Enum class MyEnum cannot implement interface MongoDB\\BSON\\(Persistable|Unserializable) in .+ on line \d+
--EXPECTF--
Fatal error: Enum class MyEnum cannot implement interface MongoDB\BSON\Persistable in %s on line %d
4 changes: 2 additions & 2 deletions tests/bson/bson-enum_error-004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ enum MyBackedEnum: int implements MongoDB\BSON\Persistable
?>
===DONE===
<?php exit(0); ?>
--EXPECTREGEX--
Fatal error: Enum class MyBackedEnum cannot implement interface MongoDB\\BSON\\(Persistable|Unserializable) in .+ on line \d+
--EXPECTF--
Fatal error: Enum class MyBackedEnum cannot implement interface MongoDB\BSON\Persistable in %s on line %d
Loading