NestedArray   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 98
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeDeep() 0 4 1
B mergeDeepArray() 0 31 8
1
<?php
2
/**
3
 * This file is part of the Composer Merge plugin.
4
 *
5
 * Copyright (C) 2015 Bryan Davis, Wikimedia Foundation, and contributors
6
 *
7
 * This software may be modified and distributed under the terms of the MIT
8
 * license. See the LICENSE file for details.
9
 */
10
11
namespace Wikimedia\Composer\Merge;
12
13
/**
14
 * Adapted from
15
 * http://cgit.drupalcode.org/drupal/tree/core/lib/Drupal/Component/Utility/NestedArray.php
16
 * @ f86a4d650d5af0b82a3981e09977055fa63f6f2e
17
 */
18
class NestedArray
19
{
20
21
    /**
22
     * Merges multiple arrays, recursively, and returns the merged array.
23
     *
24
     * This function is similar to PHP's array_merge_recursive() function, but
25
     * it handles non-array values differently. When merging values that are
26
     * not both arrays, the latter value replaces the former rather than
27
     * merging with it.
28
     *
29
     * Example:
30
     *
31
     * @code
32
     * $link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => t('X'), 'class' => array('a', 'b')));
33
     * $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('c', 'd')));
34
     *
35
     * // This results in array('fragment' => array('x', 'y'), 'attributes' =>
36
     * // array('title' => array(t('X'), t('Y')), 'class' => array('a', 'b',
37
     * // 'c', 'd'))).
38
     * $incorrect = array_merge_recursive($link_options_1, $link_options_2);
39
     *
40
     * // This results in array('fragment' => 'y', 'attributes' =>
41
     * // array('title' => t('Y'), 'class' => array('a', 'b', 'c', 'd'))).
42
     * $correct = NestedArray::mergeDeep($link_options_1, $link_options_2);
43
     * @endcode
44
     *
45
     * @param array ...
46
     *   Arrays to merge.
47
     *
48
     * @return array
49
     *   The merged array.
50
     *
51
     * @see NestedArray::mergeDeepArray()
52
     */
53 15
    public static function mergeDeep()
54
    {
55 15
        return self::mergeDeepArray(func_get_args());
56
    }
57
58
    /**
59
     * Merges multiple arrays, recursively, and returns the merged array.
60
     *
61
     * This function is equivalent to NestedArray::mergeDeep(), except the
62
     * input arrays are passed as a single array parameter rather than
63
     * a variable parameter list.
64
     *
65
     * The following are equivalent:
66
     * - NestedArray::mergeDeep($a, $b);
67
     * - NestedArray::mergeDeepArray(array($a, $b));
68
     *
69
     * The following are also equivalent:
70
     * - call_user_func_array('NestedArray::mergeDeep', $arrays_to_merge);
71
     * - NestedArray::mergeDeepArray($arrays_to_merge);
72
     *
73
     * @param array $arrays
74
     *   An arrays of arrays to merge.
75
     * @param bool  $preserveIntegerKeys
76
     *   (optional) If given, integer keys will be preserved and merged
77
     *   instead of appended. Defaults to false.
78
     *
79
     * @return array
80
     *   The merged array.
81
     *
82
     * @see NestedArray::mergeDeep()
83
     */
84 30
    public static function mergeDeepArray(
85
        array $arrays,
86
        $preserveIntegerKeys = false
87
    ) {
88 30
        $result = array();
89 30
        foreach ($arrays as $array) {
90 30
            foreach ($array as $key => $value) {
91
                // Renumber integer keys as array_merge_recursive() does
92
                // unless $preserveIntegerKeys is set to TRUE. Note that PHP
93
                // automatically converts array keys that are integer strings
94
                // (e.g., '1') to integers.
95 30
                if (is_integer($key) && !$preserveIntegerKeys) {
96 30
                    $result[] = $value;
97 30
                } elseif (isset($result[$key]) &&
98 30
                    is_array($result[$key]) &&
99 30
                    is_array($value)
100 6
                ) {
101
                    // Recurse when both values are arrays.
102 30
                    $result[$key] = self::mergeDeepArray(
103 30
                        array($result[$key], $value),
104 24
                        $preserveIntegerKeys
105 6
                    );
106 6
                } else {
107
                    // Otherwise, use the latter value, overriding any
108
                    // previous value.
109 30
                    $result[$key] = $value;
110
                }
111 6
            }
112 6
        }
113 30
        return $result;
114
    }
115
}
116
// vim:sw=4:ts=4:sts=4:et:
117