Skip to content

Wrong error message if Enum doesn't correctly implement an interface #7792

Closed

Description

@KalleZ

Description

The following code:

<?php
interface A {
    public function a(): void;
}

enum B implements A {
}

Resulted in this output:

PHP Fatal error:  Class E contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (I::a) in Command line code on line 1

But I expected this output instead:

PHP Fatal error:  Enum E contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (I::a) in Command line code on line 1

Patch:

diff --git a/Zend/zend_API.c b/Zend/zend_API.c
index 3ef291c315..04d5728ef1 100644
--- a/Zend/zend_API.c
+++ b/Zend/zend_API.c
@@ -4789,6 +4789,8 @@ ZEND_API ZEND_COLD const char *zend_get_object_type(const zend_class_entry *ce)
                return "trait";
        } else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
                return "interface";
+       } else if (ce->ce_flags & ZEND_ACC_ENUM) {
+               return "enum";
        } else {
                return "class";
        }
diff --git a/Zend/zend_inheritance.c b/Zend/zend_inheritance.c
index 82af3d8afa..002da24383 100644
--- a/Zend/zend_inheritance.c
+++ b/Zend/zend_inheritance.c
@@ -2324,9 +2324,13 @@ void zend_verify_abstract_class(zend_class_entry *ce) /* {{{ */
        } ZEND_HASH_FOREACH_END();

        if (ai.cnt) {
+               char* kind = strdup(zend_get_object_type(ce));
+               kind[0] = toupper(kind[0]);
+
                zend_error_noreturn(E_ERROR, !is_explicit_abstract
-                       ? "Class %s contains %d abstract method%s and must therefore be declared abstract or implement the remaining methods (" MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT ")"
-                       : "Class %s must implement %d abstract private method%s (" MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT ")",
+                       ? "%s %s contains %d abstract method%s and must therefore be declared abstract or implement the remaining methods (" MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT ")"
+                       : "%s %s must implement %d abstract private method%s (" MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT ")",
+                       kind,
                        ZSTR_VAL(ce->name), ai.cnt,
                        ai.cnt > 1 ? "s" : "",
                        DISPLAY_ABSTRACT_FN(0),

PHP Version

8.1.*

Operating System

Irrelevant

Activity

  1. iluuu1994 commented on Dec 19, 2021

    @iluuu1994
    Member

    We've explicitly decided not to adjust error messages in fear of opening a can of worms. I'm ok with this change but there are likely other places where the same thing applies.

    I missed that zend_get_object_type was a new thing 👍

    It's not 😄 I'll check how many other places exists where class could be replaced with enum.

  2. added a commit that references this issue on Mar 18, 2022
    ea0a6a8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions