Completed
Push — master ( cd157d...8cb219 )
by Arnold
06:18
created

iterable_before()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 5
nop 3
dl 0
loc 11
rs 9.6111
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Improved;
4
5
/**
6
 * Get elements until a match is found.
7
 *
8
 * @param iterable $iterable
9
 * @param callable $matcher
10
 * @param bool     $including
11
 * @return \Generator
12
 */
13
function iterable_before(iterable $iterable, callable $matcher, bool $including = false): \Generator
14
{
15
    foreach ($iterable as $key => $value) {
16
        $matched = (bool)call_user_func($matcher, $value, $key);
17
18
        if (!$matched || $including) {
19
            yield $key => $value;
20
        }
21
22
        if ($matched) {
23
            return;
24
        }
25
    }
26
}
27