Change "Cannot redeclare class X" into "Cannot redeclare class/interface/trait X", meaning that the following:

C:\> php -r "trait A { } trait A { }"

Will now properly print "Cannot redeclare trait A" instead of "Cannot redeclare class A" to make error messages a tiny bit clearer. Admittedly, a better solution can most likely be made by actually telling what the colliding object is a type of.

Internally this adds a new function:
 zend_get_object_type()
This commit is contained in:
Kalle Sommer Nielsen 2015-03-25 06:31:11 +01:00
parent a3d2f9b600
commit 927d53fda4
7 changed files with 22 additions and 8 deletions

View File

@ -12,4 +12,4 @@ class_alias('foo', 'test');
?>
--EXPECTF--
Warning: Cannot redeclare class test in %s on line %d
Warning: Cannot redeclare class test in %s on line %d

View File

@ -11,4 +11,4 @@ class b { }
?>
--EXPECTF--
Warning: Cannot redeclare class b in %s on line %d
Warning: Cannot redeclare interface b in %s on line %d

View File

@ -4137,6 +4137,18 @@ ZEND_API void zend_ctor_make_null(zend_execute_data *execute_data) /* {{{ */
}
/* }}} */
ZEND_API char *zend_get_object_type(zend_class_entry *ce) /* {{{ */
{
if(ce->ce_flags & ZEND_ACC_TRAIT) {
return "trait";
} else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
return "interface";
} else {
return "class";
}
}
/* }}} */
/*
* Local variables:
* tab-width: 4

View File

@ -540,6 +540,8 @@ ZEND_API zend_string *zend_resolve_method_name(zend_class_entry *ce, zend_functi
ZEND_API void zend_ctor_make_null(zend_execute_data *execute_data);
ZEND_API char *zend_get_object_type(zend_class_entry *ce);
#define add_method(arg, key, method) add_assoc_function((arg), (key), (method))
ZEND_API ZEND_FUNCTION(display_disabled_function);

View File

@ -1536,7 +1536,7 @@ ZEND_FUNCTION(class_alias)
if (zend_register_class_alias_ex(alias_name, alias_name_len, ce) == SUCCESS) {
RETURN_TRUE;
} else {
zend_error(E_WARNING, "Cannot redeclare class %s", alias_name);
zend_error(E_WARNING, "Cannot redeclare %s %s", zend_get_object_type(ce), alias_name);
RETURN_FALSE;
}
} else {

View File

@ -1002,7 +1002,7 @@ ZEND_API zend_class_entry *do_bind_class(const zend_op_array* op_array, const ze
* so we shut up about it. This allows the if (!defined('FOO')) { return; }
* approach to work.
*/
zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s", ce->name->val);
zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s %s", zend_get_object_type(ce), ce->name->val);
}
return NULL;
} else {
@ -1036,13 +1036,13 @@ ZEND_API zend_class_entry *do_bind_inherited_class(const zend_op_array *op_array
* so we shut up about it. This allows the if (!defined('FOO')) { return; }
* approach to work.
*/
zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s", Z_STRVAL_P(op2));
zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s %s", zend_get_object_type(Z_OBJCE_P(op2)), Z_STRVAL_P(op2));
}
return NULL;
}
if (zend_hash_exists(class_table, Z_STR_P(op2))) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s", ce->name->val);
zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s %s", zend_get_object_type(ce), ce->name->val);
}
zend_do_inheritance(ce, parent_ce);
@ -1051,7 +1051,7 @@ ZEND_API zend_class_entry *do_bind_inherited_class(const zend_op_array *op_array
/* Register the derived class */
if (zend_hash_add_ptr(class_table, Z_STR_P(op2), ce) == NULL) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s", ce->name->val);
zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s %s", zend_get_object_type(ce), ce->name->val);
}
return ce;
}

View File

@ -755,7 +755,7 @@ failure:
CG(in_compilation) = 1;
zend_set_compiled_filename(ce1->info.user.filename);
CG(zend_lineno) = ce1->info.user.line_start;
zend_error(E_ERROR, "Cannot redeclare class %s", ce1->name->val);
zend_error(E_ERROR, "Cannot redeclare %s %s", zend_get_object_type(ce1), ce1->name->val);
}
#ifdef __SSE2__