From: Nikita Popov Date: Wed, 13 May 2020 15:24:13 +0000 (+0200) Subject: Allow null callback to array_filter() X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=50a9f511ccc8946551f8dcb573476e075dce330c;p=php Allow null callback to array_filter() With same behavior as not passing it. --- diff --git a/Zend/zend_API.h b/Zend/zend_API.h index c355456892..3943f691bf 100644 --- a/Zend/zend_API.h +++ b/Zend/zend_API.h @@ -1380,6 +1380,9 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_value_error(uint32_t arg_num #define Z_PARAM_FUNC(dest_fci, dest_fcc) \ Z_PARAM_FUNC_EX(dest_fci, dest_fcc, 0, 0) +#define Z_PARAM_FUNC_OR_NULL(dest_fci, dest_fcc) \ + Z_PARAM_FUNC_EX(dest_fci, dest_fcc, 1, 0) + /* old "h" */ #define Z_PARAM_ARRAY_HT_EX2(dest, check_null, deref, separate) \ Z_PARAM_PROLOGUE(deref, separate); \ diff --git a/ext/libxml/libxml.c b/ext/libxml/libxml.c index febc5eca2d..09f8ae83ed 100644 --- a/ext/libxml/libxml.c +++ b/ext/libxml/libxml.c @@ -1067,7 +1067,7 @@ PHP_FUNCTION(libxml_set_external_entity_loader) zend_fcall_info_cache fcc; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_FUNC_EX(fci, fcc, 1, 0) + Z_PARAM_FUNC_OR_NULL(fci, fcc) ZEND_PARSE_PARAMETERS_END(); _php_libxml_destroy_fci(&LIBXML(entity_loader).fci, &LIBXML(entity_loader).object); diff --git a/ext/sqlite3/sqlite3.c b/ext/sqlite3/sqlite3.c index b978c49d80..2a36ffd5d4 100644 --- a/ext/sqlite3/sqlite3.c +++ b/ext/sqlite3/sqlite3.c @@ -1361,7 +1361,7 @@ PHP_METHOD(SQLite3, setAuthorizer) zend_fcall_info_cache fcc; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_FUNC_EX(fci, fcc, 1, 0) + Z_PARAM_FUNC_OR_NULL(fci, fcc) ZEND_PARSE_PARAMETERS_END(); SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3) diff --git a/ext/standard/array.c b/ext/standard/array.c index fd0ceaa600..5138614858 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -5958,7 +5958,7 @@ PHP_FUNCTION(array_filter) ZEND_PARSE_PARAMETERS_START(1, 3) Z_PARAM_ARRAY(array) Z_PARAM_OPTIONAL - Z_PARAM_FUNC(fci, fci_cache) + Z_PARAM_FUNC_OR_NULL(fci, fci_cache) Z_PARAM_LONG(use_type) ZEND_PARSE_PARAMETERS_END(); @@ -5969,7 +5969,7 @@ PHP_FUNCTION(array_filter) } array_init(return_value); - if (ZEND_NUM_ARGS() > 1) { + if (ZEND_FCI_INITIALIZED(fci)) { have_callback = 1; fci.no_separation = 0; fci.retval = &retval; @@ -6045,7 +6045,7 @@ PHP_FUNCTION(array_map) uint32_t k, maxlen = 0; ZEND_PARSE_PARAMETERS_START(2, -1) - Z_PARAM_FUNC_EX(fci, fci_cache, 1, 0) + Z_PARAM_FUNC_OR_NULL(fci, fci_cache) Z_PARAM_VARIADIC('+', arrays, n_arrays) ZEND_PARSE_PARAMETERS_END(); diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index 2017549e9b..71b922d7b2 100755 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -237,7 +237,7 @@ function array_product(array $arg): int|float {} /** @return mixed */ function array_reduce(array $arg, callable $callback, $initial = null) {} -function array_filter(array $arg, callable $callback = UNKNOWN, int $use_keys = 0): array {} +function array_filter(array $arg, ?callable $callback = null, int $use_keys = 0): array {} function array_map(?callable $callback, array $arr1, array ...$arrays): array {} diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 5e2e457ea2..23152bc22e 100755 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -338,7 +338,7 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_array_filter, 0, 1, IS_ARRAY, 0) ZEND_ARG_TYPE_INFO(0, arg, IS_ARRAY, 0) - ZEND_ARG_TYPE_INFO(0, callback, IS_CALLABLE, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, callback, IS_CALLABLE, 1, "null") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, use_keys, IS_LONG, 0, "0") ZEND_END_ARG_INFO() diff --git a/ext/standard/tests/array/array_filter_basic.phpt b/ext/standard/tests/array/array_filter_basic.phpt index b2a51ab2c6..1b68649546 100644 --- a/ext/standard/tests/array/array_filter_basic.phpt +++ b/ext/standard/tests/array/array_filter_basic.phpt @@ -31,6 +31,8 @@ var_dump( array_filter($input,"even") ); // with default arguments var_dump( array_filter($input) ); +// same as with default arguments +var_dump( array_filter($input, null) ); echo "Done" ?> @@ -52,4 +54,14 @@ array(4) { [4]=> int(-1) } +array(4) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(3) + [4]=> + int(-1) +} Done