From 97b7f99e0c5e5f18bd571ff4e9923a63f54ea084 Mon Sep 17 00:00:00 2001 From: Moshe Brodsky Date: Sun, 12 Nov 2023 07:24:10 +0200 Subject: [PATCH 1/3] fix Exception: Cannot traverse an already closed generator when running Arr::first with an empty generator and no callback --- src/Illuminate/Collections/Arr.php | 2 ++ tests/Support/SupportArrTest.php | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index 3cb9a2ca180a..c14465c6b3fa 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -195,6 +195,8 @@ public static function first($array, callable $callback = null, $default = null) foreach ($array as $item) { return $item; } + + return value($default); } foreach ($array as $key => $value) { diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index cd41e0b3799a..de305e21f0cc 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -210,6 +210,13 @@ public function testFirst() $this->assertNull($value2); $this->assertSame('bar', $value3); $this->assertSame('baz', $value4); + + $cursor = (function () { + while (false) { + yield 1; + } + })(); + $this->assertNull(Arr::first($cursor)); } public function testJoin() From f3e33798a56109648f090b5ad08f7435dd4c3430 Mon Sep 17 00:00:00 2001 From: Moshe Brodsky Date: Sun, 12 Nov 2023 07:36:17 +0200 Subject: [PATCH 2/3] force rerun actions --- tests/Support/SupportArrTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 2c5f6c224e67..05ddf688f1e5 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -220,6 +220,7 @@ public function testFirst() yield 1; } })(); + $this->assertNull(Arr::first($cursor)); } From f5a5e755ac2de36525c3ceb2062285d35004bdef Mon Sep 17 00:00:00 2001 From: Moshe Brodsky Date: Sun, 12 Nov 2023 07:36:52 +0200 Subject: [PATCH 3/3] force rerun actions --- tests/Support/SupportArrTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 05ddf688f1e5..2c5f6c224e67 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -220,7 +220,6 @@ public function testFirst() yield 1; } })(); - $this->assertNull(Arr::first($cursor)); }