Skip to content

GH-16447: improve enums' error when missing interface's hooked properties #16448

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

Open
wants to merge 1 commit into
base: PHP-8.4
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions Zend/tests/property_hooks/gh16447_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
GH-16447: Enum error messages should not suggest adding methods (get)
--FILE--
<?php

interface I {
public string $prop { get; }
}

enum E implements I {}

?>
--EXPECTF--
Fatal error: Enum E cannot implement interface I that contains hooked property I::$prop in %s on line %d
14 changes: 14 additions & 0 deletions Zend/tests/property_hooks/gh16447_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
GH-16447: Enum error messages should not suggest adding methods (set)
--FILE--
<?php

interface I {
public string $prop { set; }
}

enum E implements I {}

?>
--EXPECTF--
Fatal error: Enum E cannot implement interface I that contains hooked property I::$prop in %s on line %d
15 changes: 15 additions & 0 deletions Zend/tests/property_hooks/gh16447_3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
GH-16447: Enum error messages should not suggest adding methods ($name get allowed)
--FILE--
<?php

interface I {
public string $name { get; }
}

enum E implements I {}

?>
OKAY
--EXPECT--
OKAY
15 changes: 15 additions & 0 deletions Zend/tests/property_hooks/gh16447_4.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
GH-16447: Enum error messages should not suggest adding methods ($value get allowed on backed enum)
--FILE--
<?php

interface I {
public string $value { get; }
}

enum E: string implements I {}

?>
OKAY
--EXPECT--
OKAY
12 changes: 12 additions & 0 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -3002,6 +3002,18 @@ void zend_verify_abstract_class(zend_class_entry *ce) /* {{{ */
const zend_function *fn = prop_info->hooks[i];
if (fn && (fn->common.fn_flags & ZEND_ACC_ABSTRACT)) {
zend_verify_abstract_class_function(fn, &ai);
// Short-circuit on enums that cannot have hooked
// properties
if (ce->ce_flags & ZEND_ACC_ENUM) {
zend_error_noreturn(
E_ERROR,
"Enum %s cannot implement interface %s that contains hooked property %s::$%s",
ZSTR_VAL(ce->name),
ZSTR_VAL(prop_info->ce->name),
ZSTR_VAL(prop_info->ce->name),
ZSTR_VAL(prop_info->name)
);
}
}
}
}
Expand Down