diff --git a/src/Utils/ArrayList.php b/src/Utils/ArrayList.php index 54684c751..4f32061b4 100644 --- a/src/Utils/ArrayList.php +++ b/src/Utils/ArrayList.php @@ -41,11 +41,13 @@ public static function from(array $array): static /** * Returns an iterator over all items. - * @return \ArrayIterator + * @return \Iterator */ - public function getIterator(): \ArrayIterator + public function &getIterator(): \Iterator { - return new \ArrayIterator($this->list); + foreach ($this->list as &$item) { + yield $item; + } } diff --git a/tests/Utils/ArrayList.phpt b/tests/Utils/ArrayList.phpt index 2a60da4d5..462f7a757 100644 --- a/tests/Utils/ArrayList.phpt +++ b/tests/Utils/ArrayList.phpt @@ -157,3 +157,13 @@ test('', function () { unset($list['key']); }, OutOfRangeException::class, 'Offset invalid or out of range'); }); + + +test('returning references', function () { + $list = ArrayList::from([1, 2, 3]); + foreach ($list as $key => &$value) { + $value = 'new'; + } + + Assert::same(['new', 'new', 'new'], iterator_to_array($list)); +});