]> granicus.if.org Git - php/commitdiff
Unify magic method return type checks
authorNikita Popov <nikita.ppv@gmail.com>
Mon, 20 Jul 2020 08:51:48 +0000 (10:51 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 20 Jul 2020 08:51:48 +0000 (10:51 +0200)
Zend/tests/return_types/014.phpt
Zend/tests/return_types/018.phpt
Zend/zend_API.c
Zend/zend_compile.c

index 25bd79fb814b7a009fef946eb30fbc6b3baf900d..00f5e288bd5c4ac886b960721fe7020fad9a08e5 100644 (file)
@@ -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
index 8c745a1f65131a1805ea1e36b6ee159a0f4b1dc0..6c2f48c8c902cd5a6994f879af5178f4081a5d2d 100644 (file)
@@ -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
index a697c453ce264c195dbc6f6a91fb4d5da37fb7c4..734c792fd5999141a8100c4c7c90b12a20b399d2 100644 (file)
@@ -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] != '_'
@@ -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);
@@ -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;
index dbb37bbadbe6e8c4dda7a420ea69e63e0a876156..c2d0a117f23ef125c7026b2f0e6d38a50964ef59 100644 (file)
@@ -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) {