Skip to content

Commit

Permalink
No need filter on loop when total data is < target total filtered data
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Apr 21, 2024
1 parent d71ac82 commit 7d600ee
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/AtLeast.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Traversable;
use Webmozart\Assert\Assert;

use function count;

final class AtLeast
{
/**
Expand Down Expand Up @@ -48,6 +50,10 @@ private static function atLeastFoundTimes(
// usage must be higher than 0
Assert::greaterThan($maxCount, 0);

if (count($data) < $maxCount) {

Check failure on line 53 in src/AtLeast.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Parameter #1 $value of function count expects array|Countable, iterable<int|string, mixed> given.

Check failure on line 53 in src/AtLeast.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Parameter #1 $value of function count expects array|Countable, iterable<int|string, mixed> given.
return false;
}

$totalFound = 0;
foreach ($data as $key => $datum) {
$isFound = $filter($datum, $key);
Expand Down
6 changes: 6 additions & 0 deletions src/Only.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Traversable;
use Webmozart\Assert\Assert;

use function count;

final class Only
{
/**
Expand Down Expand Up @@ -48,6 +50,10 @@ private static function onlyFoundTimes(
// usage must be higher than 0
Assert::greaterThan($maxCount, 0);

if (count($data) < $maxCount) {

Check failure on line 53 in src/Only.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Parameter #1 $value of function count expects array|Countable, iterable<int|string, mixed> given.

Check failure on line 53 in src/Only.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Parameter #1 $value of function count expects array|Countable, iterable<int|string, mixed> given.
return false;
}

$totalFound = 0;
foreach ($data as $key => $datum) {
$isFound = $filter($datum, $key);
Expand Down
5 changes: 5 additions & 0 deletions tests/AtLeastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
];
}

Expand Down
5 changes: 5 additions & 0 deletions tests/OnlyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
];
}

Expand Down

0 comments on commit 7d600ee

Please sign in to comment.