Skip to content

Commit 8a936e8

Browse files
morrisonlevidsp
authored andcommitted
EmptyIterator now implements Countable; fixes bug 60577
(cherry picked from commit 6398844c86bee08abe4ee3f206ecd86ad0a498f9)
1 parent c0afe82 commit 8a936e8

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

ext/spl/internal/emptyiterator.inc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @version 1.0
1616
* @since PHP 5.1
1717
*/
18-
class EmptyIterator implements Iterator
18+
class EmptyIterator implements Iterator, Countable
1919
{
2020
/** No operation.
2121
* @return void
@@ -57,6 +57,15 @@ class EmptyIterator implements Iterator
5757
{
5858
// nothing to do
5959
}
60+
61+
/**
62+
* @return int
63+
*/
64+
function count()
65+
{
66+
return 0;
67+
}
68+
6069
}
6170

62-
?>
71+
?>

ext/spl/spl_iterators.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3279,12 +3279,23 @@ SPL_METHOD(EmptyIterator, next)
32793279
}
32803280
} /* }}} */
32813281

3282+
/* {{{ proto int EmptyIterator::count()
3283+
Does nothing */
3284+
SPL_METHOD(EmptyIterator, count)
3285+
{
3286+
if (zend_parse_parameters_none() == FAILURE) {
3287+
return;
3288+
}
3289+
RETURN_LONG(0);
3290+
} /* }}} */
3291+
32823292
static const zend_function_entry spl_funcs_EmptyIterator[] = {
32833293
SPL_ME(EmptyIterator, rewind, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
32843294
SPL_ME(EmptyIterator, valid, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
32853295
SPL_ME(EmptyIterator, key, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
32863296
SPL_ME(EmptyIterator, current, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
32873297
SPL_ME(EmptyIterator, next, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
3298+
SPL_ME(EmptyIterator, count, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
32883299
PHP_FE_END
32893300
};
32903301

@@ -3756,6 +3767,7 @@ PHP_MINIT_FUNCTION(spl_iterators)
37563767

37573768
REGISTER_SPL_STD_CLASS_EX(EmptyIterator, NULL, spl_funcs_EmptyIterator);
37583769
REGISTER_SPL_ITERATOR(EmptyIterator);
3770+
REGISTER_SPL_IMPLEMENTS(EmptyIterator, Countable);
37593771

37603772
REGISTER_SPL_SUB_CLASS_EX(RecursiveTreeIterator, RecursiveIteratorIterator, spl_RecursiveTreeIterator_new, spl_funcs_RecursiveTreeIterator);
37613773
REGISTER_SPL_CLASS_CONST_LONG(RecursiveTreeIterator, "BYPASS_CURRENT", RTIT_BYPASS_CURRENT);

ext/spl/tests/bug60577.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
count(new EmptyIterator) should return zero
3+
--FILE--
4+
<?php
5+
$it = new EmptyIterator;
6+
var_dump(count($it));
7+
--EXPECT--
8+
int(0)

0 commit comments

Comments
 (0)