Total Complexity | 63 |
Total Lines | 243 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |
||
11 | class Arr |
||
12 | { |
||
13 | public static function add(array $array, $key, $value) |
||
14 | { |
||
15 | if (is_null(static::get($array, $key))) { |
||
16 | static::set($array, $key, $value); |
||
17 | } |
||
18 | return $array; |
||
19 | } |
||
20 | |||
21 | public static function crossJoin(...$arrays) |
||
22 | { |
||
23 | $results = [[]]; |
||
24 | foreach ($arrays as $index => $array) { |
||
25 | $append = []; |
||
26 | foreach ($results as $product) { |
||
27 | foreach ($array as $item) { |
||
28 | $product[$index] = $item; |
||
29 | $append[] = $product; |
||
|
|||
30 | } |
||
31 | } |
||
32 | $results = $append; |
||
33 | } |
||
34 | return $results; |
||
35 | } |
||
36 | |||
37 | public static function divide(array $array) |
||
38 | { |
||
39 | return [array_keys($array), array_values($array)]; |
||
40 | } |
||
41 | |||
42 | public static function dot(array $array, $prepend = '') |
||
43 | { |
||
44 | $results = []; |
||
45 | foreach ($array as $key => $value) { |
||
46 | if (is_array($value) && !empty($value)) { |
||
47 | $results = array_merge($results, static::dot($value, $prepend.$key.'.')); |
||
48 | } else { |
||
49 | $results[$prepend.$key] = $value; |
||
50 | } |
||
51 | } |
||
52 | return $results; |
||
53 | } |
||
54 | |||
55 | public static function except(array $array, $keys) |
||
59 | } |
||
60 | |||
61 | public static function exists(array $array, $key) |
||
62 | { |
||
63 | return array_key_exists($key, $array); |
||
64 | } |
||
65 | |||
66 | public static function first(array $array, callable $callback = null, $default = null) |
||
67 | { |
||
68 | if (is_null($callback)) { |
||
69 | if (empty($array)) { |
||
70 | return $default; |
||
71 | } |
||
72 | foreach ($array as $item) { |
||
73 | return $item; |
||
74 | } |
||
75 | } |
||
76 | foreach ($array as $key => $value) { |
||
77 | if (call_user_func($callback, $value, $key)) { |
||
78 | return $value; |
||
79 | } |
||
80 | } |
||
81 | return $default; |
||
82 | } |
||
83 | |||
84 | public static function last(array $array, callable $callback = null, $default = null) |
||
85 | { |
||
86 | if (is_null($callback)) { |
||
87 | return empty($array) ? $default : end($array); |
||
88 | } |
||
89 | return static::first(array_reverse($array, true), $callback, $default); |
||
90 | } |
||
91 | |||
92 | public static function flatten(array $array, $depth = INF) |
||
103 | } |
||
104 | |||
105 | public static function forget(array &$array, $keys) |
||
106 | { |
||
107 | $original = &$array; |
||
108 | $keys = (array) $keys; |
||
109 | if (0 === count($keys)) { |
||
110 | return; |
||
111 | } |
||
112 | foreach ($keys as $key) { |
||
113 | // if the exact key exists in the top-level, remove it |
||
114 | if (static::exists($array, $key)) { |
||
115 | unset($array[$key]); |
||
116 | continue; |
||
117 | } |
||
118 | $parts = explode('.', $key); |
||
119 | // clean up before each pass |
||
120 | $array = &$original; |
||
121 | while (count($parts) > 1) { |
||
122 | $part = array_shift($parts); |
||
123 | if (isset($array[$part]) && is_array($array[$part])) { |
||
124 | $array = &$array[$part]; |
||
125 | } else { |
||
126 | continue 2; |
||
127 | } |
||
128 | } |
||
129 | unset($array[array_shift($parts)]); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | public static function get(array $array, $key, $default = null) |
||
149 | } |
||
150 | |||
151 | public static function has(array $array, $keys) |
||
152 | { |
||
153 | if (self::isArrayUnValidated($array, $keys)) { |
||
154 | return false; |
||
155 | } |
||
156 | foreach ($keys as $key) { |
||
157 | $subKeyArray = $array; |
||
158 | if (static::exists($array, $key)) { |
||
159 | continue; |
||
160 | } |
||
161 | foreach (explode('.', $key) as $segment) { |
||
162 | if (static::exists($subKeyArray, $segment)) { |
||
163 | $subKeyArray = $subKeyArray[$segment]; |
||
164 | } else { |
||
165 | return false; |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | return true; |
||
170 | } |
||
171 | |||
172 | protected static function isArrayUnValidated(array $array, $keys) |
||
173 | { |
||
174 | if (is_null($keys)) { |
||
175 | return true; |
||
176 | } |
||
177 | $keys = (array) $keys; |
||
178 | if (empty($array)) { |
||
179 | return true; |
||
180 | } |
||
181 | if ($keys === []) { |
||
182 | return true; |
||
183 | } |
||
184 | return false; |
||
185 | } |
||
186 | |||
187 | public static function isAssoc(array $array) |
||
188 | { |
||
189 | $keys = array_keys($array); |
||
190 | return array_keys($keys) !== $keys; |
||
191 | } |
||
192 | |||
193 | public static function only(array $array, $keys) |
||
194 | { |
||
195 | return array_intersect_key($array, array_flip((array) $keys)); |
||
196 | } |
||
197 | |||
198 | public static function prepend(array $array, $value, $key = null) |
||
206 | } |
||
207 | |||
208 | public static function pull(array &$array, $key, $default = null) |
||
209 | { |
||
210 | $value = static::get($array, $key, $default); |
||
211 | static::forget($array, $key); |
||
212 | return $value; |
||
213 | } |
||
214 | |||
215 | public static function random(array $array, int $amount = null) |
||
216 | { |
||
217 | if (is_null($amount)) { |
||
218 | return $array[array_rand($array)]; |
||
219 | } |
||
220 | $keys = array_rand($array, $amount); |
||
221 | $results = []; |
||
222 | foreach ((array) $keys as $key) { |
||
223 | $results[] = $array[$key]; |
||
224 | } |
||
225 | return $results; |
||
226 | } |
||
227 | |||
228 | public static function set(array &$array, string $key, $value) |
||
243 | } |
||
244 | |||
245 | |||
246 | public static function where(array $array, callable $callback) |
||
249 | } |
||
250 | |||
251 | public static function wrap($value) |
||
252 | { |
||
254 | } |
||
255 | } |
||
256 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.