Skip to content

Commit 733023b

Browse files
committed
Improve error message class type
Refer to interfaces/enums instead of classes in more places. Closes GH-7792 Closes GH-8187
1 parent b9b134d commit 733023b

16 files changed

+108
-19
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ PHP NEWS
88
- Core:
99
. Fixed bug #81380 (Observer may not be initialized properly). (krakjoe)
1010
. Fixed bug GH-7771 (Fix filename/lineno of constant expressions). (ilutov)
11+
. Fixed bug GH-7792 (Improve class type in error messages). (ilutov)
1112

1213
- Intl:
1314
. Update all grandfathered language tags with preferred values

Zend/tests/class_alias_009.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ interface c extends a, b { }
1111

1212
?>
1313
--EXPECTF--
14-
Fatal error: Class c cannot implement previously implemented interface a in %s on line %d
14+
Fatal error: Interface c cannot implement previously implemented interface a in %s on line %d

Zend/tests/constants/final_constants/final_const12.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ interface I3 extends I1, I2
1919

2020
?>
2121
--EXPECTF--
22-
Fatal error: Class I3 inherits both I1::C and I2::C, which is ambiguous in %s on line %d
22+
Fatal error: Interface I3 inherits both I1::C and I2::C, which is ambiguous in %s on line %d

Zend/tests/enum/no-enum-implements-backed-enum.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ enum Foo: int implements BackedEnum {}
77

88
?>
99
--EXPECTF--
10-
Fatal error: Class Foo cannot implement previously implemented interface BackedEnum in %s on line %d
10+
Fatal error: Enum Foo cannot implement previously implemented interface BackedEnum in %s on line %d

Zend/tests/enum/no-enum-implements-unit-enum.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ enum Foo implements UnitEnum {}
77

88
?>
99
--EXPECTF--
10-
Fatal error: Class Foo cannot implement previously implemented interface UnitEnum in %s on line %d
10+
Fatal error: Enum Foo cannot implement previously implemented interface UnitEnum in %s on line %d

Zend/tests/gh7792_1.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
GH-7792 (Refer to enum as enum instead of class)
3+
--FILE--
4+
<?php
5+
6+
interface A {
7+
public function a(): void;
8+
}
9+
10+
enum B implements A {}
11+
12+
?>
13+
--EXPECTF--
14+
Fatal error: Enum B must implement 1 abstract private method (A::a) in %s on line %d

Zend/tests/gh7792_2.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
GH-7792 (Refer to enum as enum instead of class)
3+
--FILE--
4+
<?php
5+
6+
enum Foo implements Throwable {}
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Enum Foo cannot implement interface Throwable in %s on line %d

Zend/tests/gh7792_3.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-7792 (Refer to enum as enum instead of class)
3+
--FILE--
4+
<?php
5+
6+
interface A {
7+
const FOO = 'foo';
8+
}
9+
10+
interface B {
11+
const FOO = 'foo';
12+
}
13+
14+
enum Foo implements A, B {}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Enum Foo inherits both A::FOO and B::FOO, which is ambiguous in %s on line %d

Zend/tests/gh7792_4.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
GH-7792 (Refer to enum as enum instead of class)
3+
--FILE--
4+
<?php
5+
6+
interface A {}
7+
8+
enum Foo implements A, A {}
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Enum Foo cannot implement previously implemented interface A in %s on line %d

Zend/tests/gh7792_5.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
GH-7792 (Refer to enum as enum instead of class)
3+
--FILE--
4+
<?php
5+
6+
enum Foo implements Traversable {}
7+
8+
?>
9+
--EXPECT--
10+
Fatal error: Enum Foo must implement interface Traversable as part of either Iterator or IteratorAggregate in Unknown on line 0

Zend/tests/objects_014.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ interface bar extends foo, foo {
1212
echo "Done\n";
1313
?>
1414
--EXPECTF--
15-
Fatal error: Class bar cannot implement previously implemented interface foo in %s on line %d
15+
Fatal error: Interface bar cannot implement previously implemented interface foo in %s on line %d

Zend/zend_API.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4783,14 +4783,16 @@ ZEND_API void zend_restore_error_handling(zend_error_handling *saved) /* {{{ */
47834783
}
47844784
/* }}} */
47854785

4786-
ZEND_API ZEND_COLD const char *zend_get_object_type(const zend_class_entry *ce) /* {{{ */
4786+
ZEND_API ZEND_COLD const char *zend_get_object_type_case(const zend_class_entry *ce, bool upper_case) /* {{{ */
47874787
{
4788-
if(ce->ce_flags & ZEND_ACC_TRAIT) {
4789-
return "trait";
4788+
if (ce->ce_flags & ZEND_ACC_TRAIT) {
4789+
return upper_case ? "Trait" : "trait";
47904790
} else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
4791-
return "interface";
4791+
return upper_case ? "Interface" : "interface";
4792+
} else if (ce->ce_flags & ZEND_ACC_ENUM) {
4793+
return upper_case ? "Enum" : "enum";
47924794
} else {
4793-
return "class";
4795+
return upper_case ? "Class" : "class";
47944796
}
47954797
}
47964798
/* }}} */

Zend/zend_API.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,17 @@ static zend_always_inline zend_result zend_forbid_dynamic_call(void)
721721
return SUCCESS;
722722
}
723723

724-
ZEND_API ZEND_COLD const char *zend_get_object_type(const zend_class_entry *ce);
724+
ZEND_API ZEND_COLD const char *zend_get_object_type_case(const zend_class_entry *ce, bool upper_case);
725+
726+
static zend_always_inline const char *zend_get_object_type(const zend_class_entry *ce)
727+
{
728+
return zend_get_object_type_case(ce, false);
729+
}
730+
731+
static zend_always_inline const char *zend_get_object_type_uc(const zend_class_entry *ce)
732+
{
733+
return zend_get_object_type_case(ce, true);
734+
}
725735

726736
ZEND_API bool zend_is_iterable(zval *iterable);
727737

Zend/zend_exceptions.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ static int zend_implement_throwable(zend_class_entry *interface, zend_class_entr
6767
return SUCCESS;
6868
}
6969

70+
bool can_extend = (class_type->ce_flags & ZEND_ACC_ENUM) == 0;
71+
7072
zend_error_noreturn(E_ERROR,
71-
"Class %s cannot implement interface %s, extend Exception or Error instead",
73+
can_extend
74+
? "%s %s cannot implement interface %s, extend Exception or Error instead"
75+
: "%s %s cannot implement interface %s",
76+
zend_get_object_type_uc(class_type),
7277
ZSTR_VAL(class_type->name),
7378
ZSTR_VAL(interface->name));
7479
return FAILURE;

Zend/zend_inheritance.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,7 @@ static void do_inherit_property(zend_property_info *parent_info, zend_string *ke
12851285
static inline void do_implement_interface(zend_class_entry *ce, zend_class_entry *iface) /* {{{ */
12861286
{
12871287
if (!(ce->ce_flags & ZEND_ACC_INTERFACE) && iface->interface_gets_implemented && iface->interface_gets_implemented(iface, ce) == FAILURE) {
1288-
zend_error_noreturn(E_CORE_ERROR, "Class %s could not implement interface %s", ZSTR_VAL(ce->name), ZSTR_VAL(iface->name));
1288+
zend_error_noreturn(E_CORE_ERROR, "%s %s could not implement interface %s", zend_get_object_type_uc(ce), ZSTR_VAL(ce->name), ZSTR_VAL(iface->name));
12891289
}
12901290
/* This should be prevented by the class lookup logic. */
12911291
ZEND_ASSERT(ce != iface);
@@ -1610,7 +1610,8 @@ static bool do_inherit_constant_check(
16101610

16111611
if (old_constant->ce != parent_constant->ce && old_constant->ce != ce) {
16121612
zend_error_noreturn(E_COMPILE_ERROR,
1613-
"Class %s inherits both %s::%s and %s::%s, which is ambiguous",
1613+
"%s %s inherits both %s::%s and %s::%s, which is ambiguous",
1614+
zend_get_object_type_uc(ce),
16141615
ZSTR_VAL(ce->name),
16151616
ZSTR_VAL(old_constant->ce->name), ZSTR_VAL(name),
16161617
ZSTR_VAL(parent_constant->ce->name), ZSTR_VAL(name));
@@ -1729,7 +1730,10 @@ static void zend_do_implement_interfaces(zend_class_entry *ce, zend_class_entry
17291730
if (interfaces[j] == iface) {
17301731
if (j >= num_parent_interfaces) {
17311732
efree(interfaces);
1732-
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot implement previously implemented interface %s", ZSTR_VAL(ce->name), ZSTR_VAL(iface->name));
1733+
zend_error_noreturn(E_COMPILE_ERROR, "%s %s cannot implement previously implemented interface %s",
1734+
zend_get_object_type_uc(ce),
1735+
ZSTR_VAL(ce->name),
1736+
ZSTR_VAL(iface->name));
17331737
return;
17341738
}
17351739
/* skip duplications */
@@ -2311,6 +2315,7 @@ void zend_verify_abstract_class(zend_class_entry *ce) /* {{{ */
23112315
zend_function *func;
23122316
zend_abstract_info ai;
23132317
bool is_explicit_abstract = (ce->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) != 0;
2318+
bool can_be_abstract = (ce->ce_flags & ZEND_ACC_ENUM) == 0;
23142319
memset(&ai, 0, sizeof(ai));
23152320

23162321
ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, func) {
@@ -2324,9 +2329,10 @@ void zend_verify_abstract_class(zend_class_entry *ce) /* {{{ */
23242329
} ZEND_HASH_FOREACH_END();
23252330

23262331
if (ai.cnt) {
2327-
zend_error_noreturn(E_ERROR, !is_explicit_abstract
2328-
? "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 ")"
2329-
: "Class %s must implement %d abstract private method%s (" MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT ")",
2332+
zend_error_noreturn(E_ERROR, !is_explicit_abstract && can_be_abstract
2333+
? "%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 ")"
2334+
: "%s %s must implement %d abstract private method%s (" MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT ")",
2335+
zend_get_object_type_uc(ce),
23302336
ZSTR_VAL(ce->name), ai.cnt,
23312337
ai.cnt > 1 ? "s" : "",
23322338
DISPLAY_ABSTRACT_FN(0),

Zend/zend_interfaces.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ static int zend_implement_traversable(zend_class_entry *interface, zend_class_en
266266
}
267267
}
268268
}
269-
zend_error_noreturn(E_CORE_ERROR, "Class %s must implement interface %s as part of either %s or %s",
269+
zend_error_noreturn(E_CORE_ERROR, "%s %s must implement interface %s as part of either %s or %s",
270+
zend_get_object_type_uc(class_type),
270271
ZSTR_VAL(class_type->name),
271272
ZSTR_VAL(zend_ce_traversable->name),
272273
ZSTR_VAL(zend_ce_iterator->name),

0 commit comments

Comments
 (0)