From 0e24a7c400bc1d1803ce04bea3d8a07e2b997f88 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Tue, 19 Oct 2010 10:42:38 +0000 Subject: [PATCH] - Strip out the typehint *checks* only. They are still parsed, and they are still accessible through the reflection API. --- NEWS | 2 +- Zend/tests/hint/param_type_hint_001.phpt | 38 -------------------- Zend/tests/hint/param_type_hint_003.phpt | 16 --------- Zend/tests/hint/param_type_hint_004.phpt | 16 --------- Zend/tests/hint/param_type_hint_005.phpt | 23 ------------ Zend/tests/hint/param_type_hint_011.phpt | 15 -------- Zend/tests/hint/param_type_hint_012.phpt | 18 ---------- Zend/tests/hint/param_type_hint_013.phpt | 16 --------- Zend/tests/hint/param_type_hint_014.phpt | 16 --------- Zend/tests/hint/param_type_hint_015.phpt | 19 ---------- Zend/tests/hint/param_type_hint_019.phpt | 37 ------------------- Zend/tests/hint/param_type_hint_021.phpt | 46 ------------------------ Zend/zend_execute.c | 41 ++++++--------------- Zend/zend_execute.h | 3 ++ 14 files changed, 15 insertions(+), 291 deletions(-) delete mode 100644 Zend/tests/hint/param_type_hint_001.phpt delete mode 100644 Zend/tests/hint/param_type_hint_003.phpt delete mode 100644 Zend/tests/hint/param_type_hint_004.phpt delete mode 100644 Zend/tests/hint/param_type_hint_005.phpt delete mode 100644 Zend/tests/hint/param_type_hint_011.phpt delete mode 100644 Zend/tests/hint/param_type_hint_012.phpt delete mode 100644 Zend/tests/hint/param_type_hint_013.phpt delete mode 100644 Zend/tests/hint/param_type_hint_014.phpt delete mode 100644 Zend/tests/hint/param_type_hint_015.phpt delete mode 100644 Zend/tests/hint/param_type_hint_019.phpt delete mode 100644 Zend/tests/hint/param_type_hint_021.phpt diff --git a/NEWS b/NEWS index 02f2cf4d087..2e21614ae52 100644 --- a/NEWS +++ b/NEWS @@ -56,7 +56,7 @@ FR #51815. (Pierrick) - Added iterator support in MySQLi. mysqli_result implements Traversable. (Andrey, Johannes) -- Added scalar typehinting. (Ilia, Derick) +- Added scalar typehints to the parser and the reflection API. (Ilia, Derick) - Added support for JSON_NUMERIC_CHECK option in json_encode() that converts numeric strings to integers. (Ilia) - Added support for object references in recursive serialize() calls. FR #36424. diff --git a/Zend/tests/hint/param_type_hint_001.phpt b/Zend/tests/hint/param_type_hint_001.phpt deleted file mode 100644 index a85c58ca258..00000000000 --- a/Zend/tests/hint/param_type_hint_001.phpt +++ /dev/null @@ -1,38 +0,0 @@ ---TEST-- -Parameter type hint - Testing integer hint ---FILE-- - ---EXPECTF-- -Ok -Ok -Ok -Ok - -Catchable fatal error: Argument 2 passed to test_int() must be of the type integer, string given, called in %s on line %d and defined in %s on line %d ---UEXPECTF-- -Ok -Ok -Ok -Ok - -Catchable fatal error: Argument 2 passed to test_int() must be of the type integer, Unicode string given, called in %s on line %d and defined in %s on line %d diff --git a/Zend/tests/hint/param_type_hint_003.phpt b/Zend/tests/hint/param_type_hint_003.phpt deleted file mode 100644 index ba12dfa040f..00000000000 --- a/Zend/tests/hint/param_type_hint_003.phpt +++ /dev/null @@ -1,16 +0,0 @@ ---TEST-- -Parameter type hint - Wrong parameter for integer ---FILE-- - ---EXPECTF-- -Catchable fatal error: Argument 1 passed to test_int() must be of the type integer, string given, called in %s on line %d and defined in %s on line %d ---UEXPECTF-- -Catchable fatal error: Argument 1 passed to test_int() must be of the type integer, Unicode string given, called in %s on line %d and defined in %s on line %d diff --git a/Zend/tests/hint/param_type_hint_004.phpt b/Zend/tests/hint/param_type_hint_004.phpt deleted file mode 100644 index b63f0746759..00000000000 --- a/Zend/tests/hint/param_type_hint_004.phpt +++ /dev/null @@ -1,16 +0,0 @@ ---TEST-- -Parameter type hint - Wrong parameter for double ---FILE-- - ---EXPECTF-- -Catchable fatal error: Argument 1 passed to test_double() must be of the type double, string given, called in %s on line %d and defined in %s on line %d ---UEXPECTF-- -Catchable fatal error: Argument 1 passed to test_double() must be of the type double, Unicode string given, called in %s on line %d and defined in %s on line %d diff --git a/Zend/tests/hint/param_type_hint_005.phpt b/Zend/tests/hint/param_type_hint_005.phpt deleted file mode 100644 index a3083e36b20..00000000000 --- a/Zend/tests/hint/param_type_hint_005.phpt +++ /dev/null @@ -1,23 +0,0 @@ ---TEST-- -Parameter type hint - Wrong parameter for string ---FILE-- - ---EXPECTF-- -+1.1 --.1 -11213111112321312 - - -Catchable fatal error: Argument 1 passed to test_str() must be of the type string, null given, called in %s on line %d and defined in %s on line %d diff --git a/Zend/tests/hint/param_type_hint_011.phpt b/Zend/tests/hint/param_type_hint_011.phpt deleted file mode 100644 index d8c24b8a365..00000000000 --- a/Zend/tests/hint/param_type_hint_011.phpt +++ /dev/null @@ -1,15 +0,0 @@ ---TEST-- -Parameter type hint - Testing with undefined variable ---FILE-- - ---EXPECTF-- -Notice: Undefined variable: x in %s on line %d - -Catchable fatal error: Argument 1 passed to test() must be of the type string, null given, called in %s on line %d and defined in %s on line %d diff --git a/Zend/tests/hint/param_type_hint_012.phpt b/Zend/tests/hint/param_type_hint_012.phpt deleted file mode 100644 index d5eb90d3f25..00000000000 --- a/Zend/tests/hint/param_type_hint_012.phpt +++ /dev/null @@ -1,18 +0,0 @@ ---TEST-- -Parameter type hint - Testing with lambda function ---FILE-- - ---EXPECTF-- -int(2) - -Catchable fatal error: Argument 1 passed to __lambda_func() must be of the type integer, string given, called in %s on line %d and defined in %s(%d) : runtime-created function on line %d ---UEXPECTF-- -int(2) - -Catchable fatal error: Argument 1 passed to __lambda_func() must be of the type integer, Unicode string given, called in %s on line %d and defined in %s(%d) : runtime-created function on line %d diff --git a/Zend/tests/hint/param_type_hint_013.phpt b/Zend/tests/hint/param_type_hint_013.phpt deleted file mode 100644 index c32e739bd8b..00000000000 --- a/Zend/tests/hint/param_type_hint_013.phpt +++ /dev/null @@ -1,16 +0,0 @@ ---TEST-- -Parameter type hint - Testing with lambda function using 2 parameters ---FILE-- - ---EXPECTF-- -int(1) -int(2) - -Catchable fatal error: Argument 2 passed to __lambda_func() must be of the type string, null given, called in %s on line %d and defined in %s(%d) : runtime-created function on line %d diff --git a/Zend/tests/hint/param_type_hint_014.phpt b/Zend/tests/hint/param_type_hint_014.phpt deleted file mode 100644 index 0a2642b033e..00000000000 --- a/Zend/tests/hint/param_type_hint_014.phpt +++ /dev/null @@ -1,16 +0,0 @@ ---TEST-- -Parameter type hint - Testing in function inside function ---FILE-- - ---EXPECTF-- -Catchable fatal error: Argument 1 passed to b() must be of the type double, integer given, called in %s on line %d and defined in %s on line %d diff --git a/Zend/tests/hint/param_type_hint_015.phpt b/Zend/tests/hint/param_type_hint_015.phpt deleted file mode 100644 index 2cf1d6bc6e6..00000000000 --- a/Zend/tests/hint/param_type_hint_015.phpt +++ /dev/null @@ -1,19 +0,0 @@ ---TEST-- -Parameter type hint - Testing with call_user_func() ---FILE-- - ---EXPECTF-- -ok -ok - -Catchable fatal error: Argument 1 passed to test() must be of the type boolean, null given in %s on line %d diff --git a/Zend/tests/hint/param_type_hint_019.phpt b/Zend/tests/hint/param_type_hint_019.phpt deleted file mode 100644 index aa247cda9db..00000000000 --- a/Zend/tests/hint/param_type_hint_019.phpt +++ /dev/null @@ -1,37 +0,0 @@ ---TEST-- -Parameter type hint - scalar type hints ---FILE-- - ---EXPECTF-- -int(1) -bool(true) -string(%d) "Argument 1 passed to foo() must be of the type scalar, null given, called in %s on line %d and defined" -NULL -string(%d) "Argument 1 passed to foo() must be of the type scalar, array given, called in %s on line %d and defined" -array(1) { - [0]=> - int(1) -} -string(3) "foo" -float(111.222) -string(%d) "Argument 1 passed to foo() must be of the type scalar, object given, called in %s on line %d and defined" -object(stdClass)#1 (0) { -} -string(%d) "Argument 1 passed to foo() must be of the type scalar, resource given, called in %s on line %d and defined" -resource(4) of type (stream) diff --git a/Zend/tests/hint/param_type_hint_021.phpt b/Zend/tests/hint/param_type_hint_021.phpt deleted file mode 100644 index 81166230ece..00000000000 --- a/Zend/tests/hint/param_type_hint_021.phpt +++ /dev/null @@ -1,46 +0,0 @@ ---TEST-- -Parameter type hint - numeric type hints ---FILE-- - ---EXPECTF-- -int(1) -string(%d) "Argument 1 passed to foo() must be of the type numeric, boolean given, called in %s on line %d and defined" -bool(true) -string(%d) "Argument 1 passed to foo() must be of the type numeric, null given, called in %s on line %d and defined" -NULL -string(%d) "Argument 1 passed to foo() must be of the type numeric, array given, called in %s on line %d and defined" -array(1) { - [0]=> - int(1) -} -string(%d) "Argument 1 passed to foo() must be of the type numeric, string given, called in %s on line %d and defined" -string(3) "foo" -string(6) "123.33" -string(3) "123" -string(%d) "Argument 1 passed to foo() must be of the type numeric, string given, called in %s on line %d and defined" -string(0) "" -float(111.222) -string(%d) "Argument 1 passed to foo() must be of the type numeric, object given, called in %s on line %d and defined" -object(stdClass)#1 (0) { -} -string(%d) "Argument 1 passed to foo() must be of the type numeric, resource given, called in %s on line %d and defined" -resource(4) of type (stream) diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index 6c82b097360..5dc739140c8 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -577,7 +577,7 @@ static inline void make_real_object(zval **object_ptr TSRMLS_DC) } } -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) +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) { *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); @@ -589,7 +589,7 @@ static inline char * zend_verify_arg_class_kind(const zend_arg_info *cur_arg_inf } } -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) +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) { zend_execute_data *ptr = EG(current_execute_data)->prev_execute_data; 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 } if (ptr && ptr->op_array) { - 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); + 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); } else { - 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); + 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); } return 0; } @@ -630,44 +630,25 @@ static inline int zend_verify_arg_type(zend_function *zf, zend_uint arg_num, zva if (!arg) { need_msg = zend_verify_arg_class_kind(cur_arg_info, fetch_type, &class_name, &ce TSRMLS_CC); - return zend_verify_arg_error(zf, arg_num, need_msg, class_name, "none", "" TSRMLS_CC); + return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, need_msg, class_name, "none", "" TSRMLS_CC); } if (Z_TYPE_P(arg) == IS_OBJECT) { need_msg = zend_verify_arg_class_kind(cur_arg_info, fetch_type, &class_name, &ce TSRMLS_CC); if (!ce || !instanceof_function(Z_OBJCE_P(arg), ce TSRMLS_CC)) { - return zend_verify_arg_error(zf, arg_num, need_msg, class_name, "instance of ", Z_OBJCE_P(arg)->name TSRMLS_CC); + return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, need_msg, class_name, "instance of ", Z_OBJCE_P(arg)->name TSRMLS_CC); } } else if (Z_TYPE_P(arg) != IS_NULL || !cur_arg_info->allow_null) { need_msg = zend_verify_arg_class_kind(cur_arg_info, fetch_type, &class_name, &ce TSRMLS_CC); - return zend_verify_arg_error(zf, arg_num, need_msg, class_name, zend_zval_type_name(arg), "" TSRMLS_CC); + return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, need_msg, class_name, zend_zval_type_name(arg), "" TSRMLS_CC); } - } else if (cur_arg_info->type_hint) { + } else if (cur_arg_info->type_hint && cur_arg_info->type_hint == IS_ARRAY) { if (!arg) { - 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); + return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be of the type array", "", "none", "" TSRMLS_CC); } - /* existing type already matches the hint or forced type */ - if (Z_TYPE_P(arg) == cur_arg_info->type_hint) { - return 1; + if (Z_TYPE_P(arg) != IS_ARRAY && (Z_TYPE_P(arg) != IS_NULL || !cur_arg_info->allow_null)) { + return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be of the type array", "", zend_zval_type_name(arg), "" TSRMLS_CC); } - - /* NULL type given, check if parameter is optional */ - if (Z_TYPE_P(arg) == IS_NULL && cur_arg_info->allow_null) { - return 1; - } - - 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) { - return 1; - } - - if (cur_arg_info->type_hint == IS_NUMERIC && ( - (Z_TYPE_P(arg) == IS_LONG || Z_TYPE_P(arg) == IS_DOUBLE) - || (Z_TYPE_P(arg) == IS_STRING && is_numeric_string(Z_STRVAL_P(arg), Z_STRLEN_P(arg), NULL, NULL, 0)) - )) { - return 1; - } - - 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); } return 1; } diff --git a/Zend/zend_execute.h b/Zend/zend_execute.h index 1672ee63564..e5eb26a5d02 100644 --- a/Zend/zend_execute.h +++ b/Zend/zend_execute.h @@ -77,6 +77,9 @@ ZEND_API int zend_eval_stringl(char *str, int str_len, zval *retval_ptr, char *s ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC); ZEND_API int zend_eval_stringl_ex(char *str, int str_len, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC); +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); +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); + static zend_always_inline void i_zval_ptr_dtor(zval *zval_ptr ZEND_FILE_LINE_DC) { if (!Z_DELREF_P(zval_ptr)) {