@@ -5902,8 +5902,8 @@ PHP_FUNCTION(array_rand)
5902
5902
}
5903
5903
/* }}} */
5904
5904
5905
- /* {{{ Returns the sum of the array entries */
5906
- PHP_FUNCTION ( array_sum )
5905
+ /* Wrapper for array_sum and array_product */
5906
+ static void php_array_binop ( INTERNAL_FUNCTION_PARAMETERS , const char * op_name , binary_op_type op , zend_long initial )
5907
5907
{
5908
5908
HashTable * input ;
5909
5909
zval * entry ;
@@ -5914,29 +5914,29 @@ PHP_FUNCTION(array_sum)
5914
5914
5915
5915
if (zend_hash_num_elements (input ) == 0 ) {
5916
5916
php_error_docref (NULL , E_DEPRECATED , "Passing an empty array is deprecated" );
5917
- RETURN_LONG (0 );
5917
+ RETURN_LONG (initial );
5918
5918
}
5919
5919
5920
- ZVAL_LONG (return_value , 0 );
5920
+ ZVAL_LONG (return_value , initial );
5921
5921
ZEND_HASH_FOREACH_VAL (input , entry ) {
5922
- zend_result status = add_function (return_value , return_value , entry );
5922
+ zend_result status = op (return_value , return_value , entry );
5923
5923
if (status == FAILURE ) {
5924
5924
ZEND_ASSERT (EG (exception ));
5925
5925
zend_clear_exception ();
5926
5926
/* BC resources: previously resources were cast to int */
5927
5927
if (Z_TYPE_P (entry ) == IS_RESOURCE ) {
5928
5928
zval tmp ;
5929
5929
ZVAL_LONG (& tmp , Z_RES_HANDLE_P (entry ));
5930
- add_function (return_value , return_value , & tmp );
5930
+ op (return_value , return_value , & tmp );
5931
5931
}
5932
5932
/* BC non numeric strings: previously were cast to 0 */
5933
5933
if (Z_TYPE_P (entry ) == IS_STRING ) {
5934
5934
zval tmp ;
5935
5935
ZVAL_LONG (& tmp , 0 );
5936
- add_function (return_value , return_value , & tmp );
5936
+ op (return_value , return_value , & tmp );
5937
5937
}
5938
- php_error_docref (NULL , E_WARNING , "Addition is not supported on type %s" ,
5939
- zend_zval_type_name (entry ));
5938
+ php_error_docref (NULL , E_WARNING , "%s is not supported on type %s" ,
5939
+ op_name , zend_zval_type_name (entry ));
5940
5940
}
5941
5941
} ZEND_HASH_FOREACH_END ();
5942
5942
@@ -5951,69 +5951,24 @@ PHP_FUNCTION(array_sum)
5951
5951
zend_error (E_WARNING , "Object of class %s could not be converted to int|float" ,
5952
5952
ZSTR_VAL (Z_OBJCE_P (return_value )-> name ));
5953
5953
zval_ptr_dtor (return_value );
5954
- RETURN_LONG (0 );
5954
+ RETURN_LONG (initial );
5955
5955
}
5956
5956
zval_ptr_dtor (return_value );
5957
5957
RETURN_COPY_VALUE (& dst );
5958
5958
}
5959
5959
}
5960
+
5961
+ /* {{{ Returns the sum of the array entries */
5962
+ PHP_FUNCTION (array_sum )
5963
+ {
5964
+ php_array_binop (INTERNAL_FUNCTION_PARAM_PASSTHRU , "Addition" , add_function , 0 );
5965
+ }
5960
5966
/* }}} */
5961
5967
5962
5968
/* {{{ Returns the product of the array entries */
5963
5969
PHP_FUNCTION (array_product )
5964
5970
{
5965
- HashTable * input ;
5966
- zval * entry ;
5967
-
5968
- ZEND_PARSE_PARAMETERS_START (1 , 1 )
5969
- Z_PARAM_ARRAY_HT (input )
5970
- ZEND_PARSE_PARAMETERS_END ();
5971
-
5972
- if (zend_hash_num_elements (input ) == 0 ) {
5973
- php_error_docref (NULL , E_DEPRECATED , "Passing an empty array is deprecated" );
5974
- RETURN_LONG (1 );
5975
- }
5976
-
5977
- ZVAL_LONG (return_value , 1 );
5978
-
5979
- ZEND_HASH_FOREACH_VAL (input , entry ) {
5980
- zend_result status = mul_function (return_value , return_value , entry );
5981
- if (status == FAILURE ) {
5982
- ZEND_ASSERT (EG (exception ));
5983
- zend_clear_exception ();
5984
- /* BC resources: previously resources were cast to int */
5985
- if (Z_TYPE_P (entry ) == IS_RESOURCE ) {
5986
- zval tmp ;
5987
- ZVAL_LONG (& tmp , Z_RES_HANDLE_P (entry ));
5988
- mul_function (return_value , return_value , & tmp );
5989
- }
5990
- /* BC non numeric strings: previously were cast to 0 */
5991
- if (Z_TYPE_P (entry ) == IS_STRING ) {
5992
- zval tmp ;
5993
- ZVAL_LONG (& tmp , 0 );
5994
- mul_function (return_value , return_value , & tmp );
5995
- }
5996
- php_error_docref (NULL , E_WARNING , "Multiplication is not supported on type %s" ,
5997
- zend_zval_type_name (entry ));
5998
- }
5999
- } ZEND_HASH_FOREACH_END ();
6000
-
6001
- /* Traversal of array encountered objects that support multiplication */
6002
- if (Z_TYPE_P (return_value ) == IS_OBJECT ) {
6003
- /* Cannot use convert_scalar_to_number() as we don't know if the cast succeeded */
6004
- zval dst ;
6005
- zend_result status = Z_OBJ_HT_P (return_value )-> cast_object (Z_OBJ_P (return_value ), & dst , _IS_NUMBER );
6006
-
6007
- /* Do not type error for BC */
6008
- if (status == FAILURE || (Z_TYPE (dst ) != IS_LONG && Z_TYPE (dst ) != IS_DOUBLE )) {
6009
- zend_error (E_WARNING , "Object of class %s could not be converted to int|float" ,
6010
- ZSTR_VAL (Z_OBJCE_P (return_value )-> name ));
6011
- zval_ptr_dtor (return_value );
6012
- RETURN_LONG (1 );
6013
- }
6014
- zval_ptr_dtor (return_value );
6015
- RETURN_COPY_VALUE (& dst );
6016
- }
5971
+ php_array_binop (INTERNAL_FUNCTION_PARAM_PASSTHRU , "Multiplication" , mul_function , 1 );
6017
5972
}
6018
5973
/* }}} */
6019
5974
0 commit comments