| Total Complexity | 79 |
| Total Lines | 407 |
| 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 |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * To string key and value |
||
| 61 | * |
||
| 62 | * @param array $data |
||
| 63 | * @param string $preFix |
||
| 64 | * @param string $postFix |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | public static function toString(array $data, string $preFix = ':', $postFix = ';'): string |
||
| 69 | { |
||
| 70 | $string = ''; |
||
| 71 | |||
| 72 | foreach ($data as $key => $option) { |
||
| 73 | $string .= $key . $preFix . $option . $postFix; |
||
| 74 | } |
||
| 75 | |||
| 76 | return $string; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Array to html |
||
| 81 | * |
||
| 82 | * @param array $data |
||
| 83 | * |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public static function toTags(array $data): string |
||
| 87 | { |
||
| 88 | $string = ''; |
||
| 89 | |||
| 90 | foreach ($data as $key => $item) { |
||
| 91 | $info = []; |
||
| 92 | if (is_int($key)) { |
||
| 93 | $info['tag'] = $item; |
||
| 94 | } elseif (is_array($item)) { |
||
| 95 | $info = self::replaceTagInfo($item); |
||
| 96 | $info['tag'] = $key; |
||
| 97 | } else { |
||
| 98 | $info['content'] = $item; |
||
| 99 | $info['tag'] = $key; |
||
| 100 | } |
||
| 101 | |||
| 102 | $string .= '<' . $info['tag'] . (empty($info['attr']) ? '' : $info['attr']) . (empty($info['content']) ? ' />' : '>' . $info['content'] . '</' . $info['tag'] . '>'); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $string; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Building Maps |
||
| 110 | * |
||
| 111 | * @param $data |
||
| 112 | * @param $keyGroup |
||
| 113 | * @param $keyValue |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | public static function map(array $data, $keyGroup, $keyValue = null): array |
||
| 118 | { |
||
| 119 | $result = []; |
||
| 120 | |||
| 121 | foreach ($data as $item) { |
||
| 122 | if (array_key_exists($keyGroup, $item)) { |
||
| 123 | $result[$item[$keyGroup]] = is_null($keyValue) ? $item : (array_key_exists($keyValue, $item) ? $item[$keyValue] : null); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | unset($item); |
||
| 128 | |||
| 129 | return $result; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Group array |
||
| 134 | * |
||
| 135 | * @param array $data |
||
| 136 | * @param $keyGroup |
||
| 137 | * @param $keyValue |
||
| 138 | * |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | public static function group(array $data, $keyGroup, $keyValue = null): array |
||
| 142 | { |
||
| 143 | $result = []; |
||
| 144 | |||
| 145 | foreach ($data as $item) { |
||
| 146 | if (array_key_exists($keyGroup, $item)) { |
||
| 147 | $key = $item[$keyGroup]; |
||
| 148 | |||
| 149 | if (empty($result[$key])) $result[$key] = []; |
||
| 150 | |||
| 151 | $result[$key][] = is_null($keyValue) ? $item : (array_key_exists($keyValue, $item) ? $item[$keyValue] : null); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | unset($item, $key); |
||
| 156 | |||
| 157 | return $result; |
||
| 158 | } |
||
| 159 | /** |
||
| 160 | * Index group array |
||
| 161 | * |
||
| 162 | * @param array $data |
||
| 163 | * @param $keyGroup |
||
| 164 | * @param $keyValue |
||
| 165 | * |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | public static function index(array $data, $keyGroup, $keyValue = null): array |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Append not empty array data |
||
| 185 | * |
||
| 186 | * @param array $first |
||
| 187 | * @param array $second |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | public static function appendNotEmptyData(array $first, array $second): array |
||
| 192 | { |
||
| 193 | $result = null; |
||
| 194 | |||
| 195 | switch (true) { |
||
| 196 | case empty($first) && empty($second): |
||
| 197 | $result = []; |
||
| 198 | break; |
||
| 199 | case empty($second): |
||
| 200 | $result = $first; |
||
| 201 | break; |
||
| 202 | case empty($first): |
||
| 203 | $result = self::removeEmpty($second); |
||
| 204 | break; |
||
| 205 | default: |
||
| 206 | $result = self::appendNotEmpty($first, $second); |
||
| 207 | break; |
||
| 208 | } |
||
| 209 | |||
| 210 | return $result; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Replace empty array data to not empty array data |
||
| 215 | * |
||
| 216 | * @param array $first |
||
| 217 | * @param array $second |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public static function replaceEmptyNotEmptyData(array $first, array $second): array |
||
| 222 | { |
||
| 223 | if (empty($first)) { |
||
| 224 | return []; |
||
| 225 | } elseif (empty($second)) { |
||
| 226 | return $first; |
||
| 227 | } |
||
| 228 | |||
| 229 | foreach ($first as $key => &$value) { |
||
| 230 | if (empty($value) && array_key_exists($key, $second)) $value = $second[$key]; |
||
| 231 | } |
||
| 232 | |||
| 233 | unset($value, $key, $second); |
||
| 234 | |||
| 235 | return $first; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Replace not empty array data |
||
| 240 | * |
||
| 241 | * @param array $first |
||
| 242 | * @param array $second |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | public static function replaceNotEmptyData(array $first, array $second): array |
||
| 247 | { |
||
| 248 | if (empty($first)) { |
||
| 249 | return []; |
||
| 250 | } elseif (empty($second)) { |
||
| 251 | return $first; |
||
| 252 | } |
||
| 253 | |||
| 254 | foreach ($first as $key => &$value) { |
||
| 255 | if (!empty($second[$key])) $value = $second[$key]; |
||
| 256 | } |
||
| 257 | |||
| 258 | unset($value, $key, $second); |
||
| 259 | |||
| 260 | return $first; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Merge not empty array data |
||
| 265 | * |
||
| 266 | * @param array $first |
||
| 267 | * @param array $second |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | public static function mergeNotEmptyData(array $first, array $second): array |
||
| 272 | { |
||
| 273 | if (empty($first) && empty($second)) { |
||
| 274 | return []; |
||
| 275 | } elseif (empty($second)) { |
||
| 276 | return $first; |
||
| 277 | } |
||
| 278 | |||
| 279 | foreach ($second as $key => $value) { |
||
| 280 | if (!empty($value)) $first[$key] = $value; |
||
| 281 | } |
||
| 282 | |||
| 283 | unset($value, $key); |
||
| 284 | |||
| 285 | return $first; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Convert empty to null |
||
| 290 | * |
||
| 291 | * @param array $data |
||
| 292 | * @param bool $recursive |
||
| 293 | * |
||
| 294 | * @return array |
||
| 295 | */ |
||
| 296 | public static function emptyToNull(array $data, bool $recursive = false): array |
||
| 297 | { |
||
| 298 | foreach ($data as $key => &$value) { |
||
| 299 | if (empty($value)) { |
||
| 300 | $value = null; |
||
| 301 | } elseif ($recursive && is_array($value)) { |
||
| 302 | $value = self::emptyToNull($value, $recursive); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | unset($value, $key); |
||
| 307 | |||
| 308 | return $data; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Remove empty data to array |
||
| 313 | * |
||
| 314 | * @param array $data |
||
| 315 | * @param bool $recursive |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | public static function removeEmpty(array $data, bool $recursive = false): array |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Remove null data to array |
||
| 336 | * |
||
| 337 | * @param array $data |
||
| 338 | * @param bool $recursive |
||
| 339 | * |
||
| 340 | * @return array |
||
| 341 | */ |
||
| 342 | public static function removeNull(array $data, bool $recursive = false): array |
||
| 343 | { |
||
| 344 | foreach ($data as $key => &$value) { |
||
| 345 | if (is_null($value)) { |
||
| 346 | unset($data[$key]); |
||
| 347 | } elseif ($recursive && is_array($value)) { |
||
| 348 | $value = self::removeNull($value, $recursive); |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | unset($value, $key); |
||
| 353 | |||
| 354 | return $data; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Trim value to array |
||
| 359 | * |
||
| 360 | * @param array $data |
||
| 361 | * @param bool $recursive |
||
| 362 | * |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | public static function trim(array $data, bool $recursive = true): array |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Append not empty |
||
| 382 | * |
||
| 383 | * @param array $first |
||
| 384 | * @param array $second |
||
| 385 | * |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | protected static function appendNotEmpty(array $first, array $second): array |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Replace tag info |
||
| 399 | * |
||
| 400 | * @param array $info |
||
| 401 | * |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | protected static function replaceTagInfo(array $info): array |
||
| 419 | } |
||
| 420 | } |
||
| 421 |