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

No need filter on loop when total data is < target total filtered data #23

Closed
wants to merge 8 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
4 changes: 4 additions & 0 deletions src/AtLeast.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ private static function atLeastFoundTimes(
// usage must be higher than 0
Assert::greaterThan($maxCount, 0);

if (Counter::count($data) < $maxCount) {
return false;
}

$totalFound = 0;
foreach ($data as $key => $datum) {
$isFound = $filter($datum, $key);
Expand Down
30 changes: 30 additions & 0 deletions src/Counter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace ArrayLookup;

use Traversable;

use function count;
use function is_array;
use function iterator_count;

/**
* @internal
*/
final class Counter
{
/**
* @param array<int|string, mixed>|Traversable<int|string, mixed> $data
*/
public static function count(iterable $data): int
{
// php 8.1 compat
if (is_array($data)) {
return count($data);
}

return iterator_count($data);
}
}
4 changes: 4 additions & 0 deletions src/Only.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ private static function onlyFoundTimes(
// usage must be higher than 0
Assert::greaterThan($maxCount, 0);

if (Counter::count($data) < $maxCount) {
return false;
}

$totalFound = 0;
foreach ($data as $key => $datum) {
$isFound = $filter($datum, $key);
Expand Down
43 changes: 42 additions & 1 deletion tests/AtLeastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace ArrayLookup\Tests;

use ArrayIterator;
use ArrayLookup\AtLeast;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -78,6 +79,46 @@ 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,
],
[
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,
],
];
}

Expand Down
43 changes: 42 additions & 1 deletion tests/OnlyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace ArrayLookup\Tests;

use ArrayIterator;
use ArrayLookup\Only;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -81,6 +82,46 @@ 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,
],
[
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,
],
];
}

Expand Down
Loading