functions.php ➔ array_path()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 3
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Northwoods\Config;
5
6
/**
7
 * @param mixed $default
8
 * @return mixed
9
 */
10
function array_path(array $config, string $dotPath, $default = null)
11
{
12
    // Working from the first key, descend into the array until nothing is found
13 3
    return array_reduce(
14 3
        explode('.', $dotPath),
15
        function ($config, $key) use ($default) {
16 3
            return isset($config[$key]) ? $config[$key] : $default;
17 3
        },
18 3
        $config
19
    );
20
}
21
22
/**
23
 * @param mixed $value
24
 */
25
function array_path_set(array $config, string $dotPath, $value): array
26
{
27
    // Work backward from the last key, wrapping each key in an array
28 3
    $replace = array_reduce(
29 3
        array_reverse(explode('.', $dotPath)),
30
        function ($value, $key) {
31 3
            return [$key => $value];
32 3
        },
33 3
        $value
34
    );
35
36
    // Overwrite the array with the replacement
37 3
    return array_replace_recursive($config, $replace);
38
}
39