Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
9 | class Arr |
||
10 | { |
||
11 | /** |
||
12 | * @param mixed $array |
||
13 | * @param mixed $key |
||
14 | * @param mixed $default |
||
15 | * @param mixed $strict_default_check |
||
16 | * |
||
17 | * @return mixed |
||
18 | */ |
||
19 | 109 | public static function get($array, $key, $default = null, $strict_default_check = false) |
|
30 | |||
31 | /** |
||
32 | * Вырезает ключ из массива |
||
33 | * @static |
||
34 | * |
||
35 | * @param $array |
||
36 | * @param $key |
||
37 | * @param null $default |
||
38 | * @param bool $strict_default_check |
||
39 | * |
||
40 | * @return null |
||
41 | */ |
||
42 | 18 | public static function cut(&$array, $key, $default = null, $strict_default_check = false) |
|
58 | |||
59 | /** |
||
60 | * Retrieves multiple keys from an array. If the key does not exist in the |
||
61 | * array, the default value will be added instead. |
||
62 | * Get the values "username", "password" from $_POST |
||
63 | * $auth = Arr::extract($_POST, array('username', 'password')); |
||
64 | * $param = Arr::extract($_POST, $param); |
||
65 | * |
||
66 | * @param array array to extract keys from |
||
67 | * @param mixed key or list of key names |
||
68 | * @param mixed default value |
||
69 | * |
||
70 | * @return mixed |
||
71 | */ |
||
72 | 2 | public static function extract($array, $keys, $default = null) |
|
90 | |||
91 | /** |
||
92 | * Возвращает первый элемент массива |
||
93 | * |
||
94 | * @param mixed $array |
||
95 | * |
||
96 | * @return mixed $first_value |
||
97 | */ |
||
98 | 34 | public static function reset(array $array) |
|
104 | |||
105 | /** |
||
106 | * Возвращает последний элемент массива |
||
107 | * |
||
108 | * @param mixed $array |
||
109 | * |
||
110 | * @return mixed $last_value |
||
111 | */ |
||
112 | 13 | public static function end(array $array) |
|
118 | |||
119 | /** |
||
120 | * Проверка массива на наличие ключей |
||
121 | * |
||
122 | * @param array $search |
||
123 | * @param mixed $keys |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | 1 | public static function keysExists(array $search, $keys) |
|
148 | |||
149 | /** |
||
150 | * Преобразует объект данных в ассоциативный массив |
||
151 | * |
||
152 | * @param $obj |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | 1 | public static function fromObj($obj) |
|
176 | |||
177 | /** |
||
178 | * Проверим, пересекаются ли два массива |
||
179 | * |
||
180 | * @param mixed $arr1 |
||
181 | * @param mixed $arr2 |
||
182 | * |
||
183 | * @return mixed |
||
184 | */ |
||
185 | 1 | public static function isIntersec(array $arr1 = array(), array $arr2 = array()) |
|
191 | |||
192 | /** |
||
193 | * Выполняет trim над всеми элементами массива |
||
194 | * |
||
195 | * @param array $array |
||
196 | * @param string $charlist |
||
197 | * |
||
198 | * @return array $array |
||
199 | */ |
||
200 | 8 | public static function trim($array, $charlist = '') |
|
230 | |||
231 | /** |
||
232 | * @static |
||
233 | * |
||
234 | * @param $glue |
||
235 | * @param array $array |
||
236 | * |
||
237 | * @return mixed |
||
238 | */ |
||
239 | 1 | public static function implode(array $array, $glue) |
|
251 | |||
252 | 16 | public static function reflect($array) |
|
256 | |||
257 | /** |
||
258 | * @param mixed $data |
||
259 | * |
||
260 | * @return mixed |
||
261 | */ |
||
262 | 19 | public static function reduce($data) |
|
273 | |||
274 | /** |
||
275 | * Объединение ассоциативных массивов. |
||
276 | * В отличие от CMap::mergeArray перекрывает целочисленные ключи, а не увеличивает индекс элементов |
||
277 | * |
||
278 | * @param array $a |
||
279 | * @param array $b |
||
280 | * |
||
281 | * @return array |
||
282 | */ |
||
283 | 2 | public static function mergeAssoc(array $a, array $b) |
|
302 | |||
303 | /** |
||
304 | * Делит массив на $countOfParts колонок |
||
305 | * @param array $array |
||
306 | * @param int $countOfParts |
||
307 | * @param bool $resetIndex - не использовать ассоциативные ключи, по умолчанию true |
||
308 | * @param bool $flipSort - сортировка элементов по горизонтали, по умолчанию false |
||
309 | * |
||
310 | * @return array |
||
311 | */ |
||
312 | 7 | public static function divide(array $array, $countOfParts = 2, $resetIndex = true, $flipSort = false) |
|
322 | |||
323 | /** |
||
324 | * @param $array |
||
325 | * @param $itemKey |
||
326 | * @param $item |
||
327 | */ |
||
328 | 1 | public static function push(&$array, $itemKey, $item) |
|
337 | |||
338 | /** |
||
339 | * @param $array |
||
340 | * @param $after |
||
341 | * @param $item |
||
342 | * @param $itemKey |
||
343 | */ |
||
344 | 1 | public static function insertAfter(&$array, $itemKey, $item, $after) |
|
362 | |||
363 | /** |
||
364 | * @param $array - массив элементы которого нужно отфильтровать |
||
365 | * @param string|array $keys - ключ или массив ключей элементов $array которые нужно сравнить с $value |
||
366 | * @param $value |
||
367 | * @param $condition - OR или AND условие сравнения нескольких ключей |
||
368 | * |
||
369 | * @return array |
||
370 | */ |
||
371 | 1 | public static function filter($array, $keys, $value, $condition = 'OR') |
|
395 | |||
396 | /** |
||
397 | * @param array $array |
||
398 | * |
||
399 | * @return array |
||
400 | */ |
||
401 | public static function flatten(array $array) |
||
411 | |||
412 | /** |
||
413 | * Следующий после $current элемент массива |
||
414 | * |
||
415 | * @param $array |
||
416 | * @param $current |
||
417 | * @param bool $cycle - по кругу |
||
418 | * |
||
419 | * @return mixed|null |
||
420 | */ |
||
421 | public static function next($array, $current, $cycle = false) |
||
441 | |||
442 | /** |
||
443 | * Предыдущий перед $current элемент массива |
||
444 | * @param $array |
||
445 | * @param $current |
||
446 | * @param bool $cycle - по кругу |
||
447 | * |
||
448 | * @return mixed|null |
||
449 | */ |
||
450 | public static function prev($array, $current, $cycle = false) |
||
456 | |||
457 | 6 | private static function createMatrix($countColumns, $array) |
|
474 | |||
475 | 6 | private static function createArrayByMatrix($array, $matrix, $resetKeys = true, $flipSort = false) |
|
518 | |||
519 | /** |
||
520 | * Возвращает случайный элемент из массива $elements |
||
521 | * @param array $elements |
||
522 | * |
||
523 | * @return mixed |
||
524 | */ |
||
525 | public static function random(array $elements) |
||
530 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.