Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
KalleZ opened this issue Dec 19, 2021 · 1 comment
Closed

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

KalleZ opened this issue Dec 19, 2021 · 1 comment

Comments

@KalleZ
Copy link
Member

KalleZ commented Dec 19, 2021

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

@iluuu1994
Copy link
Member

iluuu1994 commented Dec 19, 2021

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.

@iluuu1994 iluuu1994 added Feature and removed Bug labels Mar 10, 2022
iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Mar 10, 2022
Refer to iterfaces/enums instead of classes in more places.

Closes phpGH-7792
iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Mar 10, 2022
Refer to interfaces/enums instead of classes in more places.

Closes phpGH-7792
iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Mar 10, 2022
Refer to interfaces/enums instead of classes in more places.

Closes phpGH-7792
iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Mar 18, 2022
Refer to interfaces/enums instead of classes in more places.

Closes phpGH-7792
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants