Skip to content

Commit 9021c6e

Browse files
authored
fix(support): support calling first and last on empty ArrayHelper (#691)
1 parent f5b848c commit 9021c6e

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/Tempest/Support/src/ArrayHelper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ public function equals(array|self $other): bool
384384
*/
385385
public function first(?Closure $filter = null): mixed
386386
{
387+
if ($this->array === []) {
388+
return null;
389+
}
390+
387391
if ($filter === null) {
388392
return $this->array[array_key_first($this->array)];
389393
}
@@ -405,6 +409,10 @@ public function first(?Closure $filter = null): mixed
405409
*/
406410
public function last(?Closure $filter = null): mixed
407411
{
412+
if ($this->array === []) {
413+
return null;
414+
}
415+
408416
if ($filter === null) {
409417
return $this->array[array_key_last($this->array)];
410418
}

src/Tempest/Support/tests/ArrayHelperTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,14 @@ public function test_unshift(): void
178178

179179
public function test_last(): void
180180
{
181+
$this->assertSame(null, arr()->last());
181182
$this->assertSame('c', arr(['a', 'b', 'c'])->last());
182183
}
183184

184185
public function test_first(): void
185186
{
186187
$this->assertSame('a', arr(['a', 'b', 'c'])->first());
188+
$this->assertSame(null, arr()->first());
187189
}
188190

189191
public function test_is_empty(): void

0 commit comments

Comments
 (0)