DotChainOperator::update()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 2
eloc 7
c 2
b 1
f 1
nc 2
nop 3
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
4
namespace TKuni\PhpNormalizer\Helper;
5
6
use Closure;
7
8
class DotChainOperator
9
{
10 14
    public static function update($arr, $key, $val)
11
    {
12 14
        $separatedKeys = explode('.', $key);
13 14
        if (is_callable($val)) {
14 10
            self::_v($arr, $separatedKeys, $val);
15
        } else {
16
            self::_v($arr, $separatedKeys, function($in) use ($val) {
0 ignored issues
show
Unused Code introduced by
The parameter $in is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

16
            self::_v($arr, $separatedKeys, function(/** @scrutinizer ignore-unused */ $in) use ($val) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
17 4
                return $val;
18 4
            });
19
        }
20
21 14
        return $arr;
22
    }
23
24 14
    private static function _v(&$arr, array $keys, Closure $filter, $i = 0)
25
    {
26 14
        if (empty($keys[$i])) {
27 14
            $arr = $filter($arr);
28 14
            return;
29
        }
30
31 14
        $currKey = $keys[$i];
32
33 14
        if ($currKey === '*') {
34 10
            self::_h($arr, $keys, $filter, $i);
35
        } else {
36 14
            self::_v($arr[$currKey], $keys, $filter, $i + 1);
37
        }
38 14
    }
39
40 10
    private static function _h(&$arr, array $keys, Closure $filter, $i)
41
    {
42 10
        foreach ($arr as &$item) {
43 10
            self::_v($item, $keys, $filter, $i + 1);
44
        }
45
    }
46
}