From 7d600eed45ed4278ffcd198214decdbf580c4587 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 22 Apr 2024 06:24:47 +0700 Subject: [PATCH 1/8] No need filter on loop when total data is < target total filtered data --- src/AtLeast.php | 6 ++++++ src/Only.php | 6 ++++++ tests/AtLeastTest.php | 5 +++++ tests/OnlyTest.php | 5 +++++ 4 files changed, 22 insertions(+) diff --git a/src/AtLeast.php b/src/AtLeast.php index b2dcb61..e11e961 100644 --- a/src/AtLeast.php +++ b/src/AtLeast.php @@ -7,6 +7,8 @@ use Traversable; use Webmozart\Assert\Assert; +use function count; + final class AtLeast { /** @@ -48,6 +50,10 @@ private static function atLeastFoundTimes( // usage must be higher than 0 Assert::greaterThan($maxCount, 0); + if (count($data) < $maxCount) { + return false; + } + $totalFound = 0; foreach ($data as $key => $datum) { $isFound = $filter($datum, $key); diff --git a/src/Only.php b/src/Only.php index 2f712a8..13cf94a 100644 --- a/src/Only.php +++ b/src/Only.php @@ -7,6 +7,8 @@ use Traversable; use Webmozart\Assert\Assert; +use function count; + final class Only { /** @@ -48,6 +50,10 @@ private static function onlyFoundTimes( // usage must be higher than 0 Assert::greaterThan($maxCount, 0); + if (count($data) < $maxCount) { + return false; + } + $totalFound = 0; foreach ($data as $key => $datum) { $isFound = $filter($datum, $key); diff --git a/tests/AtLeastTest.php b/tests/AtLeastTest.php index a054dfd..3937c5b 100644 --- a/tests/AtLeastTest.php +++ b/tests/AtLeastTest.php @@ -78,6 +78,11 @@ public static function twiceDataProvider(): array static fn(string $datum, int $key): bool => $datum !== 'abc' && $key > 1, false ], + [ + [1], + static fn($datum): bool => $datum == 1, + false, + ], ]; } diff --git a/tests/OnlyTest.php b/tests/OnlyTest.php index d9c3bc5..4707d99 100644 --- a/tests/OnlyTest.php +++ b/tests/OnlyTest.php @@ -81,6 +81,11 @@ public static function twiceDataProvider(): array static fn(string $datum, int $key): bool => $datum !== 'abc' && $key > 1, false ], + [ + [1], + static fn($datum): bool => $datum == 1, + false, + ], ]; } From fdb31ba7af18bee1a22711372c2aaef554f743f8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 22 Apr 2024 06:30:59 +0700 Subject: [PATCH 2/8] use iterator_count() --- src/AtLeast.php | 2 +- src/Only.php | 2 +- tests/AtLeastTest.php | 15 +++++++++++++++ tests/OnlyTest.php | 5 +++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/AtLeast.php b/src/AtLeast.php index e11e961..7b885ca 100644 --- a/src/AtLeast.php +++ b/src/AtLeast.php @@ -50,7 +50,7 @@ private static function atLeastFoundTimes( // usage must be higher than 0 Assert::greaterThan($maxCount, 0); - if (count($data) < $maxCount) { + if (iterator_count($data) < $maxCount) { return false; } diff --git a/src/Only.php b/src/Only.php index 13cf94a..e4deee6 100644 --- a/src/Only.php +++ b/src/Only.php @@ -50,7 +50,7 @@ private static function onlyFoundTimes( // usage must be higher than 0 Assert::greaterThan($maxCount, 0); - if (count($data) < $maxCount) { + if (iterator_count($data) < $maxCount) { return false; } diff --git a/tests/AtLeastTest.php b/tests/AtLeastTest.php index 3937c5b..0ae492f 100644 --- a/tests/AtLeastTest.php +++ b/tests/AtLeastTest.php @@ -83,6 +83,21 @@ public static function twiceDataProvider(): array static fn($datum): bool => $datum == 1, false, ], + [ + [1, "1"], + static fn($datum): bool => $datum == 1, + true, + ], + [ + [1, true], + static fn($datum): bool => $datum == 1, + true, + ], + [ + [1, true, "1"], + static fn($datum): bool => $datum == 1, + true, + ], ]; } diff --git a/tests/OnlyTest.php b/tests/OnlyTest.php index 4707d99..145c2d2 100644 --- a/tests/OnlyTest.php +++ b/tests/OnlyTest.php @@ -86,6 +86,11 @@ public static function twiceDataProvider(): array static fn($datum): bool => $datum == 1, false, ], + [ + [1, "1"], + static fn($datum): bool => $datum == 1, + true, + ], ]; } From 345c69fa0d66d83a74c44e1a623233d1731389c8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 22 Apr 2024 06:31:56 +0700 Subject: [PATCH 3/8] more tests --- tests/OnlyTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/OnlyTest.php b/tests/OnlyTest.php index 145c2d2..5a9a200 100644 --- a/tests/OnlyTest.php +++ b/tests/OnlyTest.php @@ -91,6 +91,16 @@ public static function twiceDataProvider(): array static fn($datum): bool => $datum == 1, true, ], + [ + [1, true], + static fn($datum): bool => $datum == 1, + true, + ], + [ + [1, true, "1"], + static fn($datum): bool => $datum == 1, + false, + ], ]; } From 8ab291b257c3418075e079756446bc87292d3653 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 22 Apr 2024 06:32:53 +0700 Subject: [PATCH 4/8] cs fix --- src/AtLeast.php | 2 +- src/Only.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AtLeast.php b/src/AtLeast.php index 7b885ca..a409a27 100644 --- a/src/AtLeast.php +++ b/src/AtLeast.php @@ -7,7 +7,7 @@ use Traversable; use Webmozart\Assert\Assert; -use function count; +use function iterator_count; final class AtLeast { diff --git a/src/Only.php b/src/Only.php index e4deee6..b3a3c18 100644 --- a/src/Only.php +++ b/src/Only.php @@ -7,7 +7,7 @@ use Traversable; use Webmozart\Assert\Assert; -use function count; +use function iterator_count; final class Only { From 7b182f755b752ddc8f0896d0ce47a01de8ba72cb Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 22 Apr 2024 06:44:28 +0700 Subject: [PATCH 5/8] php 8.1 compat --- src/AtLeast.php | 4 +--- src/Only.php | 4 +--- tests/AtLeastTest.php | 23 ++++++++++++++++++++++- tests/OnlyTest.php | 23 ++++++++++++++++++++++- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/AtLeast.php b/src/AtLeast.php index a409a27..a3802ae 100644 --- a/src/AtLeast.php +++ b/src/AtLeast.php @@ -7,8 +7,6 @@ use Traversable; use Webmozart\Assert\Assert; -use function iterator_count; - final class AtLeast { /** @@ -50,7 +48,7 @@ private static function atLeastFoundTimes( // usage must be higher than 0 Assert::greaterThan($maxCount, 0); - if (iterator_count($data) < $maxCount) { + if (Counter::count($data) < $maxCount) { return false; } diff --git a/src/Only.php b/src/Only.php index b3a3c18..25181ce 100644 --- a/src/Only.php +++ b/src/Only.php @@ -7,8 +7,6 @@ use Traversable; use Webmozart\Assert\Assert; -use function iterator_count; - final class Only { /** @@ -50,7 +48,7 @@ private static function onlyFoundTimes( // usage must be higher than 0 Assert::greaterThan($maxCount, 0); - if (iterator_count($data) < $maxCount) { + if (Counter::count($data) < $maxCount) { return false; } diff --git a/tests/AtLeastTest.php b/tests/AtLeastTest.php index 0ae492f..df85c08 100644 --- a/tests/AtLeastTest.php +++ b/tests/AtLeastTest.php @@ -4,6 +4,7 @@ namespace ArrayLookup\Tests; +use ArrayIterator; use ArrayLookup\AtLeast; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -46,7 +47,7 @@ public static function onceDataProvider(): array } #[DataProvider('twiceDataProvider')] - public function testTwice(array $data, callable $filter, bool $expected): void + public function testTwice(iterable $data, callable $filter, bool $expected): void { $this->assertSame( $expected, @@ -83,21 +84,41 @@ public static function twiceDataProvider(): array static fn($datum): bool => $datum == 1, false, ], + [ + new ArrayIterator([1]), + static fn($datum): bool => $datum == 1, + false, + ], [ [1, "1"], static fn($datum): bool => $datum == 1, true, ], + [ + new ArrayIterator([1, "1"]), + static fn($datum): bool => $datum == 1, + true, + ], [ [1, true], static fn($datum): bool => $datum == 1, true, ], + [ + new ArrayIterator([1, true]), + static fn($datum): bool => $datum == 1, + true, + ], [ [1, true, "1"], static fn($datum): bool => $datum == 1, true, ], + [ + new ArrayIterator([1, true, "1"]), + static fn($datum): bool => $datum == 1, + true, + ], ]; } diff --git a/tests/OnlyTest.php b/tests/OnlyTest.php index 5a9a200..50cef18 100644 --- a/tests/OnlyTest.php +++ b/tests/OnlyTest.php @@ -4,6 +4,7 @@ namespace ArrayLookup\Tests; +use ArrayIterator; use ArrayLookup\Only; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -49,7 +50,7 @@ public static function onceDataProvider(): array // phpcs:enable #[DataProvider('twiceDataProvider')] - public function testTwice(array $data, callable $filter, bool $expected): void + public function testTwice(iterable $data, callable $filter, bool $expected): void { $this->assertSame( $expected, @@ -86,21 +87,41 @@ public static function twiceDataProvider(): array static fn($datum): bool => $datum == 1, false, ], + [ + new ArrayIterator([1]), + static fn($datum): bool => $datum == 1, + false, + ], [ [1, "1"], static fn($datum): bool => $datum == 1, true, ], + [ + new ArrayIterator([1, "1"]), + static fn($datum): bool => $datum == 1, + true, + ], [ [1, true], static fn($datum): bool => $datum == 1, true, ], + [ + new ArrayIterator([1, true]), + static fn($datum): bool => $datum == 1, + true, + ], [ [1, true, "1"], static fn($datum): bool => $datum == 1, false, ], + [ + new ArrayIterator([1, true, "1"]), + static fn($datum): bool => $datum == 1, + false, + ], ]; } From d6e5ebcf334b109132c998a08bd0ddac6572cd9b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 22 Apr 2024 06:44:36 +0700 Subject: [PATCH 6/8] add Counter --- src/Counter.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/Counter.php diff --git a/src/Counter.php b/src/Counter.php new file mode 100644 index 0000000..1889c02 --- /dev/null +++ b/src/Counter.php @@ -0,0 +1,27 @@ +|Traversable $data + */ + public static function count(iterable $data): int + { + // php 8.1 compat + if (is_array($data)) { + return count($data); + } + + return iterator_count($data); + } +} From b817f28863cd4639e32652aed1aa7ecc8509c4c8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 22 Apr 2024 06:44:52 +0700 Subject: [PATCH 7/8] final --- src/Counter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Counter.php b/src/Counter.php index 1889c02..aac1d06 100644 --- a/src/Counter.php +++ b/src/Counter.php @@ -10,7 +10,7 @@ use function is_array; use function iterator_count; -class Counter +final class Counter { /** * @param array|Traversable $data From 10bc72fda12775598cffdcf043c253fed93a8938 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 22 Apr 2024 06:57:29 +0700 Subject: [PATCH 8/8] make Counter class as internal --- src/Counter.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Counter.php b/src/Counter.php index aac1d06..9a57e60 100644 --- a/src/Counter.php +++ b/src/Counter.php @@ -10,6 +10,9 @@ use function is_array; use function iterator_count; +/** + * @internal + */ final class Counter { /**