Skip to content

Commit b2cf406

Browse files
committed
Cast objects to numeric
1 parent 8efc087 commit b2cf406

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

ext/standard/array.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5919,6 +5919,21 @@ static void php_array_binop(INTERNAL_FUNCTION_PARAMETERS, const char *op_name, b
59195919

59205920
ZVAL_LONG(return_value, initial);
59215921
ZEND_HASH_FOREACH_VAL(input, entry) {
5922+
/* For objects we try to cast them to a numeric type */
5923+
if (Z_TYPE_P(entry) == IS_OBJECT) {
5924+
zval dst;
5925+
zend_result status = Z_OBJ_HT_P(entry)->cast_object(Z_OBJ_P(entry), &dst, _IS_NUMBER);
5926+
5927+
/* Do not type error for BC */
5928+
if (status == FAILURE || (Z_TYPE(dst) != IS_LONG && Z_TYPE(dst) != IS_DOUBLE)) {
5929+
php_error_docref(NULL, E_WARNING, "%s is not supported on type %s",
5930+
op_name, zend_zval_type_name(entry));
5931+
continue;
5932+
}
5933+
op(return_value, return_value, &dst);
5934+
continue;
5935+
}
5936+
59225937
zend_result status = op(return_value, return_value, entry);
59235938
if (status == FAILURE) {
59245939
ZEND_ASSERT(EG(exception));
@@ -5939,23 +5954,6 @@ static void php_array_binop(INTERNAL_FUNCTION_PARAMETERS, const char *op_name, b
59395954
op_name, zend_zval_type_name(entry));
59405955
}
59415956
} ZEND_HASH_FOREACH_END();
5942-
5943-
/* Traversal of array encountered objects that support multiplication */
5944-
if (Z_TYPE_P(return_value) == IS_OBJECT) {
5945-
/* Cannot use convert_scalar_to_number() as we don't know if the cast succeeded */
5946-
zval dst;
5947-
zend_result status = Z_OBJ_HT_P(return_value)->cast_object(Z_OBJ_P(return_value), &dst, _IS_NUMBER);
5948-
5949-
/* Do not type error for BC */
5950-
if (status == FAILURE || (Z_TYPE(dst) != IS_LONG && Z_TYPE(dst) != IS_DOUBLE)) {
5951-
zend_error(E_WARNING, "Object of class %s could not be converted to int|float",
5952-
ZSTR_VAL(Z_OBJCE_P(return_value)->name));
5953-
zval_ptr_dtor(return_value);
5954-
RETURN_LONG(initial);
5955-
}
5956-
zval_ptr_dtor(return_value);
5957-
RETURN_COPY_VALUE(&dst);
5958-
}
59595957
}
59605958

59615959
/* {{{ Returns the sum of the array entries */

ext/standard/tests/array/array_product_objects_operation_no_cast.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ var_dump(array_reduce($input, fn($carry, $value) => $carry * $value, 1));
1515
--EXPECTF--
1616
array_product() version:
1717

18-
Warning: Object of class DoOperationNoCast could not be converted to int|float in %s on line %d
18+
Warning: array_product(): Multiplication is not supported on type DoOperationNoCast in %s on line %d
19+
20+
Warning: array_product(): Multiplication is not supported on type DoOperationNoCast in %s on line %d
21+
22+
Warning: array_product(): Multiplication is not supported on type DoOperationNoCast in %s on line %d
1923
int(1)
2024
array_reduce() version:
2125
object(DoOperationNoCast)#5 (1) {

ext/standard/tests/array/array_sum_objects_operation_no_cast.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ var_dump(array_reduce($input, fn($carry, $value) => $carry + $value, 0));
1515
--EXPECTF--
1616
array_sum() version:
1717

18-
Warning: Object of class DoOperationNoCast could not be converted to int|float in %s on line %d
18+
Warning: array_sum(): Addition is not supported on type DoOperationNoCast in %s on line %d
19+
20+
Warning: array_sum(): Addition is not supported on type DoOperationNoCast in %s on line %d
1921
int(0)
2022
array_reduce() version:
2123
object(DoOperationNoCast)#5 (1) {

ext/standard/tests/array/array_sum_objects_operation_no_cast_FFI.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ var_dump(array_reduce($input, fn($carry, $value) => $carry + $value, 0));
2020
--EXPECTF--
2121
array_sum() version:
2222

23-
Warning: Object of class FFI\CData could not be converted to int|float in %s on line %d
24-
int(0)
23+
Warning: array_sum(): Addition is not supported on type FFI\CData in %s on line %d
24+
int(1)
2525
array_reduce() version:
2626
object(FFI\CData:int32_t*)#4 (1) {
2727
[0]=>

0 commit comments

Comments
 (0)