Add string return type to __toString() of internal classes

Same as with userland classes, automatically add a string return
type to __toString() methods in internal classes, so the signature
is compatible with Stringable.
This commit is contained in:
Nikita Popov 2021-11-09 10:17:26 +01:00
parent 535a0553e8
commit a551b08307
3 changed files with 20 additions and 4 deletions

View File

@ -2311,6 +2311,9 @@ ZEND_API void zend_add_magic_method(zend_class_entry *ce, zend_function *fptr, z
}
}
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arg_info_toString, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()
/* registers all functions in *library_functions in the function hash */
ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type) /* {{{ */
{
@ -2393,6 +2396,16 @@ ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend
internal_function->required_num_args = 0;
}
/* If not specified, add __toString() return type for compatibility with Stringable
* interface. */
if (scope && zend_string_equals_literal_ci(internal_function->function_name, "__tostring") &&
!(internal_function->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
internal_function->arg_info = (zend_internal_arg_info *) arg_info_toString + 1;
internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
internal_function->num_args = internal_function->required_num_args = 0;
}
zend_set_function_arg_flags((zend_function*)internal_function);
if (ptr->flags & ZEND_ACC_ABSTRACT) {
if (scope) {

View File

@ -12,8 +12,11 @@ interface _ZendTestInterface
class _ZendTestClass {
public static function is_object(): int {}
/** @deprecated */
public function __toString(): string {}
/**
* @deprecated
* @return string
*/
public function __toString() {}
public function returnsStatic(): static {}
}

View File

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 49b9abbc5ce826e749861fd511e98f1add001368 */
* Stub hash: a6755b9cb5c4625e91d69f17c3aa702a189ba01c */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()
@ -68,7 +68,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class__ZendTestClass_is_object, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class__ZendTestClass___toString, 0, 0, IS_STRING, 0)
ZEND_BEGIN_ARG_INFO_EX(arginfo_class__ZendTestClass___toString, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class__ZendTestClass_returnsStatic, 0, 0, IS_STATIC, 0)