php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #60577
Patch bug60577_emptyiterator_count.diff revision 2011-12-20 20:20 UTC by adam at sixohthree dot com

Patch bug60577_emptyiterator_count.diff for SPL related Bug #60577

Patch version 2011-12-20 20:20 UTC

Return to Bug #60577 | Download this patch
Patch Revisions:

Developer: adam@sixohthree.com

Index: ext/spl/tests/bug60577.phpt
===================================================================
--- ext/spl/tests/bug60577.phpt	(revision 0)
+++ ext/spl/tests/bug60577.phpt	(revision 0)
@@ -0,0 +1,8 @@
+--TEST--
+count(new EmptyIterator) should return zero
+--FILE--
+<?php
+$it = new EmptyIterator;
+var_dump(count($it));
+--EXPECT--
+int(0)
Index: ext/spl/internal/emptyiterator.inc
===================================================================
--- ext/spl/internal/emptyiterator.inc	(revision 321283)
+++ ext/spl/internal/emptyiterator.inc	(working copy)
@@ -15,7 +15,7 @@
  * @version 1.0
  * @since PHP 5.1
  */
-class EmptyIterator implements Iterator
+class EmptyIterator implements Iterator, Countable
 {
 	/** No operation.
 	 * @return void
@@ -57,6 +57,13 @@
 	{
 		// nothing to do
 	}
+
+	/** @return integer
+	 */
+	function count()
+	{
+		return 0;
+	}
 }
 
-?>
\ No newline at end of file
+?>
Index: ext/spl/spl_iterators.c
===================================================================
--- ext/spl/spl_iterators.c	(revision 321283)
+++ ext/spl/spl_iterators.c	(working copy)
@@ -3270,12 +3270,23 @@
 	}
 } /* }}} */
 
+/* {{{ proto void 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
 };
 
@@ -3747,6 +3758,7 @@
 
 	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);
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Jan 25 17:01:27 2021 UTC