Skip to content

Commit

Permalink
enhanced array_product()
Browse files Browse the repository at this point in the history
  • Loading branch information
tjerk authored and = committed Jul 25, 2013
1 parent e1711b2 commit 3e00853
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
21 changes: 20 additions & 1 deletion ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -4059,10 +4059,14 @@ PHP_FUNCTION(array_product)
zval *input,
**entry,
entry_n;
zend_bool have_callback = 0;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;
zval **args[1], *retval;
HashPosition pos;
double dval;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &input) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|f", &input, &fci, &fci_cache) == FAILURE) {
return;
}

Expand All @@ -4071,10 +4075,25 @@ PHP_FUNCTION(array_product)
return;
}

if (ZEND_NUM_ARGS() > 1) {
have_callback = 1;
fci.retval_ptr_ptr = &retval;
fci.no_separation = 0;
fci.param_count = 1;
}

for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(input), &pos);
zend_hash_get_current_data_ex(Z_ARRVAL_P(input), (void **)&entry, &pos) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(input), &pos)
) {
if (have_callback) {
args[0] = entry;
fci.params = args;

if (zend_call_function(&fci, &fci_cache TSRMLS_CC) == SUCCESS && retval) {
entry = &retval;
}
}
if (Z_TYPE_PP(entry) == IS_ARRAY || Z_TYPE_PP(entry) == IS_OBJECT) {
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,9 @@ ZEND_BEGIN_ARG_INFO(arginfo_array_sum, 0)
ZEND_ARG_INFO(0, arg) /* ARRAY_INFO(0, arg, 0) */
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_array_product, 0)
ZEND_BEGIN_ARG_INFO_EX(arginfo_array_product, 0, 0, 1)
ZEND_ARG_INFO(0, arg) /* ARRAY_INFO(0, arg, 0) */
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_array_reduce, 0, 0, 2)
Expand Down
14 changes: 11 additions & 3 deletions ext/standard/tests/array/array_product_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,36 @@ var_dump( array_product() );
echo "\n-- Testing array_product() function with more than expected no. of arguments --\n";
$input = array(1, 2);
$extra_arg = 10;
var_dump( array_product($input, $extra_arg) );
var_dump( array_product($input, 'is_numeric', $extra_arg) );

echo "\n-- Testing array_product() function incorrect argument type --\n";
var_dump( array_product("bob") );

echo "\n-- Testing array_product() function incorrect second argument type --\n";
var_dump( array_product($input, $input) );

?>
===DONE===
--EXPECTF--
*** Testing array_product() : error conditions ***

-- Testing array_product() function with Zero arguments --

Warning: array_product() expects exactly 1 parameter, 0 given in %sarray_product_error.php on line %d
Warning: array_product() expects at least 1 parameter, 0 given in %sarray_product_error.php on line %d
NULL

-- Testing array_product() function with more than expected no. of arguments --

Warning: array_product() expects exactly 1 parameter, 2 given in %sarray_product_error.php on line %d
Warning: array_product() expects at most 2 parameters, 3 given in %sarray_product_error.php on line %d
NULL

-- Testing array_product() function incorrect argument type --

Warning: array_product() expects parameter 1 to be array, string given in %sarray_product_error.php on line %d
NULL

-- Testing array_product() function incorrect second argument type --

Warning: array_product() expects parameter 2 to be a valid callback, first array member is not a valid class name or object in /home/tjerk/work/php/php-src/ext/standard/tests/array/array_product_error.php on line 24
NULL
===DONE===

0 comments on commit 3e00853

Please sign in to comment.