Skip to content

Commit e4883e9

Browse files
committed
Cast objects to numeric
1 parent 648bb68 commit e4883e9

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
@@ -5931,6 +5931,21 @@ static void php_array_binop(INTERNAL_FUNCTION_PARAMETERS, const char *op_name, b
59315931

59325932
ZVAL_LONG(return_value, initial);
59335933
ZEND_HASH_FOREACH_VAL(input, entry) {
5934+
/* For objects we try to cast them to a numeric type */
5935+
if (Z_TYPE_P(entry) == IS_OBJECT) {
5936+
zval dst;
5937+
zend_result status = Z_OBJ_HT_P(entry)->cast_object(Z_OBJ_P(entry), &dst, _IS_NUMBER);
5938+
5939+
/* Do not type error for BC */
5940+
if (status == FAILURE || (Z_TYPE(dst) != IS_LONG && Z_TYPE(dst) != IS_DOUBLE)) {
5941+
php_error_docref(NULL, E_WARNING, "%s is not supported on type %s",
5942+
op_name, zend_zval_type_name(entry));
5943+
continue;
5944+
}
5945+
op(return_value, return_value, &dst);
5946+
continue;
5947+
}
5948+
59345949
zend_result status = op(return_value, return_value, entry);
59355950
if (status == FAILURE) {
59365951
ZEND_ASSERT(EG(exception));
@@ -5951,23 +5966,6 @@ static void php_array_binop(INTERNAL_FUNCTION_PARAMETERS, const char *op_name, b
59515966
op_name, zend_zval_type_name(entry));
59525967
}
59535968
} ZEND_HASH_FOREACH_END();
5954-
5955-
/* Traversal of array encountered objects that support multiplication */
5956-
if (Z_TYPE_P(return_value) == IS_OBJECT) {
5957-
/* Cannot use convert_scalar_to_number() as we don't know if the cast succeeded */
5958-
zval dst;
5959-
zend_result status = Z_OBJ_HT_P(return_value)->cast_object(Z_OBJ_P(return_value), &dst, _IS_NUMBER);
5960-
5961-
/* Do not type error for BC */
5962-
if (status == FAILURE || (Z_TYPE(dst) != IS_LONG && Z_TYPE(dst) != IS_DOUBLE)) {
5963-
zend_error(E_WARNING, "Object of class %s could not be converted to int|float",
5964-
ZSTR_VAL(Z_OBJCE_P(return_value)->name));
5965-
zval_ptr_dtor(return_value);
5966-
RETURN_LONG(initial);
5967-
}
5968-
zval_ptr_dtor(return_value);
5969-
RETURN_COPY_VALUE(&dst);
5970-
}
59715969
}
59725970

59735971
/* {{{ 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)