Skip to content

Commit 42cb5b5

Browse files
committed
Throw from MultipleIterator::key/current() for invalid iterator
Calling current()/key() on an invalid iterator is an error condition. Throw instead of returning false.
1 parent 1bd779c commit 42cb5b5

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

ext/spl/spl_observer.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,9 @@ static void spl_multiple_iterator_get_all(spl_SplObjectStorage *intern, int get_
10971097

10981098
num_elements = zend_hash_num_elements(&intern->storage);
10991099
if (num_elements < 1) {
1100-
RETURN_FALSE;
1100+
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Called %s() on an invalid iterator",
1101+
get_type == SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT ? "current" : "key");
1102+
RETURN_THROWS();
11011103
}
11021104

11031105
array_init_size(return_value, num_elements);

ext/spl/spl_observer.stub.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function valid() {}
5858
/** @return int */
5959
public function key() {}
6060

61-
/** @return object|null */
61+
/** @return object */
6262
public function current() {}
6363

6464
/** @return void */
@@ -141,10 +141,10 @@ public function rewind() {}
141141
/** @return bool */
142142
public function valid() {}
143143

144-
/** @return array|false */
144+
/** @return array */
145145
public function key() {}
146146

147-
/** @return array|false */
147+
/** @return array */
148148
public function current() {}
149149

150150
/** @return void */

ext/spl/spl_observer_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 89437500594aa41fa5cd36f31de07ad65f2bf408 */
2+
* Stub hash: b82f26b4c1340b58c0faa31c5e14fef4c9778928 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SplObserver_update, 0, 0, 1)
55
ZEND_ARG_OBJ_INFO(0, subject, SplSubject, 0)

ext/spl/tests/multiple_iterator_001.phpt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ echo "-- Default flags, no iterators --\n";
1313
foreach($m as $value) {
1414
var_dump($value);
1515
}
16-
var_dump($m->current());
16+
try {
17+
var_dump($m->current());
18+
} catch (RuntimeException $e) {
19+
echo $e->getMessage(), "\n";
20+
}
21+
try {
22+
var_dump($m->key());
23+
} catch (RuntimeException $e) {
24+
echo $e->getMessage(), "\n";
25+
}
1726

1827
$m->attachIterator($iter1);
1928
$m->attachIterator($iter2);
@@ -105,7 +114,8 @@ foreach($m as $key => $value) {
105114
?>
106115
--EXPECTF--
107116
-- Default flags, no iterators --
108-
bool(false)
117+
Called current() on an invalid iterator
118+
Called key() on an invalid iterator
109119
-- Default flags, MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_NUMERIC --
110120
bool(true)
111121
array(3) {

0 commit comments

Comments
 (0)