Test Failed
Push — tmp ( 15f615...89cc97 )
by Paul
10:31 queued 04:40
created

Arr   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 265
Duplicated Lines 0 %

Test Coverage

Coverage 51.35%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 95
dl 0
loc 265
ccs 57
cts 111
cp 0.5135
rs 8.64
c 3
b 0
f 1
wmc 47

18 Methods

Rating   Name   Duplication   Size   Complexity  
A consolidate() 0 11 5
A compare() 0 5 1
A convertFromDotNotation() 0 7 2
A flatten() 0 16 5
A insert() 0 12 3
A insertBefore() 0 3 1
A get() 0 15 4
A convertFromString() 0 8 3
A prefixKeys() 0 15 4
A unprefixKeys() 0 3 1
A unique() 0 5 2
A uniqueInt() 0 3 1
A removeEmptyValues() 0 12 4
A isIndexedAndFlat() 0 6 3
A reindex() 0 5 2
A set() 0 15 3
A insertAfter() 0 3 1
A prepend() 0 7 2

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.

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
namespace GeminiLabs\SiteReviews\Helpers;
4
5
use GeminiLabs\SiteReviews\Helper;
6
7
class Arr
8
{
9
    /**
10
     * @return bool
11
     */
12 1
    public static function compare(array $arr1, array $arr2)
13
    {
14 1
        sort($arr1);
15 1
        sort($arr2);
16 1
        return $arr1 == $arr2;
17
    }
18
19
    /**
20
     * @param mixed $array
21
     * @param bool $explode
22
     * @return array
23
     */
24 9
    public static function consolidate($array, $explode = true)
25
    {
26 9
        if (is_string($array) && $explode) {
27
            $array = static::convertFromString($array, function ($item) {
28
                return '' !== trim($item);
29
            });
30
        }
31 9
        if (is_object($array)) {
32
            return get_object_vars($array);
33
        }
34 9
        return is_array($array) ? $array : [];
35
    }
36
37
    /**
38
     * @return array
39
     */
40 7
    public static function convertFromDotNotation(array $array)
41
    {
42 7
        $results = [];
43 7
        foreach ($array as $path => $value) {
44 7
            $results = static::set($results, $path, $value);
45
        }
46 7
        return $results;
47
    }
48
49
    /**
50
     * @param string|array $string
51
     * @param mixed $callback
52
     * @return array
53
     */
54
    public static function convertFromString($string, $callback = null)
55
    {
56
        if (!is_array($array = $string)) {
57
            $array = array_map('trim', explode(',', $string));
0 ignored issues
show
Bug introduced by
It seems like $string can also be of type array; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

57
            $array = array_map('trim', explode(',', /** @scrutinizer ignore-type */ $string));
Loading history...
58
        }
59
        return $callback
60
            ? array_filter($array, $callback)
61
            : array_filter($array);
62
    }
63
64
    /**
65
     * @param bool $flattenValue
66
     * @param string $prefix
67
     * @return array
68
     */
69 1
    public static function flatten(array $array, $flattenValue = false, $prefix = '')
70
    {
71 1
        $result = [];
72 1
        foreach ($array as $key => $value) {
73 1
            $newKey = ltrim($prefix.'.'.$key, '.');
74 1
            if (static::isIndexedAndFlat($value)) {
75 1
                if ($flattenValue) {
76 1
                    $value = '['.implode(', ', $value).']';
77
                }
78 1
            } elseif (is_array($value)) {
79 1
                $result = array_merge($result, static::flatten($value, $flattenValue, $newKey));
80 1
                continue;
81
            }
82 1
            $result[$newKey] = $value;
83
        }
84 1
        return $result;
85
    }
86
87
    /**
88
     * Get a value from an array of values using a dot-notation path as reference.
89
     * @param mixed $data
90
     * @param string $path
91
     * @param mixed $fallback
92
     * @return mixed
93
     */
94 7
    public static function get($data, $path = '', $fallback = '')
95
    {
96 7
        $data = static::consolidate($data);
97 7
        $keys = explode('.', $path);
98 7
        foreach ($keys as $key) {
99 7
            if (!isset($data[$key])) {
100 7
                return $fallback;
101
            }
102 7
            if (is_object($data[$key])) {
103
                $data = static::consolidate($data[$key]);
104
                continue;
105
            }
106 7
            $data = $data[$key];
107
        }
108 7
        return $data;
109
    }
110
111
    /**
112
     * @param string $key
113
     * @return array
114
     */
115
    public static function insertAfter($key, array $array, array $insert)
116
    {
117
        return static::insert($array, $insert, $key, 'after');
118
    }
119
120
    /**
121
     * @param string $key
122
     * @return array
123
     */
124
    public static function insertBefore($key, array $array, array $insert)
125
    {
126
        return static::insert($array, $insert, $key, 'before');
127
    }
128
129
    /**
130
     * @param string $key
131
     * @param string $position
132
     * @return array
133
     */
134
    public static function insert(array $array, array $insert, $key, $position = 'before')
135
    {
136
        $keyPosition = intval(array_search($key, array_keys($array)));
137
        if ('after' == $position) {
138
            ++$keyPosition;
139
        }
140
        if (false !== $keyPosition) {
141
            $result = array_slice($array, 0, $keyPosition);
142
            $result = array_merge($result, $insert);
143
            return array_merge($result, array_slice($array, $keyPosition));
144
        }
145
        return array_merge($array, $insert);
146
    }
147
148
    /**
149
     * @param mixed $array
150
     * @return bool
151
     */
152 2
    public static function isIndexedAndFlat($array)
153
    {
154 2
        if (!is_array($array) || array_filter($array, 'is_array')) {
155 2
            return false;
156
        }
157 2
        return wp_is_numeric_array($array);
158
    }
159
160
    /**
161
     * @param bool $prefixed
162
     * @return array
163
     */
164
    public static function prefixKeys(array $values, $prefixed = true)
165
    {
166
        $trim = '_';
167
        $prefix = $prefixed
168
            ? $trim
169
            : '';
170
        $prefixed = [];
171
        foreach ($values as $key => $value) {
172
            $key = trim($key);
173
            if (0 === strpos($key, $trim)) {
174
                $key = substr($key, strlen($trim));
175
            }
176
            $prefixed[$prefix.$key] = $value;
177
        }
178
        return $prefixed;
179
    }
180
181
    /**
182
     * @param array $array
183
     * @param mixed $value
184
     * @param mixed $key
185
     * @return array
186
     */
187
    public static function prepend($array, $value, $key = null)
188
    {
189
        if (!is_null($key)) {
190
            return [$key => $value] + $array;
191
        }
192
        array_unshift($array, $value);
193
        return $array;
194
    }
195
196
    /**
197
     * @param mixed $array
198
     * @return array
199
     */
200
    public static function reindex($array)
201
    {
202
        return static::isIndexedAndFlat($array)
203
            ? array_values($array)
204
            : $array;
205
    }
206
207
    /**
208
     * @return array
209
     */
210 1
    public static function removeEmptyValues(array $array)
211
    {
212 1
        $result = [];
213 1
        foreach ($array as $key => $value) {
214 1
            if (Helper::isEmpty($value)) {
215 1
                continue;
216
            }
217 1
            $result[$key] = is_array($value)
218 1
                ? static::removeEmptyValues($value)
219 1
                : $value;
220
        }
221 1
        return $result;
222
    }
223
224
    /**
225
     * Set a value to an array of values using a dot-notation path as reference.
226
     * @param mixed $data
227
     * @param string $path
228
     * @param mixed $value
229
     * @return array
230
     */
231 8
    public static function set($data, $path, $value)
232
    {
233 8
        $token = strtok($path, '.');
234 8
        $ref = &$data;
235 8
        while (false !== $token) {
236 8
            if (is_object($ref)) {
237
                $ref = &$ref->$token;
238
            } else {
239 8
                $ref = static::consolidate($ref);
240 8
                $ref = &$ref[$token];
241
            }
242 8
            $token = strtok('.');
243
        }
244 8
        $ref = $value;
245 8
        return $data;
246
    }
247
248
    /**
249
     * @return array
250
     */
251
    public static function unique(array $values)
252
    {
253
        return static::isIndexedAndFlat($values)
254
            ? array_values(array_filter(array_unique($values)))
255
            : $values;
256
    }
257
258
    /**
259
     * @return array
260
     */
261
    public static function uniqueInt(array $values)
262
    {
263
        return static::unique(array_map('absint', $values));
264
    }
265
266
    /**
267
     * @return array
268
     */
269
    public static function unprefixKeys(array $values)
270
    {
271
        return static::prefixKeys($values, false);
272
    }
273
}
274