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

azjezz / psl / 22680264557

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

Pull #610

github

azjezz
perf: optimizations

Signed-off-by: azjezz <azjezz@protonmail.com>
Pull Request #610: perf: replace PSL calls with native PHP builtins and fix O(n2) algorithms

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

45.45
/src/Psl/Dict/filter.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Psl\Dict;
6

7
use Closure;
8

9
use function array_filter;
10
use function is_array;
11

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

41
    if (is_array($iterable)) {
9✔
42
        return array_filter($iterable, $predicate);
9✔
43
    }
44

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

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

UNCOV
54
    return $result;
×
55
}
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