Skip to content

Commit

Permalink
EmptyIterator now implements Countable; fixes bug 60577
Browse files Browse the repository at this point in the history
(cherry picked from commit 6398844c86bee08abe4ee3f206ecd86ad0a498f9)
  • Loading branch information
morrisonlevi authored and dsp committed Sep 16, 2013
1 parent c0afe82 commit 8a936e8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
13 changes: 11 additions & 2 deletions ext/spl/internal/emptyiterator.inc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @version 1.0
* @since PHP 5.1
*/
class EmptyIterator implements Iterator
class EmptyIterator implements Iterator, Countable
{
/** No operation.
* @return void
Expand Down Expand Up @@ -57,6 +57,15 @@ class EmptyIterator implements Iterator
{
// nothing to do
}

/**
* @return int
*/
function count()
{
return 0;
}

}

?>
?>
12 changes: 12 additions & 0 deletions ext/spl/spl_iterators.c
Original file line number Diff line number Diff line change
Expand Up @@ -3279,12 +3279,23 @@ SPL_METHOD(EmptyIterator, next)
}
} /* }}} */

/* {{{ proto int EmptyIterator::count()
Does nothing */
SPL_METHOD(EmptyIterator, count)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
RETURN_LONG(0);
} /* }}} */

static const zend_function_entry spl_funcs_EmptyIterator[] = {
SPL_ME(EmptyIterator, rewind, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
SPL_ME(EmptyIterator, valid, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
SPL_ME(EmptyIterator, key, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
SPL_ME(EmptyIterator, current, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
SPL_ME(EmptyIterator, next, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
SPL_ME(EmptyIterator, count, arginfo_recursive_it_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};

Expand Down Expand Up @@ -3756,6 +3767,7 @@ PHP_MINIT_FUNCTION(spl_iterators)

REGISTER_SPL_STD_CLASS_EX(EmptyIterator, NULL, spl_funcs_EmptyIterator);
REGISTER_SPL_ITERATOR(EmptyIterator);
REGISTER_SPL_IMPLEMENTS(EmptyIterator, Countable);

REGISTER_SPL_SUB_CLASS_EX(RecursiveTreeIterator, RecursiveIteratorIterator, spl_RecursiveTreeIterator_new, spl_funcs_RecursiveTreeIterator);
REGISTER_SPL_CLASS_CONST_LONG(RecursiveTreeIterator, "BYPASS_CURRENT", RTIT_BYPASS_CURRENT);
Expand Down
8 changes: 8 additions & 0 deletions ext/spl/tests/bug60577.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
count(new EmptyIterator) should return zero
--FILE--
<?php
$it = new EmptyIterator;
var_dump(count($it));
--EXPECT--
int(0)

0 comments on commit 8a936e8

Please sign in to comment.