getin.php ➔ getin()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 3
Metric Value
cc 3
eloc 10
c 3
b 0
f 3
nc 1
nop 0
dl 0
loc 20
ccs 9
cts 9
cp 1
crap 3
rs 9.4285
1
<?php
2
3
namespace Sergiors\Functional;
4
5
const getin = '\Sergiors\Functional\getin';
6
7
/**
8
 * Returns the value in a nested associative structure,
9
 * where $ks is a sequence of keys. Returns false if the key
10
 * is not present, or the $notfound value if supplied.
11
 *
12
 * @author Sérgio Rafael Siqueira <[email protected]>
13
 *
14
 * @link https://clojuredocs.org/clojure.core/get-in
15
 *
16
 * @return mixed
17
 */
18
function getin(/* ...$args */)
19
{
20 2
    $args = func_get_args();
21
22
    $getin = function (array $xs, array $ks, $notfound = false) {
23 2
        if (!array_key_exists(0, $ks)) {
24 1
            return $notfound;
25
        }
26
27 2
        $x = get($xs, $ks[0], $notfound);
28
29 2
        if (is_array($x)) {
30 2
            return getin($x, array_slice($ks, 1), $notfound);
31
        }
32
33 2
        return $x;
34 2
    };
35
36 2
    return call_user_func_array(partial($getin), $args);
37
}
38