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 | 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 | public static function cut(&$array, $key, $default = null, $strict_default_check = false) |
||
| 43 | {
|
||
| 44 | if( $strict_default_check ) |
||
| 45 | {
|
||
| 46 | $value = !empty($array[$key]) ? $array[$key] : $default; |
||
| 47 | } |
||
| 48 | else |
||
| 49 | {
|
||
| 50 | $value = isset($array[$key]) ? $array[$key] : $default; |
||
| 51 | } |
||
| 52 | |||
| 53 | if( isset($array[$key]) ) |
||
| 54 | unset($array[$key]); |
||
| 55 | |||
| 56 | return $value; |
||
| 57 | } |
||
| 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 | public static function extract($array, $keys, $default = null) |
||
| 73 | {
|
||
| 74 | $found = array(); |
||
| 75 | |||
| 76 | if( is_array($keys) ) |
||
| 77 | {
|
||
| 78 | foreach($keys as $key) |
||
| 79 | {
|
||
| 80 | $found[$key] = isset($array[$key]) ? $array[$key] : $default; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | else |
||
| 84 | {
|
||
| 85 | $found = isset($array[$keys]) ? $array[$keys] : $default; |
||
| 86 | } |
||
| 87 | |||
| 88 | return $found; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Возвращает первый элемент массива |
||
| 93 | * |
||
| 94 | * @param mixed $array |
||
| 95 | * |
||
| 96 | * @return mixed $first_value |
||
| 97 | */ |
||
| 98 | public static function reset(array $array) |
||
| 99 | {
|
||
| 100 | $item = reset($array); |
||
| 101 | |||
| 102 | return $item; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Возвращает последний элемент массива |
||
| 107 | * |
||
| 108 | * @param mixed $array |
||
| 109 | * |
||
| 110 | * @return mixed $last_value |
||
| 111 | */ |
||
| 112 | public static function end(array $array) |
||
| 113 | {
|
||
| 114 | $item = end($array); |
||
| 115 | |||
| 116 | return $item; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Проверка массива на наличие ключей |
||
| 121 | * |
||
| 122 | * @param array $search |
||
| 123 | * @param mixed $keys |
||
| 124 | * |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | public static function keysExists(array $search, $keys) |
||
| 128 | {
|
||
| 129 | $result = true; |
||
| 130 | |||
| 131 | if( is_array($keys) ) |
||
| 132 | {
|
||
| 133 | foreach($keys as $value) |
||
| 134 | {
|
||
| 135 | if( !array_key_exists($value, $search) ) |
||
| 136 | {
|
||
| 137 | return false; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | else |
||
| 142 | {
|
||
| 143 | $result = array_key_exists($keys, $search); |
||
| 144 | } |
||
| 145 | |||
| 146 | return $result; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Преобразует объект данных в ассоциативный массив |
||
| 151 | * |
||
| 152 | * @param $obj |
||
| 153 | * |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public static function fromObj($obj) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Проверим, пересекаются ли два массива |
||
| 179 | * |
||
| 180 | * @param mixed $arr1 |
||
| 181 | * @param mixed $arr2 |
||
| 182 | * |
||
| 183 | * @return mixed |
||
| 184 | */ |
||
| 185 | public static function isIntersec(array $arr1 = array(), array $arr2 = array()) |
||
| 186 | {
|
||
| 187 | $intersec = array_intersect($arr1, $arr2); |
||
| 188 | |||
| 189 | return !empty($intersec); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Выполняет trim над всеми элементами массива |
||
| 194 | * |
||
| 195 | * @param array $array |
||
| 196 | * @param string $charlist |
||
| 197 | * |
||
| 198 | * @return array $array |
||
| 199 | */ |
||
| 200 | public static function trim($array, $charlist = '') |
||
| 201 | {
|
||
| 202 | if( is_array($array) ) |
||
| 203 | {
|
||
| 204 | foreach($array as $key => $value) |
||
| 205 | {
|
||
| 206 | if( !empty($charlist) ) |
||
| 207 | {
|
||
| 208 | $array[$key] = self::trim($value, $charlist); |
||
| 209 | } |
||
| 210 | else |
||
| 211 | {
|
||
| 212 | $array[$key] = self::trim($value); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } |
||
| 216 | elseif( is_string($array) ) |
||
| 217 | {
|
||
| 218 | if( !empty($charlist) ) |
||
| 219 | {
|
||
| 220 | $array = trim($array, $charlist); |
||
| 221 | } |
||
| 222 | else |
||
| 223 | {
|
||
| 224 | $array = trim($array); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | return $array; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @static |
||
| 233 | * |
||
| 234 | * @param $glue |
||
| 235 | * @param array $array |
||
| 236 | * |
||
| 237 | * @return mixed |
||
| 238 | */ |
||
| 239 | public static function implode(array $array, $glue) |
||
| 240 | {
|
||
| 241 | foreach($array as $key => $value) |
||
| 242 | {
|
||
| 243 | if( empty($value) ) |
||
| 244 | {
|
||
| 245 | unset($array[$key]); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | return preg_replace("/\s+/", " ", implode($glue, $array));
|
||
| 250 | } |
||
| 251 | |||
| 252 | public static function reflect($array) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param mixed $data |
||
| 259 | * |
||
| 260 | * @return mixed |
||
| 261 | */ |
||
| 262 | public static function reduce($data) |
||
| 263 | {
|
||
| 264 | if( is_array($data) ) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Объединение ассоциативных массивов. |
||
| 276 | * В отличие от CMap::mergeArray перекрывает целочисленные ключи, а не увеличивает индекс элементов |
||
| 277 | * |
||
| 278 | * @param array $a |
||
| 279 | * @param array $b |
||
| 280 | * |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | 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 | 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 | public static function push(&$array, $itemKey, $item) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param $array |
||
| 340 | * @param $after |
||
| 341 | * @param $item |
||
| 342 | * @param $itemKey |
||
| 343 | */ |
||
| 344 | 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 | 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 | private static function createMatrix($countColumns, $array) |
||
| 474 | |||
| 475 | private static function createArrayByMatrix($array, $matrix, $resetKeys = true, $flipSort = false) |
||
| 518 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.