Skip to content

Commit d0adef6

Browse files
committed
Version 2: 🚀 Faster process with early validate filter before loop
1 parent 3029f92 commit d0adef6

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

rector.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6-
use Rector\PHPUnit\Set\PHPUnitSetList;
6+
use Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromBooleanStrictReturnsRector;
77

88
return RectorConfig::configure()
99
->withPhpSets(php81: true)
@@ -15,8 +15,11 @@
1515
privatization: true,
1616
typeDeclarations: true
1717
)
18-
->withSets([
19-
PHPUnitSetList::PHPUNIT_100,
18+
->withComposerBased(phpunit: true)
19+
->withSkip([
20+
BoolReturnTypeFromBooleanStrictReturnsRector::class => [
21+
__DIR__ . '/tests/FilterTest.php',
22+
],
2023
])
2124
->withParallel()
2225
->withRootFiles()

src/AtLeast.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace ArrayLookup;
66

7+
use ArrayLookup\Assert\Filter;
78
use Traversable;
89
use Webmozart\Assert\Assert;
910

@@ -47,14 +48,13 @@ private static function atLeastFoundTimes(
4748
): bool {
4849
// usage must be higher than 0
4950
Assert::greaterThan($maxCount, 0);
51+
// filter must be a callable with bool return type
52+
Filter::boolean($filter);
5053

5154
$totalFound = 0;
5255
foreach ($data as $key => $datum) {
5356
$isFound = $filter($datum, $key);
5457

55-
// returns of callable must be bool
56-
Assert::boolean($isFound);
57-
5858
if (! $isFound) {
5959
continue;
6060
}

src/Collector.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace ArrayLookup;
66

7+
use ArrayLookup\Assert\Filter;
78
use Traversable;
89
use Webmozart\Assert\Assert;
910

@@ -71,6 +72,11 @@ public function getResults(): array
7172
$collectedData = [];
7273
$isCallableWhen = is_callable($this->when);
7374

75+
if (is_callable($this->when)) {
76+
// filter must be a callable with bool return type
77+
Filter::boolean($this->when);
78+
}
79+
7480
foreach ($this->data as $key => $datum) {
7581
if ($isCallableWhen) {
7682
/**
@@ -79,8 +85,6 @@ public function getResults(): array
7985
$when = $this->when;
8086
$isFound = ($when)($datum, $key);
8187

82-
Assert::boolean($isFound);
83-
8488
if (! $isFound) {
8589
continue;
8690
}

src/Finder.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace ArrayLookup;
66

77
use ArrayIterator;
8+
use ArrayLookup\Assert\Filter;
89
use ArrayObject;
910
use Traversable;
1011
use Webmozart\Assert\Assert;
@@ -24,12 +25,12 @@ final class Finder
2425
*/
2526
public static function first(iterable $data, callable $filter, bool $returnKey = false): mixed
2627
{
28+
// filter must be a callable with bool return type
29+
Filter::boolean($filter);
30+
2731
foreach ($data as $key => $datum) {
2832
$isFound = $filter($datum, $key);
2933

30-
// returns of callable must be bool
31-
Assert::boolean($isFound);
32-
3334
if (! $isFound) {
3435
continue;
3536
}
@@ -71,6 +72,9 @@ public static function last(
7172
// ensure data is array for end(), key(), current(), prev() usage
7273
Assert::isArray($data);
7374

75+
// filter must be a callable with bool return type
76+
Filter::boolean($filter);
77+
7478
// Use end(), key(), current(), prev() usage instead of array_reverse()
7579
// to avoid immediatelly got "Out of memory" on many data
7680
// see https://3v4l.org/IHo2H vs https://3v4l.org/Wqejc
@@ -91,9 +95,6 @@ public static function last(
9195
$current = current($data);
9296
$isFound = $filter($current, $key);
9397

94-
// returns of callable must be bool
95-
Assert::boolean($isFound);
96-
9798
if (! $isFound) {
9899
// go to previous row
99100
prev($data);
@@ -133,12 +134,12 @@ public static function rows(
133134
$newKey = 0;
134135
$totalFound = 0;
135136

137+
// filter must be a callable with bool return type
138+
Filter::boolean($filter);
139+
136140
foreach ($data as $key => $datum) {
137141
$isFound = $filter($datum, $key);
138142

139-
// returns of callable must be bool
140-
Assert::boolean($isFound);
141-
142143
if (! $isFound) {
143144
continue;
144145
}

src/Only.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace ArrayLookup;
66

7+
use ArrayLookup\Assert\Filter;
78
use Traversable;
89
use Webmozart\Assert\Assert;
910

@@ -47,14 +48,13 @@ private static function onlyFoundTimes(
4748
): bool {
4849
// usage must be higher than 0
4950
Assert::greaterThan($maxCount, 0);
51+
// filter must be a callable with bool return type
52+
Filter::boolean($filter);
5053

5154
$totalFound = 0;
5255
foreach ($data as $key => $datum) {
5356
$isFound = $filter($datum, $key);
5457

55-
// returns of callable must be bool
56-
Assert::boolean($isFound);
57-
5858
if (! $isFound) {
5959
continue;
6060
}

0 commit comments

Comments
 (0)