• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

azjezz / psl / 22680098232

04 Mar 2026 05:02PM UTC coverage: 97.34% (-1.0%) from 98.375%
22680098232

push

github

azjezz
perf: replace PSL calls with native PHP builtins and fix O(n2) algorithms

Signed-off-by: azjezz <azjezz@protonmail.com>

319 of 355 new or added lines in 90 files covered. (89.86%)

64 existing lines in 12 files now uncovered.

9258 of 9511 relevant lines covered (97.34%)

34.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

40.0
/src/Psl/Vec/filter.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Psl\Vec;
6

7
use Closure;
8

9
use function array_filter;
10
use function array_values;
11
use function is_array;
12

13
/**
14
 * Returns a vec containing only the values for which the given predicate
15
 * returns `true`.
16
 *
17
 * The default predicate is casting the value to boolean.
18
 *
19
 * Example:
20
 *
21
 *      Vec\filter(['', '0', 'a', 'b'])
22
 *      => Vec('a', 'b')
23
 *
24
 *      Vec\filter(['foo', 'bar', 'baz', 'qux'], fn(string $value): bool => Str\contains($value, 'a'));
25
 *      => Vec('bar', 'baz')
26
 *
27
 * @template T
28
 *
29
 * @param iterable<T> $iterable
30
 * @param (Closure(T): bool)|null $predicate
31
 *
32
 * @return list<T>
33
 */
34
function filter(iterable $iterable, null|Closure $predicate = null): array
35
{
36
    $predicate ??=
5✔
37
        /**
38
         * @param T $value
39
         */
40
        static fn(mixed $value): bool => (bool) $value;
5✔
41

42
    if (is_array($iterable)) {
5✔
43
        return array_values(array_filter($iterable, $predicate));
5✔
44
    }
45

UNCOV
46
    $result = [];
×
UNCOV
47
    foreach ($iterable as $v) {
×
UNCOV
48
        if (!$predicate($v)) {
×
UNCOV
49
            continue;
×
50
        }
51

UNCOV
52
        $result[] = $v;
×
53
    }
54

UNCOV
55
    return $result;
×
56
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc