Completed
Push — master ( 5dfc66...6e95cd )
by Bryan
03:24
created

NestedArray   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 95
ccs 18
cts 18
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeDeep() 0 4 1
C mergeDeepArray() 0 28 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 5
    public static function mergeDeep()
54
    {
55 5
        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  $preserve_integer_keys
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 20
    public static function mergeDeepArray(
0 ignored issues
show
Coding Style Naming introduced by
The parameter $preserve_integer_keys is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
85
        array $arrays,
86
        $preserve_integer_keys = false
87
    ) {
88 20
        $result = array();
89 20
        foreach ($arrays as $array) {
90 20
            foreach ($array as $key => $value) {
91
                // Renumber integer keys as array_merge_recursive() does
92
                // unless $preserve_integer_keys is set to TRUE. Note that PHP
93
                // automatically converts array keys that are integer strings
94
                // (e.g., '1') to integers.
95 20
                if (is_integer($key) && !$preserve_integer_keys) {
96 20
                    $result[] = $value;
97 20
                } elseif (isset($result[$key]) &&
98 20
                    is_array($result[$key]) &&
99 20
                    is_array($value)
100 20
                ) {
101
                    // Recurse when both values are arrays.
102 20
                    $result[$key] = self::mergeDeepArray(array($result[$key], $value), $preserve_integer_keys);
103 20
                } else {
104
                    // Otherwise, use the latter value, overriding any
105
                    // previous value.
106 20
                    $result[$key] = $value;
107
                }
108 20
            }
109 20
        }
110 20
        return $result;
111
    }
112
}
113
// vim:sw=4:ts=4:sts=4:et:
114