Skip to content

Commit f45f36a

Browse files
duncanxia97dg
authored andcommitted
ArrayList support returning references (#289)
1 parent 9f29f6d commit f45f36a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/Utils/ArrayList.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ public static function from(array $array): static
4141

4242
/**
4343
* Returns an iterator over all items.
44-
* @return \ArrayIterator<int, T>
44+
* @return \Iterator<int, T>
4545
*/
46-
public function getIterator(): \ArrayIterator
46+
public function &getIterator(): \Iterator
4747
{
48-
return new \ArrayIterator($this->list);
48+
foreach ($this->list as &$item) {
49+
yield $item;
50+
}
4951
}
5052

5153

tests/Utils/ArrayList.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,13 @@ test('', function () {
157157
unset($list['key']);
158158
}, OutOfRangeException::class, 'Offset invalid or out of range');
159159
});
160+
161+
162+
test('returning references', function () {
163+
$list = ArrayList::from([1, 2, 3]);
164+
foreach ($list as $key => &$value) {
165+
$value = 'new';
166+
}
167+
168+
Assert::same(['new', 'new', 'new'], iterator_to_array($list));
169+
});

0 commit comments

Comments
 (0)