| Total Complexity | 74 |
| Total Lines | 376 |
| 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 | return array_key_exists($key, $data) ? $data[$key] : $default; |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Pull array key |
||
| 30 | * |
||
| 31 | * @param array $data |
||
| 32 | * @param $key |
||
| 33 | * @param $default |
||
| 34 | * |
||
| 35 | * @return mixed|null |
||
| 36 | */ |
||
| 37 | public static function pullKey(array &$data, $key, $default = null) |
||
| 38 | { |
||
| 39 | $result = self::get($data, $key, $default); |
||
| 40 | unset($data[$key]); |
||
| 41 | |||
| 42 | return $result; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Object to array |
||
| 47 | * |
||
| 48 | * @param $data |
||
| 49 | * |
||
| 50 | * @return array |
||
| 51 | */ |
||
| 52 | public static function objectToArray($data): array |
||
| 53 | { |
||
| 54 | return json_decode(json_encode($data), true); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * To string key and value |
||
| 59 | * |
||
| 60 | * @param array $data |
||
| 61 | * @param string $preFix |
||
| 62 | * @param string $postFix |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public static function toString(array $data, string $preFix = ':', $postFix = ';'): string |
||
| 67 | { |
||
| 68 | $string = ''; |
||
| 69 | |||
| 70 | foreach ($data as $key => $option) { |
||
| 71 | $string .= $key . $preFix . $option . $postFix; |
||
| 72 | } |
||
| 73 | |||
| 74 | return $string; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Array to html |
||
| 79 | * |
||
| 80 | * @param array $data |
||
| 81 | * |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | public static function toTags(array $data): string |
||
| 85 | { |
||
| 86 | $string = ''; |
||
| 87 | |||
| 88 | foreach ($data as $key => $item) { |
||
| 89 | $info = []; |
||
| 90 | if (is_int($key)) { |
||
| 91 | $info['tag'] = $item; |
||
| 92 | } elseif (is_array($item)) { |
||
| 93 | $info = self::replaceTagInfo($item); |
||
| 94 | $info['tag'] = $key; |
||
| 95 | } else { |
||
| 96 | $info['content'] = $item; |
||
| 97 | $info['tag'] = $key; |
||
| 98 | } |
||
| 99 | |||
| 100 | $string .= '<' . $info['tag'] . (empty($info['attr']) ? '' : $info['attr']) . (empty($info['content']) ? ' />' : '>' . $info['content'] . '</' . $info['tag'] . '>'); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $string; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Building Maps |
||
| 108 | * |
||
| 109 | * @param $data |
||
| 110 | * @param $keyGroup |
||
| 111 | * @param $keyValue |
||
| 112 | * |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public static function map(array $data, $keyGroup, $keyValue = null): array |
||
| 116 | { |
||
| 117 | $result = []; |
||
| 118 | |||
| 119 | foreach ($data as $item) { |
||
| 120 | if (array_key_exists($keyGroup, $item)) { |
||
| 121 | $result[$item[$keyGroup]] = self::getValueCheckParam($item, $keyValue); |
||
| 122 | } |
||
| 123 | } |
||
| 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][] = self::getValueCheckParam($item, $keyValue); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | return $result; |
||
| 152 | } |
||
| 153 | /** |
||
| 154 | * Index group array |
||
| 155 | * |
||
| 156 | * @param array $data |
||
| 157 | * @param $keyGroup |
||
| 158 | * @param $keyValue |
||
| 159 | * |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | public static function index(array $data, $keyGroup, $keyValue = null): array |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Append not empty array data |
||
| 177 | * |
||
| 178 | * @param array $first |
||
| 179 | * @param array $second |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | public static function appendNotEmptyData(array $first, array $second): array |
||
| 184 | { |
||
| 185 | switch (true) { |
||
| 186 | case empty($first) && empty($second): |
||
| 187 | $result = []; |
||
| 188 | break; |
||
| 189 | case empty($second): |
||
| 190 | $result = $first; |
||
| 191 | break; |
||
| 192 | case empty($first): |
||
| 193 | $result = self::removeEmpty($second); |
||
| 194 | break; |
||
| 195 | default: |
||
| 196 | $result = self::appendNotEmpty($first, $second); |
||
| 197 | break; |
||
| 198 | } |
||
| 199 | |||
| 200 | return $result; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Replace empty array data to not empty array data |
||
| 205 | * |
||
| 206 | * @param array $first |
||
| 207 | * @param array $second |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public static function replaceEmptyNotEmptyData(array $first = [], array $second = []): array |
||
| 212 | { |
||
| 213 | if (!empty($first) && !empty($second)) { |
||
| 214 | foreach ($first as $key => &$value) { |
||
| 215 | if (empty($value) && array_key_exists($key, $second)) $value = $second[$key]; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | return $first; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Replace not empty array data |
||
| 223 | * |
||
| 224 | * @param array $first |
||
| 225 | * @param array $second |
||
| 226 | * |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | public static function replaceNotEmptyData(array $first = [], array $second = []): array |
||
| 230 | { |
||
| 231 | if (!empty($first) && !empty($second)) { |
||
| 232 | foreach ($first as $key => &$value) { |
||
| 233 | if (!empty($second[$key])) $value = $second[$key]; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | return $first; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Merge not empty array data |
||
| 241 | * |
||
| 242 | * @param array $first |
||
| 243 | * @param array $second |
||
| 244 | * |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | public static function mergeNotEmptyData(array $first = [], array $second = []): array |
||
| 248 | { |
||
| 249 | if (!empty($second)) { |
||
| 250 | foreach ($second as $key => $value) { |
||
| 251 | if (!empty($value)) $first[$key] = $value; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | return $first; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Replace empty to null |
||
| 259 | * |
||
| 260 | * @param array $data |
||
| 261 | * @param bool $recursive |
||
| 262 | * |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | public static function emptyToNull(array $data, bool $recursive = false): array |
||
| 266 | { |
||
| 267 | foreach ($data as $key => &$value) { |
||
| 268 | if (empty($value)) { |
||
| 269 | $value = null; |
||
| 270 | } elseif ($recursive && is_array($value)) { |
||
| 271 | $value = self::emptyToNull($value, $recursive); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | return $data; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Remove empty data to array |
||
| 279 | * |
||
| 280 | * @param array $data |
||
| 281 | * @param bool $recursive |
||
| 282 | * |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public static function removeEmpty(array $data, bool $recursive = false): array |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Remove null data to array |
||
| 299 | * |
||
| 300 | * @param array $data |
||
| 301 | * @param bool $recursive |
||
| 302 | * |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | public static function removeNull(array $data, bool $recursive = false): array |
||
| 306 | { |
||
| 307 | foreach ($data as $key => &$value) { |
||
| 308 | if (is_null($value)) { |
||
| 309 | unset($data[$key]); |
||
| 310 | } elseif ($recursive && is_array($value)) { |
||
| 311 | $value = self::removeNull($value, $recursive); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | return $data; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Trim value to array |
||
| 319 | * |
||
| 320 | * @param array $data |
||
| 321 | * @param bool $recursive |
||
| 322 | * |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | public static function trim(array $data, bool $recursive = true): array |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Append not empty |
||
| 339 | * |
||
| 340 | * @param array $first |
||
| 341 | * @param array $second |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | protected static function appendNotEmpty(array $first, array $second): array |
||
| 346 | { |
||
| 347 | foreach ($second as $key => $value) { |
||
| 348 | if (!empty($value) && !array_key_exists($key, $first)) $first[$key] = $value; |
||
| 349 | } |
||
| 350 | return $first; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Replace tag info |
||
| 355 | * |
||
| 356 | * @param array $info |
||
| 357 | * |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | protected static function replaceTagInfo(array $info): array |
||
| 361 | { |
||
| 362 | $result = ['attr' => '', 'content' => '']; |
||
| 363 | |||
| 364 | foreach ($info as $key => $item) { |
||
| 365 | if (is_int($key) && is_array($item)) { |
||
| 366 | $result['attr'] .= ' ' . trim(self::toString($item, '="', '" ')); |
||
| 367 | } elseif (is_int($key)) { |
||
| 368 | $result['content'] = $item; |
||
| 369 | } else { |
||
| 370 | $result['attr'] .= ' ' . $key .'="' . $item . '"'; |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | return $result; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get value check param |
||
| 379 | * |
||
| 380 | * @param array $data |
||
| 381 | * @param $keyValue |
||
| 382 | * |
||
| 383 | * @return mixed |
||
| 384 | */ |
||
| 385 | protected static function getValueCheckParam(array $data, $keyValue) |
||
| 388 | } |
||
| 389 | } |
||
| 390 |