Skip to content

Commit

Permalink
fix(support): support calling first and last on empty `ArrayHelpe…
Browse files Browse the repository at this point in the history
…r` (#691)
  • Loading branch information
innocenzi authored Nov 6, 2024
1 parent f5b848c commit 9021c6e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Tempest/Support/src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ public function equals(array|self $other): bool
*/
public function first(?Closure $filter = null): mixed
{
if ($this->array === []) {
return null;
}

if ($filter === null) {
return $this->array[array_key_first($this->array)];
}
Expand All @@ -405,6 +409,10 @@ public function first(?Closure $filter = null): mixed
*/
public function last(?Closure $filter = null): mixed
{
if ($this->array === []) {
return null;
}

if ($filter === null) {
return $this->array[array_key_last($this->array)];
}
Expand Down
2 changes: 2 additions & 0 deletions src/Tempest/Support/tests/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,14 @@ public function test_unshift(): void

public function test_last(): void
{
$this->assertSame(null, arr()->last());
$this->assertSame('c', arr(['a', 'b', 'c'])->last());
}

public function test_first(): void
{
$this->assertSame('a', arr(['a', 'b', 'c'])->first());
$this->assertSame(null, arr()->first());
}

public function test_is_empty(): void
Expand Down

0 comments on commit 9021c6e

Please # to comment.