From 1111ff81191e49121b3f0166d2c7a73db8e821ad Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 16 Dec 2025 15:08:01 +0700 Subject: [PATCH] Make faster search on Finder::last() on SplFixedArray data --- src/Finder.php | 5 +++++ tests/FinderTest.php | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Finder.php b/src/Finder.php index 1e77ebd..926145a 100644 --- a/src/Finder.php +++ b/src/Finder.php @@ -7,6 +7,7 @@ use ArrayIterator; use ArrayLookup\Assert\Filter; use ArrayObject; +use SplFixedArray; use Traversable; use Webmozart\Assert\Assert; @@ -51,6 +52,10 @@ private static function resolveArrayFromTraversable(Traversable $traversable): a return $traversable->getArrayCopy(); } + if ($traversable instanceof SplFixedArray) { + return $traversable->toArray(); + } + return iterator_to_array($traversable); } diff --git a/tests/FinderTest.php b/tests/FinderTest.php index 1b9afa2..fac445a 100644 --- a/tests/FinderTest.php +++ b/tests/FinderTest.php @@ -14,6 +14,7 @@ use Iterator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; +use SplFixedArray; use stdClass; use function current; @@ -248,6 +249,16 @@ public static function lastReturnKeyDataProvider(): Iterator static fn(string $datum, int $key): bool => str_contains($datum, 'test') && $key === 1, null, ]; + yield [ + SplFixedArray::fromArray([6, 7, 8, 9]), + static fn($datum): bool => $datum > 5, + 3, + ]; + yield [ + SplFixedArray::fromArray([6, 7, 8, 9]), + static fn($datum): bool => $datum < 5, + null, + ]; } /**