Skip to content

Instantly share code, notes, and snippets.

@nikic
Created March 4, 2012 17:51
Show Gist options
  • Save nikic/cf229eb87b3b393137e3 to your computer and use it in GitHub Desktop.
Save nikic/cf229eb87b3b393137e3 to your computer and use it in GitHub Desktop.
Index: Zend/zend_compile.c
===================================================================
--- Zend/zend_compile.c (revision 323880)
+++ Zend/zend_compile.c (working copy)
@@ -1878,27 +1878,66 @@
if (class_type->op_type != IS_UNUSED) {
cur_arg_info->allow_null = 0;
+ if (op == ZEND_RECV_INIT) {
+ if (Z_TYPE(initialization->u.constant) == IS_NULL || (Z_TYPE(initialization->u.constant) == IS_CONSTANT && !strcasecmp(Z_STRVAL(initialization->u.constant), "NULL"))) {
+ cur_arg_info->allow_null = 1;
+ } else {
+ switch (class_type->u.constant.type) {
+ case IS_ARRAY:
+ case IS_ARRAY_CAST:
+ if (Z_TYPE(initialization->u.constant) != IS_ARRAY && Z_TYPE(initialization->u.constant) != IS_CONSTANT_ARRAY) {
+ zend_error(E_COMPILE_ERROR, "Default value for parameters with array type hint can only be an array or NULL");
+ }
+ break;
+ case IS_LONG_CAST:
+ if (Z_TYPE(initialization->u.constant) != IS_LONG) {
+ zend_error(E_COMPILE_ERROR, "Default value for parameters with int type hint can only be INT or NULL");
+ }
+ break;
+ case IS_DOUBLE_CAST:
+ if (Z_TYPE(initialization->u.constant) != IS_DOUBLE) {
+ zend_error(E_COMPILE_ERROR, "Default value for parameters with float type hint can only be FLOAT or NULL");
+ }
+ break;
+ case IS_BOOL_CAST:
+ if (Z_TYPE(initialization->u.constant) != IS_BOOL) {
+ zend_error(E_COMPILE_ERROR, "Default value for parameters with bool type hint can only be BOOL or NULL");
+ }
+ break;
+ case IS_STRING_CAST:
+ if (Z_TYPE(initialization->u.constant) != IS_STRING) {
+ zend_error(E_COMPILE_ERROR, "Default value for parameters with string type hint can only be STRING or NULL");
+ }
+ break;
+ case IS_OBJECT_CAST:
+ zend_error(E_COMPILE_ERROR, "Default value for parameters with object type hint can only be NULL");
+ break;
+ case IS_CALLABLE:
+ zend_error(E_COMPILE_ERROR, "Default value for parameters with callable type hint can only be NULL");
+ break;
+ default:
+ zend_error(E_COMPILE_ERROR, "Default value for parameters with a class type hint can only be NULL");
+ }
+ }
+ }
- if (class_type->u.constant.type != IS_NULL) {
- if (class_type->u.constant.type == IS_ARRAY) {
- cur_arg_info->type_hint = IS_ARRAY;
- if (op == ZEND_RECV_INIT) {
- if (Z_TYPE(initialization->u.constant) == IS_NULL || (Z_TYPE(initialization->u.constant) == IS_CONSTANT && !strcasecmp(Z_STRVAL(initialization->u.constant), "NULL"))) {
- cur_arg_info->allow_null = 1;
- } else if (Z_TYPE(initialization->u.constant) != IS_ARRAY && Z_TYPE(initialization->u.constant) != IS_CONSTANT_ARRAY) {
- zend_error(E_COMPILE_ERROR, "Default value for parameters with array type hint can only be an array or NULL");
- }
+ switch (class_type->u.constant.type) {
+ case IS_ARRAY:
+ case IS_CALLABLE:
+ cur_arg_info->type_hint = class_type->u.constant.type;
+ break;
+ case IS_ARRAY_CAST:
+ case IS_LONG_CAST:
+ case IS_DOUBLE_CAST:
+ case IS_BOOL_CAST:
+ case IS_STRING_CAST:
+ case IS_OBJECT_CAST:
+ cur_arg_info->type_hint = class_type->u.constant.type;
+ if (pass_by_reference) {
+ zend_error(E_COMPILE_ERROR, "Parameters with %s type hint cannot be passed by reference", zend_get_type_by_const(class_type->u.constant.type));
}
- } else if (class_type->u.constant.type == IS_CALLABLE) {
- cur_arg_info->type_hint = IS_CALLABLE;
- if (op == ZEND_RECV_INIT) {
- if (Z_TYPE(initialization->u.constant) == IS_NULL || (Z_TYPE(initialization->u.constant) == IS_CONSTANT && !strcasecmp(Z_STRVAL(initialization->u.constant), "NULL"))) {
- cur_arg_info->allow_null = 1;
- } else {
- zend_error(E_COMPILE_ERROR, "Default value for parameters with callable type hint can only be NULL");
- }
- }
- } else {
+ break;
+ default:
cur_arg_info->type_hint = IS_OBJECT;
if (ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type(Z_STRVAL(class_type->u.constant), Z_STRLEN(class_type->u.constant))) {
zend_resolve_class_name(class_type, opline->extended_value, 1 TSRMLS_CC);
@@ -1906,14 +1945,6 @@
Z_STRVAL(class_type->u.constant) = (char*)zend_new_interned_string(class_type->u.constant.value.str.val, class_type->u.constant.value.str.len + 1, 1 TSRMLS_CC);
cur_arg_info->class_name = class_type->u.constant.value.str.val;
cur_arg_info->class_name_len = class_type->u.constant.value.str.len;
- if (op == ZEND_RECV_INIT) {
- if (Z_TYPE(initialization->u.constant) == IS_NULL || (Z_TYPE(initialization->u.constant) == IS_CONSTANT && !strcasecmp(Z_STRVAL(initialization->u.constant), "NULL"))) {
- cur_arg_info->allow_null = 1;
- } else {
- zend_error(E_COMPILE_ERROR, "Default value for parameters with a class type hint can only be NULL");
- }
- }
- }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment