- Strip out the typehint *checks* only. They are still parsed, and they are

still accessible through the reflection API.
This commit is contained in:
Derick Rethans 2010-10-19 10:42:38 +00:00
parent defd00ab01
commit 0e24a7c400
14 changed files with 15 additions and 291 deletions

2
NEWS
View File

@ -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.

View File

@ -1,38 +0,0 @@
--TEST--
Parameter type hint - Testing integer hint
--FILE--
<?php
function test_int($x, int $a = 1) {
}
test_int(1);
print "Ok\n";
test_int('1211');
print "Ok\n";
test_int(null);
print "Ok\n";
test_int(1, 1);
print "Ok\n";
test_int(null, '1211');
print "Ok\n";
?>
--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

View File

@ -1,16 +0,0 @@
--TEST--
Parameter type hint - Wrong parameter for integer
--FILE--
<?php
function test_int(int $a) {
echo $a, "\n";
}
test_int('+1');
?>
--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

View File

@ -1,16 +0,0 @@
--TEST--
Parameter type hint - Wrong parameter for double
--FILE--
<?php
function test_double(real $a) {
echo $a, "\n";
}
test_double('+1.1');
?>
--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

View File

@ -1,23 +0,0 @@
--TEST--
Parameter type hint - Wrong parameter for string
--FILE--
<?php
function test_str(string $a) {
echo $a, "\n";
}
test_str('+1.1');
test_str('-.1');
test_str('11213111112321312');
test_str('');
test_str(null);
?>
--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

View File

@ -1,15 +0,0 @@
--TEST--
Parameter type hint - Testing with undefined variable
--FILE--
<?php
function test(string $a) {
}
test($x);
?>
--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

View File

@ -1,18 +0,0 @@
--TEST--
Parameter type hint - Testing with lambda function
--FILE--
<?php
$test = create_function('integer $i', 'return $i + 1;');
var_dump($test(1));
var_dump($test('foo'));
?>
--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

View File

@ -1,16 +0,0 @@
--TEST--
Parameter type hint - Testing with lambda function using 2 parameters
--FILE--
<?php
$test = create_function('integer $i, string $j', 'return $i + $j;');
var_dump($test(1, 'foo'));
var_dump($test(1, '1'));
var_dump($test(1, NULL));
?>
--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

View File

@ -1,16 +0,0 @@
--TEST--
Parameter type hint - Testing in function inside function
--FILE--
<?php
function a() {
function b(real $c) {
}
return b(1);
}
a();
?>
--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

View File

@ -1,19 +0,0 @@
--TEST--
Parameter type hint - Testing with call_user_func()
--FILE--
<?php
function test(bool $b) {
print "ok\n";
}
call_user_func('test', true);
call_user_func('test', false);
call_user_func('test', NULL);
?>
--EXPECTF--
ok
ok
Catchable fatal error: Argument 1 passed to test() must be of the type boolean, null given in %s on line %d

View File

@ -1,37 +0,0 @@
--TEST--
Parameter type hint - scalar type hints
--FILE--
<?php
function x($error, $msg) { var_dump($msg); }
set_error_handler('x', E_RECOVERABLE_ERROR);
function foo(scalar $param) { var_dump($param); }
foo(1);
foo(true);
foo(NULL);
foo(array(1));
foo("foo");
foo(111.222);
foo(new Stdclass);
foo(tmpfile());
?>
--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)

View File

@ -1,46 +0,0 @@
--TEST--
Parameter type hint - numeric type hints
--FILE--
<?php
function x($error, $msg) { var_dump($msg); }
set_error_handler('x', E_RECOVERABLE_ERROR);
function foo(numeric $param) { var_dump($param); }
foo(1);
foo(true);
foo(NULL);
foo(array(1));
foo("foo");
foo("123.33");
foo("123");
foo("");
foo(111.222);
foo(new Stdclass);
foo(tmpfile());
?>
--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)

View File

@ -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;
}

View File

@ -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)) {