Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return early on Finder::last() on empty array #22

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public static function last(
// ensure data is array for end(), key(), current(), prev() usage
Assert::isArray($data);

if ($data === []) {
return null;
}

// Use end(), key(), current(), prev() usage instead of array_reverse()
// to avoid immediatelly got "Out of memory" on many data
// see https://3v4l.org/IHo2H vs https://3v4l.org/Wqejc
Expand Down Expand Up @@ -114,8 +118,6 @@ public static function last(

return $resortkey;
}

return null;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public static function lastDataProvider(): array
static fn(string $datum, int $key): bool => str_contains($datum, 'test') && $key === 1,
null,
],
[
[],
static fn(string $datum, int $key): bool => str_contains($datum, 'test') && $key === 1,
null,
],
];
}

Expand Down
Loading