mirror of
https://github.com/php/php-src.git
synced 2025-08-15 21:48:51 +02:00
Remove code duplication in zend_std_compare_objects (#8710)
This commit is contained in:
parent
418f7211f7
commit
120b4f7ae6
1 changed files with 29 additions and 41 deletions
|
@ -1616,51 +1616,39 @@ ZEND_API int zend_std_compare_objects(zval *o1, zval *o2) /* {{{ */
|
|||
|
||||
if (Z_TYPE_P(o1) != Z_TYPE_P(o2)) {
|
||||
/* Object and non-object */
|
||||
zval *object;
|
||||
zval *value;
|
||||
zval casted;
|
||||
bool object_lhs;
|
||||
if (Z_TYPE_P(o1) == IS_OBJECT) {
|
||||
ZEND_ASSERT(Z_TYPE_P(o2) != IS_OBJECT);
|
||||
zend_uchar target_type = (Z_TYPE_P(o2) == IS_FALSE || Z_TYPE_P(o2) == IS_TRUE)
|
||||
? _IS_BOOL : Z_TYPE_P(o2);
|
||||
if (Z_OBJ_HT_P(o1)->cast_object(Z_OBJ_P(o1), &casted, target_type) == FAILURE) {
|
||||
// TODO: Less crazy.
|
||||
if (target_type == IS_LONG || target_type == IS_DOUBLE) {
|
||||
zend_error(E_NOTICE, "Object of class %s could not be converted to %s",
|
||||
ZSTR_VAL(Z_OBJCE_P(o1)->name), zend_get_type_by_const(target_type));
|
||||
if (target_type == IS_LONG) {
|
||||
ZVAL_LONG(&casted, 1);
|
||||
} else {
|
||||
ZVAL_DOUBLE(&casted, 1.0);
|
||||
}
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
int ret = zend_compare(&casted, o2);
|
||||
zval_ptr_dtor(&casted);
|
||||
return ret;
|
||||
object = o1;
|
||||
value = o2;
|
||||
object_lhs = true;
|
||||
} else {
|
||||
ZEND_ASSERT(Z_TYPE_P(o2) == IS_OBJECT);
|
||||
zend_uchar target_type = (Z_TYPE_P(o1) == IS_FALSE || Z_TYPE_P(o1) == IS_TRUE)
|
||||
? _IS_BOOL : Z_TYPE_P(o1);
|
||||
if (Z_OBJ_HT_P(o2)->cast_object(Z_OBJ_P(o2), &casted, target_type) == FAILURE) {
|
||||
// TODO: Less crazy.
|
||||
if (target_type == IS_LONG || target_type == IS_DOUBLE) {
|
||||
zend_error(E_NOTICE, "Object of class %s could not be converted to %s",
|
||||
ZSTR_VAL(Z_OBJCE_P(o2)->name), zend_get_type_by_const(target_type));
|
||||
if (target_type == IS_LONG) {
|
||||
ZVAL_LONG(&casted, 1);
|
||||
} else {
|
||||
ZVAL_DOUBLE(&casted, 1.0);
|
||||
}
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
int ret = zend_compare(o1, &casted);
|
||||
zval_ptr_dtor(&casted);
|
||||
return ret;
|
||||
object = o2;
|
||||
value = o1;
|
||||
object_lhs = false;
|
||||
}
|
||||
return ZEND_UNCOMPARABLE;
|
||||
ZEND_ASSERT(Z_TYPE_P(value) != IS_OBJECT);
|
||||
zend_uchar target_type = (Z_TYPE_P(value) == IS_FALSE || Z_TYPE_P(value) == IS_TRUE)
|
||||
? _IS_BOOL : Z_TYPE_P(value);
|
||||
if (Z_OBJ_HT_P(object)->cast_object(Z_OBJ_P(object), &casted, target_type) == FAILURE) {
|
||||
// TODO: Less crazy.
|
||||
if (target_type == IS_LONG || target_type == IS_DOUBLE) {
|
||||
zend_error(E_NOTICE, "Object of class %s could not be converted to %s",
|
||||
ZSTR_VAL(Z_OBJCE_P(object)->name), zend_get_type_by_const(target_type));
|
||||
if (target_type == IS_LONG) {
|
||||
ZVAL_LONG(&casted, 1);
|
||||
} else {
|
||||
ZVAL_DOUBLE(&casted, 1.0);
|
||||
}
|
||||
} else {
|
||||
return object_lhs ? 1 : -1;
|
||||
}
|
||||
}
|
||||
int ret = object_lhs ? zend_compare(&casted, value) : zend_compare(value, &casted);
|
||||
zval_ptr_dtor(&casted);
|
||||
return ret;
|
||||
}
|
||||
|
||||
zobj1 = Z_OBJ_P(o1);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue