Skip to content

Commit c948905

Browse files
fix Exception: Cannot traverse an already closed generator when running Arr::first with an empty generator and no callback (#48851)
* fix Exception: Cannot traverse an already closed generator when running Arr::first with an empty generator and no callback * styling * should use default return
1 parent 800e264 commit c948905

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/Illuminate/Collections/Arr.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ public static function first($array, callable $callback = null, $default = null)
195195
foreach ($array as $item) {
196196
return $item;
197197
}
198+
199+
return value($default);
198200
}
199201

200202
foreach ($array as $key => $value) {

tests/Support/SupportArrTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ public function testFirst()
214214
$this->assertSame('bar', $value3);
215215
$this->assertSame('baz', $value4);
216216
$this->assertEquals(100, $value5);
217+
218+
$cursor = (function () {
219+
while (false) {
220+
yield 1;
221+
}
222+
})();
223+
$this->assertNull(Arr::first($cursor));
217224
}
218225

219226
public function testJoin()

0 commit comments

Comments
 (0)