Skip to content

Commit 1134d8e

Browse files
committed
Resolve "static" to class name in error message
1 parent 99bcc11 commit 1134d8e

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

Zend/tests/type_declarations/static_type_return.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ object(B)#3 (0) {
7575

7676
object(A)#3 (0) {
7777
}
78-
Return value of A::test2() must be an instance of static, instance of A returned
78+
Return value of A::test2() must be an instance of B, instance of A returned
7979

8080
object(A)#3 (0) {
8181
}
@@ -84,7 +84,7 @@ object(C)#3 (0) {
8484

8585
object(A)#3 (0) {
8686
}
87-
Return value of A::test4() must be of type static|array, instance of A returned
87+
Return value of A::test4() must be of type B|array, instance of A returned
8888

8989
Return value of {closure}() must be an instance of static, instance of stdClass returned
9090
object(A)#1 (0) {

Zend/zend_compile.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,14 @@ zend_string *zend_type_to_string_resolved(zend_type type, zend_class_entry *scop
11971197

11981198
uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
11991199
if (type_mask & MAY_BE_STATIC) {
1200-
str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STATIC));
1200+
zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
1201+
if (scope) {
1202+
zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
1203+
if (called_scope) {
1204+
name = called_scope->name;
1205+
}
1206+
}
1207+
str = add_type_string(str, name);
12011208
}
12021209
if (type_mask & MAY_BE_CALLABLE) {
12031210
str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE));

Zend/zend_execute.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ static ZEND_COLD void zend_verify_type_error_common(
674674
}
675675

676676
if (is_union_type(arg_info->type)) {
677-
zend_string *type_str = zend_type_to_string(arg_info->type);
677+
zend_string *type_str = zend_type_to_string_resolved(arg_info->type, zf->common.scope);
678678
smart_str_appends(&str, "be of type ");
679679
smart_str_append(&str, type_str);
680680
zend_string_release(type_str);
@@ -714,9 +714,16 @@ static ZEND_COLD void zend_verify_type_error_common(
714714
case MAY_BE_ITERABLE:
715715
smart_str_appends(&str, "be iterable");
716716
break;
717-
case MAY_BE_STATIC:
718-
smart_str_appends(&str, "be an instance of static");
717+
case MAY_BE_STATIC: {
718+
zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
719+
smart_str_appends(&str, "be an instance of ");
720+
if (called_scope) {
721+
smart_str_append(&str, called_scope->name);
722+
} else {
723+
smart_str_appends(&str, "static");
724+
}
719725
break;
726+
}
720727
default:
721728
{
722729
/* Hack to print the type without null */

0 commit comments

Comments
 (0)