Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/spl/php_spl.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function spl_object_id(object $object): int {}

function iterator_apply(Traversable $iterator, callable $callback, ?array $args = null): int {}

function iterator_count(Traversable $iterator): int {}
function iterator_count(iterable $iterator): int {}

/** @refcount 1 */
function iterator_to_array(Traversable $iterator, bool $preserve_keys = true): array {}
function iterator_to_array(iterable $iterator, bool $preserve_keys = true): array {}
6 changes: 3 additions & 3 deletions ext/spl/php_spl_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 21 additions & 7 deletions ext/spl/spl_iterators.c
Original file line number Diff line number Diff line change
Expand Up @@ -3171,8 +3171,18 @@ PHP_FUNCTION(iterator_to_array)
zval *obj;
bool use_keys = 1;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &obj, zend_ce_traversable, &use_keys) == FAILURE) {
RETURN_THROWS();
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ITERABLE(obj)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(use_keys)
ZEND_PARSE_PARAMETERS_END();

if (Z_TYPE_P(obj) == IS_ARRAY) {
if (use_keys) {
RETURN_COPY(obj);
} else {
RETURN_ARR(zend_array_to_list(Z_ARRVAL_P(obj)));
}
}

array_init(return_value);
Expand All @@ -3195,12 +3205,16 @@ PHP_FUNCTION(iterator_count)
zval *obj;
zend_long count = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &obj, zend_ce_traversable) == FAILURE) {
RETURN_THROWS();
}
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ITERABLE(obj)
ZEND_PARSE_PARAMETERS_END();

if (spl_iterator_apply(obj, spl_iterator_count_apply, (void*)&count) == FAILURE) {
return;
if (Z_TYPE_P(obj) == IS_ARRAY) {
count = zend_hash_num_elements(Z_ARRVAL_P(obj));
} else {
if (spl_iterator_apply(obj, spl_iterator_count_apply, (void*)&count) == FAILURE) {
return;
}
}

RETURN_LONG(count);
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/tests/iterator_count.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ iterator_count('1');

?>
--EXPECTF--
Fatal error: Uncaught TypeError: iterator_count(): Argument #1 ($iterator) must be of type Traversable, string given in %s:%d
Fatal error: Uncaught TypeError: iterator_count(): Argument #1 ($iterator) must be of type Traversable|array, string given in %s:%d
Stack trace:
#0 %s(%d): iterator_count('1')
#1 {main}
Expand Down
14 changes: 14 additions & 0 deletions ext/spl/tests/iterator_count_array.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
SPL: iterator_count() supports arrays.
--FILE--
<?php

var_dump(iterator_count([]));
var_dump(iterator_count([1]));
var_dump(iterator_count(['a' => 1, 'b' => 2, 5 => 3]));

?>
--EXPECTF--
int(0)
int(1)
int(3)
2 changes: 1 addition & 1 deletion ext/spl/tests/iterator_to_array.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ iterator_to_array('test','test');

?>
--EXPECTF--
Fatal error: Uncaught TypeError: iterator_to_array(): Argument #1 ($iterator) must be of type Traversable, string given in %s:%d
Fatal error: Uncaught TypeError: iterator_to_array(): Argument #1 ($iterator) must be of type Traversable|array, string given in %s:%d
Stack trace:
#0 %s(%d): iterator_to_array('test', 'test')
#1 {main}
Expand Down
42 changes: 42 additions & 0 deletions ext/spl/tests/iterator_to_array_array.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
SPL: iterator_to_array() supports arrays.
--FILE--
<?php

var_dump(iterator_to_array([]));
var_dump(iterator_to_array([1]));
var_dump(iterator_to_array(['a' => 1, 'b' => 2, 5 => 3]));
var_dump(iterator_to_array([], false));
var_dump(iterator_to_array([1], false));
var_dump(iterator_to_array(['a' => 1, 'b' => 2, 5 => 3], false));

?>
--EXPECT--
array(0) {
}
array(1) {
[0]=>
int(1)
}
array(3) {
["a"]=>
int(1)
["b"]=>
int(2)
[5]=>
int(3)
}
array(0) {
}
array(1) {
[0]=>
int(1)
}
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}