Arr   B
last analyzed

Complexity

Total Complexity 44

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 44
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 246
rs 8.3396

6 Methods

Rating   Name   Duplication   Size   Complexity  
C get() 0 38 11
C set() 0 30 7
D merge() 0 37 10
C mergeAssoc() 0 32 8
C delete() 0 35 7
A __toString() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Arr often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Arr, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Konfig.
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * PHP version 5
9
 *
10
 * @category Library
11
 * @package  Konfig
12
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
13
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
14
 * @link     https://xeriab.github.io/projects/konfig
15
 */
16
17
namespace Exen\Konfig;
18
19
use ArrayAccess;
20
use InvalidArgumentException;
21
22
/**
23
 * Konfig's array utilities class
24
 *
25
 * @category Main
26
 * @package  Konfig
27
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
28
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
29
 * @link     https://xeriab.github.io/projects/konfig
30
 */
31
final class Arr
32
{
33
    /**
34
     * Gets a dot-notated key from an array, with a default value if it does
35
     * not exist.
36
     *
37
     * @param array  $array   The search array
38
     * @param string $key     The dot-notated key or array of keys
39
     * @param string $default The default value
40
     *
41
     * @return mixed
42
     *
43
     * @since              0.1.0
44
     * @codeCoverageIgnore
45
     */
46
    public static function get(array $array, $key, $default = null)
47
    {
48
        if (!is_array($array) && !$array instanceof ArrayAccess) {
49
            throw new InvalidArgumentException(
50
                'First parameter must be an array or ArrayAccess object.'
51
            );
52
        }
53
54
        if (is_null($key)) {
55
            return $array;
56
        }
57
58
        if (is_array($key)) {
59
            $return = [];
60
61
            foreach ($key as $k) {
62
                $return[$k] = self::get($array, $k, $default);
63
            }
64
65
            return $return;
66
        }
67
68
        foreach (explode('.', $key) as $key_part) {
69
            if (($array instanceof ArrayAccess
70
                && isset($array[$key_part])) === false
71
            ) {
72
                if (!is_array($array)
73
                    || !array_key_exists($key_part, $array)
74
                ) {
75
                    return Utils::checkValue($default);
76
                }
77
            }
78
79
            $array = $array[$key_part];
80
        }
81
82
        return $array;
83
    }
84
85
    /**
86
     * Set an array item (dot-notated) to the value.
87
     *
88
     * @param array $array The array to insert it into
89
     * @param mixed $key   The dot-notated key to set or array of keys
90
     * @param mixed $value The value
91
     *
92
     * @return             void Void
93
     * @since              0.1.0
94
     * @codeCoverageIgnore
95
     */
96
    public static function set(array &$array, $key, $value = null)
97
    {
98
        if (is_null($key)) {
99
            $array = $value;
100
101
            return;
102
        }
103
104
        if (is_array($key)) {
105
            foreach ($key as $k => $v) {
106
                self::set($array, $k, $v);
107
            }
108
        } else {
109
            $keys = explode('.', $key);
110
111
            while (count($keys) > 1) {
112
                $key = array_shift($keys);
113
114
                if (!isset($array[$key])
115
                    || !is_array($array[$key])
116
                ) {
117
                    $array[$key] = [];
118
                }
119
120
                $array = &$array[$key];
121
            }
122
123
            $array[array_shift($keys)] = $value;
124
        }
125
    }
126
127
    /**
128
     * Merge two arrays recursively.
129
     *
130
     * @throws InvalidArgumentException
131
     *
132
     * @return array
133
     *
134
     * @since              0.1.0
135
     * @codeCoverageIgnore
136
     */
137
    public static function merge()
138
    {
139
        $array = func_get_arg(0);
140
        $arrays = array_slice(func_get_args(), 1);
141
142
        if (!is_array($array)) {
143
            throw new InvalidArgumentException(
144
                'Exen\Konfig\Arr::merge() - all arguments must be arrays.'
145
            );
146
        }
147
148
        foreach ($arrays as $arr) {
149
            if (!is_array($arr)) {
150
                throw new InvalidArgumentException(
151
                    'Exen\Konfig\Arr::merge() - all arguments must be arrays.'
152
                );
153
            }
154
155
            foreach ($arr as $key => $value) {
156
                // Numeric keys are appended
157
                if (is_int($key)) {
158
                    array_key_exists($key, $array) ?
159
                        array_push($array, $value) :
160
                        $array[$key] = $value;
161
                } elseif (is_array($value)
162
                    && array_key_exists($key, $array)
163
                    && is_array($array[$key])
164
                ) {
165
                    $array[$key] = self::merge($array[$key], $value);
166
                } else {
167
                    $array[$key] = $value;
168
                }
169
            }
170
        }
171
172
        return $array;
173
    }
174
175
    /**
176
     * Merge two arrays recursively | assoc.
177
     *
178
     * @throws InvalidArgumentException
179
     *
180
     * @return array
181
     *
182
     * @since              0.1.0
183
     * @codeCoverageIgnore
184
     */
185
    public static function mergeAssoc()
186
    {
187
        $array = func_get_arg(0);
188
        $arrays = array_slice(func_get_args(), 1);
189
190
        if (!is_array($array)) {
191
            throw new InvalidArgumentException(
192
                'Exen\Konfig\Arr::mergeAssoc() - all arguments must be arrays.'
193
            );
194
        }
195
196
        foreach ($arrays as $arr) {
197
            if (!is_array($arr)) {
198
                throw new InvalidArgumentException(
199
                    'Exen\Konfig\Arr::mergeAssoc() - all arguments must be arrays.'
200
                );
201
            }
202
203
            foreach ($arr as $key => $value) {
204
                if (is_array($value)
205
                    && array_key_exists($key, $array)
206
                    && is_array($array[$key])
207
                ) {
208
                    $array[$key] = self::mergeAssoc($array[$key], $value);
209
                } else {
210
                    $array[$key] = $value;
211
                }
212
            }
213
        }
214
215
        return $array;
216
    }
217
218
    /**
219
     * Un-sets dot-notated key from an array.
220
     *
221
     * @param array $array The search array
222
     * @param mixed $key   The dot-notated key or array of keys
223
     *
224
     * @return mixed
225
     *
226
     * @since              0.1.0
227
     * @codeCoverageIgnore
228
     */
229
    public static function delete(array &$array, $key)
230
    {
231
        if (is_null($key)) {
232
            return false;
233
        }
234
235
        if (is_array($key)) {
236
            $return = [];
237
238
            foreach ($key as $k) {
239
                $return[$k] = self::delete($array, $k);
240
            }
241
242
            return $return;
243
        }
244
245
        $key_parts = explode('.', $key);
246
247
        if (!is_array($array)
248
            || !array_key_exists($key_parts[0], $array)
249
        ) {
250
            return false;
251
        }
252
253
        $this_key = array_shift($key_parts);
254
255
        if (!empty($key_parts)) {
256
            $key = implode('.', $key_parts);
257
            return self::delete($array[$this_key], $key);
258
        } else {
259
            unset($array[$this_key]);
260
        }
261
262
        return true;
263
    }
264
265
    /**
266
     * __toString.
267
     *
268
     * @return             string
269
     * @since              0.1.2
270
     * @codeCoverageIgnore
271
     */
272
    public function __toString()
273
    {
274
        return 'Exen\Konfig\Arr' . PHP_EOL;
275
    }
276
}
277
278
// END OF ./src/Arr.php FILE
279