Skip to content

Commit 0e24a7c

Browse files
author
Derick Rethans
committed
- Strip out the typehint *checks* only. They are still parsed, and they are
still accessible through the reflection API.
1 parent defd00a commit 0e24a7c

14 files changed

+15
-291
lines changed

NEWS

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
FR #51815. (Pierrick)
5757
- Added iterator support in MySQLi. mysqli_result implements Traversable.
5858
(Andrey, Johannes)
59-
- Added scalar typehinting. (Ilia, Derick)
59+
- Added scalar typehints to the parser and the reflection API. (Ilia, Derick)
6060
- Added support for JSON_NUMERIC_CHECK option in json_encode() that converts
6161
numeric strings to integers. (Ilia)
6262
- Added support for object references in recursive serialize() calls. FR #36424.

Zend/tests/hint/param_type_hint_001.phpt

-38
This file was deleted.

Zend/tests/hint/param_type_hint_003.phpt

-16
This file was deleted.

Zend/tests/hint/param_type_hint_004.phpt

-16
This file was deleted.

Zend/tests/hint/param_type_hint_005.phpt

-23
This file was deleted.

Zend/tests/hint/param_type_hint_011.phpt

-15
This file was deleted.

Zend/tests/hint/param_type_hint_012.phpt

-18
This file was deleted.

Zend/tests/hint/param_type_hint_013.phpt

-16
This file was deleted.

Zend/tests/hint/param_type_hint_014.phpt

-16
This file was deleted.

Zend/tests/hint/param_type_hint_015.phpt

-19
This file was deleted.

Zend/tests/hint/param_type_hint_019.phpt

-37
This file was deleted.

Zend/tests/hint/param_type_hint_021.phpt

-46
This file was deleted.

Zend/zend_execute.c

+11-30
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ static inline void make_real_object(zval **object_ptr TSRMLS_DC)
577577
}
578578
}
579579

580-
static inline char * zend_verify_arg_class_kind(const zend_arg_info *cur_arg_info, ulong fetch_type, const char **class_name, zend_class_entry **pce TSRMLS_DC)
580+
ZEND_API char * zend_verify_arg_class_kind(const zend_arg_info *cur_arg_info, ulong fetch_type, const char **class_name, zend_class_entry **pce TSRMLS_DC)
581581
{
582582
*pce = zend_fetch_class(cur_arg_info->class_name, cur_arg_info->class_name_len, (fetch_type | ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_NO_AUTOLOAD) TSRMLS_CC);
583583

@@ -589,7 +589,7 @@ static inline char * zend_verify_arg_class_kind(const zend_arg_info *cur_arg_inf
589589
}
590590
}
591591

592-
static inline int zend_verify_arg_error(const zend_function *zf, zend_uint arg_num, const char *need_msg, const char *need_kind, const char *given_msg, char *given_kind TSRMLS_DC)
592+
ZEND_API int zend_verify_arg_error(int error_type, const zend_function *zf, zend_uint arg_num, const char *need_msg, const char *need_kind, const char *given_msg, char *given_kind TSRMLS_DC)
593593
{
594594
zend_execute_data *ptr = EG(current_execute_data)->prev_execute_data;
595595
char *fname = zf->common.function_name;
@@ -605,9 +605,9 @@ static inline int zend_verify_arg_error(const zend_function *zf, zend_uint arg_n
605605
}
606606

607607
if (ptr && ptr->op_array) {
608-
zend_error(E_RECOVERABLE_ERROR, "Argument %d passed to %s%s%s() must %s%s, %s%s given, called in %s on line %d and defined", arg_num, fclass, fsep, fname, need_msg, need_kind, given_msg, given_kind, ptr->op_array->filename, ptr->opline->lineno);
608+
zend_error(error_type, "Argument %d passed to %s%s%s() must %s%s, %s%s given, called in %s on line %d and defined", arg_num, fclass, fsep, fname, need_msg, need_kind, given_msg, given_kind, ptr->op_array->filename, ptr->opline->lineno);
609609
} else {
610-
zend_error(E_RECOVERABLE_ERROR, "Argument %d passed to %s%s%s() must %s%s, %s%s given", arg_num, fclass, fsep, fname, need_msg, need_kind, given_msg, given_kind);
610+
zend_error(error_type, "Argument %d passed to %s%s%s() must %s%s, %s%s given", arg_num, fclass, fsep, fname, need_msg, need_kind, given_msg, given_kind);
611611
}
612612
return 0;
613613
}
@@ -630,44 +630,25 @@ static inline int zend_verify_arg_type(zend_function *zf, zend_uint arg_num, zva
630630

631631
if (!arg) {
632632
need_msg = zend_verify_arg_class_kind(cur_arg_info, fetch_type, &class_name, &ce TSRMLS_CC);
633-
return zend_verify_arg_error(zf, arg_num, need_msg, class_name, "none", "" TSRMLS_CC);
633+
return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, need_msg, class_name, "none", "" TSRMLS_CC);
634634
}
635635
if (Z_TYPE_P(arg) == IS_OBJECT) {
636636
need_msg = zend_verify_arg_class_kind(cur_arg_info, fetch_type, &class_name, &ce TSRMLS_CC);
637637
if (!ce || !instanceof_function(Z_OBJCE_P(arg), ce TSRMLS_CC)) {
638-
return zend_verify_arg_error(zf, arg_num, need_msg, class_name, "instance of ", Z_OBJCE_P(arg)->name TSRMLS_CC);
638+
return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, need_msg, class_name, "instance of ", Z_OBJCE_P(arg)->name TSRMLS_CC);
639639
}
640640
} else if (Z_TYPE_P(arg) != IS_NULL || !cur_arg_info->allow_null) {
641641
need_msg = zend_verify_arg_class_kind(cur_arg_info, fetch_type, &class_name, &ce TSRMLS_CC);
642-
return zend_verify_arg_error(zf, arg_num, need_msg, class_name, zend_zval_type_name(arg), "" TSRMLS_CC);
642+
return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, need_msg, class_name, zend_zval_type_name(arg), "" TSRMLS_CC);
643643
}
644-
} else if (cur_arg_info->type_hint) {
644+
} else if (cur_arg_info->type_hint && cur_arg_info->type_hint == IS_ARRAY) {
645645
if (!arg) {
646-
return zend_verify_arg_error(zf, arg_num, "be of the type ", zend_get_type_by_const(cur_arg_info->type_hint), "none", "" TSRMLS_CC);
646+
return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be of the type array", "", "none", "" TSRMLS_CC);
647647
}
648648

649-
/* existing type already matches the hint or forced type */
650-
if (Z_TYPE_P(arg) == cur_arg_info->type_hint) {
651-
return 1;
649+
if (Z_TYPE_P(arg) != IS_ARRAY && (Z_TYPE_P(arg) != IS_NULL || !cur_arg_info->allow_null)) {
650+
return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be of the type array", "", zend_zval_type_name(arg), "" TSRMLS_CC);
652651
}
653-
654-
/* NULL type given, check if parameter is optional */
655-
if (Z_TYPE_P(arg) == IS_NULL && cur_arg_info->allow_null) {
656-
return 1;
657-
}
658-
659-
if (cur_arg_info->type_hint == IS_SCALAR && Z_TYPE_P(arg) != IS_NULL && Z_TYPE_P(arg) != IS_ARRAY && Z_TYPE_P(arg) != IS_OBJECT && Z_TYPE_P(arg) != IS_RESOURCE) {
660-
return 1;
661-
}
662-
663-
if (cur_arg_info->type_hint == IS_NUMERIC && (
664-
(Z_TYPE_P(arg) == IS_LONG || Z_TYPE_P(arg) == IS_DOUBLE)
665-
|| (Z_TYPE_P(arg) == IS_STRING && is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, NULL, 0))
666-
)) {
667-
return 1;
668-
}
669-
670-
return zend_verify_arg_error(zf, arg_num, "be of the type ", zend_get_type_by_const(cur_arg_info->type_hint), "", zend_zval_type_name(arg) TSRMLS_CC);
671652
}
672653
return 1;
673654
}

Zend/zend_execute.h

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ ZEND_API int zend_eval_stringl(char *str, int str_len, zval *retval_ptr, char *s
7777
ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC);
7878
ZEND_API int zend_eval_stringl_ex(char *str, int str_len, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC);
7979

80+
ZEND_API char * zend_verify_arg_class_kind(const zend_arg_info *cur_arg_info, ulong fetch_type, const char **class_name, zend_class_entry **pce TSRMLS_DC);
81+
ZEND_API int zend_verify_arg_error(int error_type, const zend_function *zf, zend_uint arg_num, const char *need_msg, const char *need_kind, const char *given_msg, char *given_kind TSRMLS_DC);
82+
8083
static zend_always_inline void i_zval_ptr_dtor(zval *zval_ptr ZEND_FILE_LINE_DC)
8184
{
8285
if (!Z_DELREF_P(zval_ptr)) {

0 commit comments

Comments
 (0)