ArrayUtility::setNodes()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
ccs 0
cts 13
cp 0
rs 8.6346
cc 7
nc 6
nop 2
crap 56
1
<?php
2
3
/**
4
 * Arrays utility.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Utility;
9
10
/**
11
 * Arrays utility.
12
 */
13
class ArrayUtility
14
{
15
    /**
16
     * Set a node in the array.
17
     *
18
     * @see http://www.php.net/manual/de/function.array-walk-recursive.php#106340
19
     */
20
    public static function setNodes(array $data, array &$array): void
21
    {
22
        $separator = '|';
23
        foreach ($data as $name => $value) {
24
            if (false === mb_strpos($name, $separator)) {
25
                $array[$name] = $value;
26
            } else {
27
                $keys = explode($separator, $name);
28
                $optTree = &$array;
29
                while (($key = array_shift($keys))) {
30
                    if ($keys) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $keys of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
31
                        if (!isset($optTree[$key]) || !\is_array($optTree[$key])) {
32
                            $optTree[$key] = [];
33
                        }
34
                        $optTree = &$optTree[$key];
35
                    } else {
36
                        $optTree[$key] = $value;
37
                    }
38
                }
39
            }
40
        }
41
    }
42
43
    /**
44
     * Merge the Array Smart.
45
     *
46
     * @return array
47
     */
48
    public static function mergeRecursiveDistinct(array &$array1, array &$array2)
49
    {
50
        $merged = $array1;
51
52
        foreach ($array2 as $key => &$value) {
53
            if (\is_array($value) && isset($merged[$key]) && \is_array($merged[$key])) {
54
                $merged[$key] = self::mergeRecursiveDistinct($merged[$key], $value);
55
            } else {
56
                $merged[$key] = $value;
57
            }
58
        }
59
60
        return $merged;
61
    }
62
}
63