Skip to content

Commit

Permalink
Unify magic method return type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Jul 20, 2020
1 parent fbbcf82 commit 312fe2b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Zend/tests/return_types/014.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ class Foo {
function __construct() : Foo {}
}
--EXPECTF--
Fatal error: Constructor %s::%s() cannot declare a return type in %s on line %d
Fatal error: Method Foo::__construct() cannot declare a return type in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/return_types/018.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ class Foo {
function __destruct() : Foo {}
}
--EXPECTF--
Fatal error: Destructor %s::%s() cannot declare a return type in %s on line %d
Fatal error: Method Foo::__destruct() cannot declare a return type in %s on line %d
23 changes: 12 additions & 11 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -2049,6 +2049,15 @@ static void zend_check_magic_method_static(
}
}

static void zend_check_magic_method_no_return_type(
const char *name, const zend_class_entry *ce, const zend_function *fptr, int error_type)
{
if (fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
zend_error_noreturn(error_type, "Method %s::%s() cannot declare a return type",
ZSTR_VAL(ce->name), name);
}
}

ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce, const zend_function *fptr, zend_string *lcname, int error_type) /* {{{ */
{
if (ZSTR_VAL(fptr->common.function_name)[0] != '_'
Expand All @@ -2058,12 +2067,15 @@ ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce,

if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
zend_check_magic_method_non_static("__construct", ce, fptr, error_type);
zend_check_magic_method_no_return_type("__construct", ce, fptr, error_type);
} else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
zend_check_magic_method_args(0, "__destruct", ce, fptr, error_type);
zend_check_magic_method_non_static("__destruct", ce, fptr, error_type);
zend_check_magic_method_no_return_type("__destruct", ce, fptr, error_type);
} else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
zend_check_magic_method_args(0, "__clone", ce, fptr, error_type);
zend_check_magic_method_non_static("__clone", ce, fptr, error_type);
zend_check_magic_method_no_return_type("__clone", ce, fptr, error_type);
} else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
zend_check_magic_method_args(1, "__get", ce, fptr, error_type);
zend_check_magic_method_non_static("__get", ce, fptr, error_type);
Expand Down Expand Up @@ -2359,17 +2371,6 @@ ZEND_API int zend_register_functions(zend_class_entry *scope, const zend_functio
if (ctor) {
ctor->common.fn_flags |= ZEND_ACC_CTOR;
}
if (ctor && (ctor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
zend_error_noreturn(E_CORE_ERROR, "Constructor %s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(ctor->common.function_name));
}

if (dtor && (dtor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
zend_error_noreturn(E_CORE_ERROR, "Destructor %s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(dtor->common.function_name));
}

if (clone && (clone->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
zend_error_noreturn(E_CORE_ERROR, "%s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(clone->common.function_name));
}
efree((char*)lc_class_name);
}
return SUCCESS;
Expand Down
19 changes: 0 additions & 19 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -7161,25 +7161,6 @@ void zend_compile_class_decl(znode *result, zend_ast *ast, zend_bool toplevel) /

if (ce->constructor) {
ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
if (ce->constructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
zend_error_noreturn(E_COMPILE_ERROR,
"Constructor %s::%s() cannot declare a return type",
ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
}
}
if (ce->destructor) {
if (ce->destructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
zend_error_noreturn(E_COMPILE_ERROR,
"Destructor %s::%s() cannot declare a return type",
ZSTR_VAL(ce->name), ZSTR_VAL(ce->destructor->common.function_name));
}
}
if (ce->clone) {
if (ce->clone->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
zend_error_noreturn(E_COMPILE_ERROR,
"Clone method %s::%s() cannot declare a return type",
ZSTR_VAL(ce->name), ZSTR_VAL(ce->clone->common.function_name));
}
}

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) {
Expand Down

0 comments on commit 312fe2b

Please sign in to comment.