Skip to content

Commit 312fe2b

Browse files
committed
Unify magic method return type checks
1 parent fbbcf82 commit 312fe2b

File tree

4 files changed

+14
-32
lines changed

4 files changed

+14
-32
lines changed

Zend/tests/return_types/014.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ class Foo {
77
function __construct() : Foo {}
88
}
99
--EXPECTF--
10-
Fatal error: Constructor %s::%s() cannot declare a return type in %s on line %d
10+
Fatal error: Method Foo::__construct() cannot declare a return type in %s on line %d

Zend/tests/return_types/018.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ class Foo {
77
function __destruct() : Foo {}
88
}
99
--EXPECTF--
10-
Fatal error: Destructor %s::%s() cannot declare a return type in %s on line %d
10+
Fatal error: Method Foo::__destruct() cannot declare a return type in %s on line %d

Zend/zend_API.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,15 @@ static void zend_check_magic_method_static(
20492049
}
20502050
}
20512051

2052+
static void zend_check_magic_method_no_return_type(
2053+
const char *name, const zend_class_entry *ce, const zend_function *fptr, int error_type)
2054+
{
2055+
if (fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
2056+
zend_error_noreturn(error_type, "Method %s::%s() cannot declare a return type",
2057+
ZSTR_VAL(ce->name), name);
2058+
}
2059+
}
2060+
20522061
ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce, const zend_function *fptr, zend_string *lcname, int error_type) /* {{{ */
20532062
{
20542063
if (ZSTR_VAL(fptr->common.function_name)[0] != '_'
@@ -2058,12 +2067,15 @@ ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce,
20582067

20592068
if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
20602069
zend_check_magic_method_non_static("__construct", ce, fptr, error_type);
2070+
zend_check_magic_method_no_return_type("__construct", ce, fptr, error_type);
20612071
} else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
20622072
zend_check_magic_method_args(0, "__destruct", ce, fptr, error_type);
20632073
zend_check_magic_method_non_static("__destruct", ce, fptr, error_type);
2074+
zend_check_magic_method_no_return_type("__destruct", ce, fptr, error_type);
20642075
} else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
20652076
zend_check_magic_method_args(0, "__clone", ce, fptr, error_type);
20662077
zend_check_magic_method_non_static("__clone", ce, fptr, error_type);
2078+
zend_check_magic_method_no_return_type("__clone", ce, fptr, error_type);
20672079
} else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
20682080
zend_check_magic_method_args(1, "__get", ce, fptr, error_type);
20692081
zend_check_magic_method_non_static("__get", ce, fptr, error_type);
@@ -2359,17 +2371,6 @@ ZEND_API int zend_register_functions(zend_class_entry *scope, const zend_functio
23592371
if (ctor) {
23602372
ctor->common.fn_flags |= ZEND_ACC_CTOR;
23612373
}
2362-
if (ctor && (ctor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
2363-
zend_error_noreturn(E_CORE_ERROR, "Constructor %s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(ctor->common.function_name));
2364-
}
2365-
2366-
if (dtor && (dtor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
2367-
zend_error_noreturn(E_CORE_ERROR, "Destructor %s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(dtor->common.function_name));
2368-
}
2369-
2370-
if (clone && (clone->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
2371-
zend_error_noreturn(E_CORE_ERROR, "%s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(clone->common.function_name));
2372-
}
23732374
efree((char*)lc_class_name);
23742375
}
23752376
return SUCCESS;

Zend/zend_compile.c

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7161,25 +7161,6 @@ void zend_compile_class_decl(znode *result, zend_ast *ast, zend_bool toplevel) /
71617161

71627162
if (ce->constructor) {
71637163
ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
7164-
if (ce->constructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
7165-
zend_error_noreturn(E_COMPILE_ERROR,
7166-
"Constructor %s::%s() cannot declare a return type",
7167-
ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
7168-
}
7169-
}
7170-
if (ce->destructor) {
7171-
if (ce->destructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
7172-
zend_error_noreturn(E_COMPILE_ERROR,
7173-
"Destructor %s::%s() cannot declare a return type",
7174-
ZSTR_VAL(ce->name), ZSTR_VAL(ce->destructor->common.function_name));
7175-
}
7176-
}
7177-
if (ce->clone) {
7178-
if (ce->clone->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
7179-
zend_error_noreturn(E_COMPILE_ERROR,
7180-
"Clone method %s::%s() cannot declare a return type",
7181-
ZSTR_VAL(ce->name), ZSTR_VAL(ce->clone->common.function_name));
7182-
}
71837164
}
71847165

71857166
if ((ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) == ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {

0 commit comments

Comments
 (0)