Skip to content

Enum error message consistency #9350

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

Merged
merged 18 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
14b5d8c
Updated magic method error message, updated existing tests, and added…
ollieread Aug 16, 2022
d0b5dea
Updated __isset enum tests
ollieread Aug 16, 2022
fa3244b
Remove __construct and __destruct tests in favour of existing ones
ollieread Aug 16, 2022
c90fa62
Fix existing enum tests to include new magic method error message
ollieread Aug 16, 2022
0f0f39d
Updated enum exception messages to be consistent, and not wrap enum n…
ollieread Aug 16, 2022
7f0b4dc
Merge branch 'master' into enum-stringable-and-magic-methods
ollieread Aug 16, 2022
bee3186
Normalise enum property error message to not quote enum name
ollieread Aug 16, 2022
533f0f8
Fixed internal enums test
ollieread Aug 16, 2022
2d07685
Unquote the integer value for invalid enum backing value error message
ollieread Aug 19, 2022
fd3dbad
Fixed expected error message for invalid int on enum backing test
ollieread Aug 19, 2022
4054284
Fix internal enum tests with correct error message
ollieread Aug 19, 2022
320344b
Change "may not" to "cannot" within the enum error messages
ollieread Aug 22, 2022
1b2a8cb
Added the enum class name to the enum error messages
ollieread Aug 22, 2022
0e3c8a3
Updated enum error messages in enum tests
ollieread Aug 22, 2022
3a90c2b
Remove return statement for enum __set test
ollieread Aug 22, 2022
44d0066
Correct plurality of "enum" in enum error messages
ollieread Aug 22, 2022
a985251
Update enum static property error messages to include enum class anme
ollieread Aug 22, 2022
688084d
Change enum error message when an enum has properties
ollieread Aug 23, 2022
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
15 changes: 15 additions & 0 deletions Zend/tests/enum/__clone.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Enum __clone
Copy link
Member

Choose a reason for hiding this comment

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

Nit:

Suggested change
Enum __clone
Enums cannot include the __clone magic method

Copy link
Member

Choose a reason for hiding this comment

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

The rest of the tests could profit from this change

--FILE--
<?php

enum Foo {
case Bar;

public function __clone() {
}
}

?>
--EXPECTF--
Fatal error: Enum Foo cannot include magic method __clone in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/enum/__debugInfo.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Enum __debugInfo
--FILE--
<?php

enum Foo {
case Bar;

public function __debugInfo(): array {
return $this->cases();
}
}

?>
--EXPECTF--
Fatal error: Enum Foo cannot include magic method __debugInfo in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/enum/__get.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ enum Foo {

?>
--EXPECTF--
Fatal error: Enum may not include __get in %s on line %d
Fatal error: Enum Foo cannot include magic method __get in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/enum/__isset.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ enum Foo {

?>
--EXPECTF--
Fatal error: Enum may not include __isset in %s on line %d
Fatal error: Enum Foo cannot include magic method __isset in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/enum/__serialize.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Enum __serialize
--FILE--
<?php

enum Foo {
case Bar;

public function __serialize(): array {
return $this->cases();
}
}

?>
--EXPECTF--
Fatal error: Enum Foo cannot include magic method __serialize in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/enum/__set.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Enum __set
--FILE--
<?php

enum Foo {
case Bar;

public function __set(string $name, mixed $value)
{
}
}

?>
--EXPECTF--
Fatal error: Enum Foo cannot include magic method __set in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/enum/__set_state.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Enum __set_state
--FILE--
<?php

enum Foo {
case Bar;

public static function __set_state(array $properties): object {

}
}

?>
--EXPECTF--
Fatal error: Enum Foo cannot include magic method __set_state in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/enum/__sleep.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Enum __sleep
--FILE--
<?php

enum Foo {
case Bar;

public function __sleep(): array {

}
}

?>
--EXPECTF--
Fatal error: Enum Foo cannot include magic method __sleep in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/enum/__toString.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Enum __toString
--FILE--
<?php

enum Foo {
case Bar;

public function __toString(): string {
return $this->name;
}
}

?>
--EXPECTF--
Fatal error: Enum Foo cannot include magic method __toString in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/enum/__unserialize.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Enum __unserialize
--FILE--
<?php

enum Foo {
case Bar;

public function __unserialize(array $data) {

}
}

?>
--EXPECTF--
Fatal error: Enum Foo cannot include magic method __unserialize in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/enum/__unset.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Enum __unset
--FILE--
<?php

enum Foo {
case Bar;

public function __unset($property) {
return;
}
}

?>
--EXPECTF--
Fatal error: Enum Foo cannot include magic method __unset in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/enum/__wakeup.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Enum __wakeup
--FILE--
<?php

enum Foo {
case Bar;

public function __wakeup() {

}
}

?>
--EXPECTF--
Fatal error: Enum Foo cannot include magic method __wakeup in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/enum/backed-from-invalid-int.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ try {

?>
--EXPECT--
2 is not a valid backing value for enum "Foo"
2 is not a valid backing value for enum Foo
2 changes: 1 addition & 1 deletion Zend/tests/enum/backed-from-invalid-string.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ try {

?>
--EXPECT--
"A" is not a valid backing value for enum "Suit"
"A" is not a valid backing value for enum Suit
2 changes: 1 addition & 1 deletion Zend/tests/enum/backed-from-invalid-type.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ try {

?>
--EXPECT--
"42" is not a valid backing value for enum "Suit"
"42" is not a valid backing value for enum Suit
Foo::from(): Argument #1 ($value) must be of type int, string given
4 changes: 2 additions & 2 deletions Zend/tests/enum/internal_enums.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ enum(ZendTestIntEnum::Foo)
enum(ZendTestIntEnum::Foo)
enum(ZendTestIntEnum::Foo)
enum(ZendTestIntEnum::Foo)
ValueError: 2 is not a valid backing value for enum "ZendTestIntEnum"
ValueError: 2 is not a valid backing value for enum ZendTestIntEnum
NULL
ValueError: 2 is not a valid backing value for enum "ZendTestIntEnum"
ValueError: 2 is not a valid backing value for enum ZendTestIntEnum
NULL
enum(ZendTestIntEnum::Baz)
enum(ZendTestIntEnum::Baz)
Expand Down
2 changes: 1 addition & 1 deletion Zend/tests/enum/no-constructors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ enum Foo {

?>
--EXPECTF--
Fatal error: Enum may not include __construct in %s on line %d
Fatal error: Enum Foo cannot include magic method __construct in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/enum/no-destruct.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ enum Foo {

?>
--EXPECTF--
Fatal error: Enum may not include __destruct in %s on line %d
Fatal error: Enum Foo cannot include magic method __destruct in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/enum/no-implement-serializable-indirect.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ var_dump(unserialize(serialize(Foo::Bar)));
--EXPECTF--
Deprecated: %s implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d

Fatal error: Enums may not implement the Serializable interface in %s on line %d
Fatal error: Enum Foo cannot implement the Serializable interface in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/enum/no-implement-serializable.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ var_dump(unserialize(serialize(Foo::Bar)));
--EXPECTF--
Deprecated: %s implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d

Fatal error: Enums may not implement the Serializable interface in %s on line %d
Fatal error: Enum Foo cannot implement the Serializable interface in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/enum/no-name-property.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ enum Foo {

?>
--EXPECTF--
Fatal error: Enums may not include properties in %s on line %d
Fatal error: Enum Foo cannot include properties in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/enum/no-properties.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ enum Foo {

?>
--EXPECTF--
Fatal error: Enums may not include properties in %s on line %d
Fatal error: Enum Foo cannot include properties in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/enum/no-static-properties.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ enum Foo {

?>
--EXPECTF--
Fatal error: Enums may not include properties in %s on line %d
Fatal error: Enum Foo cannot include properties in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/enum/no-value-property.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ enum Foo: int {

?>
--EXPECTF--
Fatal error: Enums may not include properties in %s on line %d
Fatal error: Enum Foo cannot include properties in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/enum/traits-no-__construct.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ var_dump(Bar::Baz);

?>
--EXPECTF--
Fatal error: Enum may not include __construct in %s on line %d
Fatal error: Enum Bar cannot include magic method __construct in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/enum/traits-no-forbidden-methods.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ enum Suit {

?>
--EXPECTF--
Fatal error: Enum may not include __construct in %s on line %d
Fatal error: Enum Suit cannot include magic method __construct in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/enum/traits-no-properties.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ enum Suit {

?>
--EXPECTF--
Fatal error: Enum "Suit" may not include properties in %s on line %d
Fatal error: Enum Suit cannot include properties in %s on line %d
2 changes: 1 addition & 1 deletion Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -7405,7 +7405,7 @@ static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t f
}

if (ce->ce_flags & ZEND_ACC_ENUM) {
zend_error_noreturn(E_COMPILE_ERROR, "Enums may not include properties");
zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name));
}

if (flags & ZEND_ACC_ABSTRACT) {
Expand Down
12 changes: 6 additions & 6 deletions Zend/zend_enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define ZEND_ENUM_DISALLOW_MAGIC_METHOD(propertyName, methodName) \
do { \
if (ce->propertyName) { \
zend_error_noreturn(E_COMPILE_ERROR, "Enum may not include %s", methodName); \
zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include magic method %s", ZSTR_VAL(ce->name), methodName); \
} \
} while (0);

Expand Down Expand Up @@ -67,7 +67,7 @@ static void zend_verify_enum_properties(zend_class_entry *ce)
continue;
}
// FIXME: File/line number for traits?
zend_error_noreturn(E_COMPILE_ERROR, "Enum \"%s\" may not include properties",
zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties",
ZSTR_VAL(ce->name));
} ZEND_HASH_FOREACH_END();
}
Expand Down Expand Up @@ -99,7 +99,7 @@ static void zend_verify_enum_magic_methods(zend_class_entry *ce)
const char *forbidden_method = forbidden_methods[i];

if (zend_hash_str_exists(&ce->function_table, forbidden_method, strlen(forbidden_method))) {
zend_error_noreturn(E_COMPILE_ERROR, "Enum may not include magic method %s", forbidden_method);
zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include magic method %s", ZSTR_VAL(ce->name), forbidden_method);
}
}
}
Expand All @@ -108,7 +108,7 @@ static void zend_verify_enum_interfaces(zend_class_entry *ce)
{
if (zend_class_implements_interface(ce, zend_ce_serializable)) {
zend_error_noreturn(E_COMPILE_ERROR,
"Enums may not implement the Serializable interface");
"Enum %s cannot implement the Serializable interface", ZSTR_VAL(ce->name));
}
}

Expand Down Expand Up @@ -307,10 +307,10 @@ ZEND_API zend_result zend_enum_get_case_by_value(zend_object **result, zend_clas
}

if (ce->enum_backing_type == IS_LONG) {
zend_value_error(ZEND_LONG_FMT " is not a valid backing value for enum \"%s\"", long_key, ZSTR_VAL(ce->name));
zend_value_error(ZEND_LONG_FMT " is not a valid backing value for enum %s", long_key, ZSTR_VAL(ce->name));
} else {
ZEND_ASSERT(ce->enum_backing_type == IS_STRING);
zend_value_error("\"%s\" is not a valid backing value for enum \"%s\"", ZSTR_VAL(string_key), ZSTR_VAL(ce->name));
zend_value_error("\"%s\" is not a valid backing value for enum %s", ZSTR_VAL(string_key), ZSTR_VAL(ce->name));
}
return FAILURE;
}
Expand Down