Passed
Push — master ( e65d14...ed7f33 )
by Arnold
01:53
created

iterable_find_key()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Improved;
6
7
/**
8
 * Get the first element that matches a condition and return the key.
9
 * Returns null if no element is found.
10
 *
11
 * @param iterable $iterable
12
 * @param callable $matcher
13
 * @return mixed
14
 */
15
function iterable_find_key(iterable $iterable, callable $matcher)
16
{
17 14
    foreach ($iterable as $key => $value) {
18 13
        if ((bool)call_user_func($matcher, $value, $key)) {
19 13
            return $key;
20
        }
21
    }
22
23 5
    return null;
24
}
25