Total Complexity | 73 |
Total Lines | 362 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Arrays 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 Arrays, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Arrays |
||
13 | { |
||
14 | /** |
||
15 | * Get array value by key |
||
16 | * |
||
17 | * @param array $data |
||
18 | * @param $key |
||
19 | * @param $default |
||
20 | * |
||
21 | * @return mixed|null |
||
22 | */ |
||
23 | public static function get(array $data, $key, $default = null) |
||
24 | { |
||
25 | if (!array_key_exists($key, $data)) return $default; |
||
26 | |||
27 | return $data[$key]; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Pull array key |
||
32 | * |
||
33 | * @param array $data |
||
34 | * @param $key |
||
35 | * @param $default |
||
36 | * |
||
37 | * @return mixed|null |
||
38 | */ |
||
39 | public static function pullKey(array &$data, $key, $default = null) |
||
40 | { |
||
41 | $result = self::get($data, $key, $default); |
||
42 | unset($data[$key]); |
||
43 | |||
44 | return $result; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Object to array |
||
49 | * |
||
50 | * @param $data |
||
51 | * |
||
52 | * @return array |
||
53 | */ |
||
54 | public static function objectToArray($data): array |
||
55 | { |
||
56 | return json_decode(json_encode($data), true); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Array to html |
||
61 | * |
||
62 | * @param array $data |
||
63 | * |
||
64 | * @return string |
||
65 | */ |
||
66 | public static function toTags(array $data): string |
||
67 | { |
||
68 | $result = []; |
||
69 | |||
70 | foreach ($data as $key => $item) { |
||
71 | if (is_int($key)) { |
||
72 | $tag = $item; |
||
73 | } else { |
||
74 | $tag = $key; |
||
75 | |||
76 | if (is_array($item)) { |
||
77 | $options = ''; |
||
78 | $content = ''; |
||
79 | |||
80 | foreach ($item as $key2 => $item2) { |
||
81 | if (is_int($key2) && is_array($item2)) { |
||
82 | foreach ($item2 as $name => $value) { |
||
83 | $options .= ' ' . $name .'="' . $value . '"'; |
||
84 | } |
||
85 | } elseif (is_int($key2)) { |
||
86 | $content = $item2; |
||
87 | } else { |
||
88 | $options .= ' ' . $key2 .'="' . $item2 . '"'; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | } else { |
||
93 | $content = $item; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | $result[] = '<' . $tag . (empty($options) ? '' : $options) . (empty($content) ? ' />' : '>' . $content . '</' . $tag . '>'); |
||
98 | unset($options, $content, $tag); |
||
99 | } |
||
100 | |||
101 | return implode(PHP_EOL, $result); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Building Maps |
||
106 | * |
||
107 | * @param $data |
||
108 | * @param $keyGroup |
||
109 | * @param $keyValue |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | public static function map(array $data, $keyGroup, $keyValue = null): array |
||
114 | { |
||
115 | $result = []; |
||
116 | |||
117 | foreach ($data as $item) { |
||
118 | if (array_key_exists($keyGroup, $item)) { |
||
119 | $result[$item[$keyGroup]] = is_null($keyValue) ? $item : (array_key_exists($keyValue, $item) ? $item[$keyValue] : null); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | unset($item); |
||
124 | |||
125 | return $result; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Group array |
||
130 | * |
||
131 | * @param array $data |
||
132 | * @param $keyGroup |
||
133 | * @param $keyValue |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | public static function group(array $data, $keyGroup, $keyValue = null): array |
||
138 | { |
||
139 | $result = []; |
||
140 | |||
141 | foreach ($data as $item) { |
||
142 | if (array_key_exists($keyGroup, $item)) { |
||
143 | $key = $item[$keyGroup]; |
||
144 | |||
145 | if (empty($result[$key])) $result[$key] = []; |
||
146 | |||
147 | $result[$key][] = is_null($keyValue) ? $item : (array_key_exists($keyValue, $item) ? $item[$keyValue] : null); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | unset($item, $key); |
||
152 | |||
153 | return $result; |
||
154 | } |
||
155 | /** |
||
156 | * Index group array |
||
157 | * |
||
158 | * @param array $data |
||
159 | * @param $keyGroup |
||
160 | * @param $keyValue |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | public static function index(array $data, $keyGroup, $keyValue = null): array |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Append not empty array data |
||
181 | * |
||
182 | * @param array $first |
||
183 | * @param array $second |
||
184 | * |
||
185 | * @return array |
||
186 | */ |
||
187 | public static function appendNotEmptyData(array $first, array $second): array |
||
188 | { |
||
189 | $result = null; |
||
190 | |||
191 | switch (true) { |
||
192 | case empty($first) && empty($second): |
||
193 | $result = []; |
||
194 | break; |
||
195 | case empty($second): |
||
196 | $result = $first; |
||
197 | break; |
||
198 | case empty($first): |
||
199 | $result = self::removeEmpty($second); |
||
200 | break; |
||
201 | default: |
||
202 | $result = self::mergeNotEmptyData($first, $second); |
||
203 | break; |
||
204 | } |
||
205 | |||
206 | return $result; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Replace empty array data to not empty array data |
||
211 | * |
||
212 | * @param array $first |
||
213 | * @param array $second |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | public static function replaceEmptyNotEmptyData(array $first, array $second): array |
||
218 | { |
||
219 | if (empty($first)) { |
||
220 | return []; |
||
221 | } elseif (empty($second)) { |
||
222 | return $first; |
||
223 | } |
||
224 | |||
225 | foreach ($first as $key => &$value) { |
||
226 | if (empty($value) && array_key_exists($key, $second)) $value = $second[$key]; |
||
227 | } |
||
228 | |||
229 | unset($value, $key, $second); |
||
230 | |||
231 | return $first; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Replace not empty array data |
||
236 | * |
||
237 | * @param array $first |
||
238 | * @param array $second |
||
239 | * |
||
240 | * @return array |
||
241 | */ |
||
242 | public static function replaceNotEmptyData(array $first, array $second): array |
||
243 | { |
||
244 | if (empty($first)) { |
||
245 | return []; |
||
246 | } elseif (empty($second)) { |
||
247 | return $first; |
||
248 | } |
||
249 | |||
250 | foreach ($first as $key => &$value) { |
||
251 | if (!empty($second[$key])) $value = $second[$key]; |
||
252 | } |
||
253 | |||
254 | unset($value, $key, $second); |
||
255 | |||
256 | return $first; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Merge not empty array data |
||
261 | * |
||
262 | * @param array $first |
||
263 | * @param array $second |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | public static function mergeNotEmptyData(array $first, array $second): array |
||
268 | { |
||
269 | if (empty($first) && empty($second)) { |
||
270 | return []; |
||
271 | } elseif (empty($second)) { |
||
272 | return $first; |
||
273 | } |
||
274 | |||
275 | foreach ($second as $key => $value) { |
||
276 | if (!empty($value)) $first[$key] = $value; |
||
277 | } |
||
278 | |||
279 | unset($value, $key); |
||
280 | |||
281 | return $first; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Convert empty to null |
||
286 | * |
||
287 | * @param array $data |
||
288 | * @param bool $recursive |
||
289 | * |
||
290 | * @return array |
||
291 | */ |
||
292 | public static function emptyToNull(array $data, bool $recursive = false): array |
||
293 | { |
||
294 | foreach ($data as $key => &$value) { |
||
295 | if (empty($value)) { |
||
296 | $value = null; |
||
297 | } elseif ($recursive && is_array($value)) { |
||
298 | $value = self::emptyToNull($value, $recursive); |
||
299 | } |
||
300 | } |
||
301 | |||
302 | unset($value, $key); |
||
303 | |||
304 | return $data; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Remove empty data to array |
||
309 | * |
||
310 | * @param array $data |
||
311 | * @param bool $recursive |
||
312 | * |
||
313 | * @return array |
||
314 | */ |
||
315 | public static function removeEmpty(array $data, bool $recursive = false): array |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Remove null data to array |
||
332 | * |
||
333 | * @param array $data |
||
334 | * @param bool $recursive |
||
335 | * |
||
336 | * @return array |
||
337 | */ |
||
338 | public static function removeNull(array $data, bool $recursive = false): array |
||
339 | { |
||
340 | foreach ($data as $key => &$value) { |
||
341 | if (is_null($value)) { |
||
342 | unset($data[$key]); |
||
343 | } elseif ($recursive && is_array($value)) { |
||
344 | $value = self::removeNull($value, $recursive); |
||
345 | } |
||
346 | } |
||
347 | |||
348 | unset($value, $key); |
||
349 | |||
350 | return $data; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Trim value to array |
||
355 | * |
||
356 | * @param array $data |
||
357 | * @param bool $recursive |
||
358 | * |
||
359 | * @return array |
||
360 | */ |
||
361 | public static function trim(array $data, bool $recursive = true): array |
||
374 | } |
||
375 | } |
||
376 |