| Total Complexity | 40 |
| Total Lines | 341 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 0 |
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.
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 |
||
| 10 | class Arr |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Renaming array keys. |
||
| 14 | * As the second parameter, a callback function is passed, which determines the actions for processing the value. |
||
| 15 | * The output of the function must be a string with a name. |
||
| 16 | * |
||
| 17 | * @param array $array |
||
| 18 | * @param callable $callback |
||
| 19 | * |
||
| 20 | * @return array |
||
| 21 | */ |
||
| 22 | public function renameKeys(array $array, callable $callback): array |
||
| 23 | { |
||
| 24 | $result = []; |
||
| 25 | |||
| 26 | foreach ($array as $key => $value) { |
||
| 27 | $new = $callback($key, $value); |
||
| 28 | |||
| 29 | $result[$new] = $value; |
||
| 30 | } |
||
| 31 | |||
| 32 | return $result; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Renaming array keys with map. |
||
| 37 | * |
||
| 38 | * @param array $array |
||
| 39 | * @param array $map |
||
| 40 | * |
||
| 41 | * @return array |
||
| 42 | */ |
||
| 43 | public function renameKeysMap(array $array, array $map): array |
||
| 44 | { |
||
| 45 | return $this->renameKeys($array, static function ($key) use ($map) { |
||
| 46 | return $map[$key] ?? $key; |
||
| 47 | }); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the size of the longest text element of the array. |
||
| 52 | * |
||
| 53 | * @param array $array |
||
| 54 | * |
||
| 55 | * @return int |
||
| 56 | */ |
||
| 57 | public function longestStringLength(array $array): int |
||
| 58 | { |
||
| 59 | return ! empty($array) |
||
| 60 | ? max(array_map('mb_strlen', $array)) |
||
| 61 | : 0; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Push one a unique element onto the end of array. |
||
| 66 | * |
||
| 67 | * @param array $array |
||
| 68 | * @param array|mixed $values |
||
| 69 | * |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public function addUnique(array $array, $values): array |
||
| 73 | { |
||
| 74 | if ($this->isArrayable($values)) { |
||
|
|
|||
| 75 | foreach ($values as $value) { |
||
| 76 | $array = $this->addUnique($array, $value); |
||
| 77 | } |
||
| 78 | } else { |
||
| 79 | array_push($array, $values); |
||
| 80 | } |
||
| 81 | |||
| 82 | return array_values(array_unique($array)); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Sort an associative array in the order specified by an array of keys. |
||
| 87 | * |
||
| 88 | * Example: |
||
| 89 | * |
||
| 90 | * $arr = ['q' => 1, 'r' => 2, 's' => 5, 'w' => 123]; |
||
| 91 | * |
||
| 92 | * Arr::sortByKeys($arr, ['q', 'w', 'e']); |
||
| 93 | * |
||
| 94 | * print_r($arr); |
||
| 95 | * |
||
| 96 | * Array |
||
| 97 | * ( |
||
| 98 | * [q] => 1 |
||
| 99 | * [w] => 123 |
||
| 100 | * [r] => 2 |
||
| 101 | * [s] => 5 |
||
| 102 | * ) |
||
| 103 | * |
||
| 104 | * @see https://gist.github.com/Ellrion/a3145621f936aa9416f4c04987533d8d#file-helper-php |
||
| 105 | * |
||
| 106 | * @param array $array |
||
| 107 | * @param array $sorter |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | public function sortByKeys(array $array, array $sorter): array |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Merge one or more arrays recursively. |
||
| 121 | * Don't forget that numeric keys NOT will be renumbered! |
||
| 122 | * |
||
| 123 | * @param array[] ...$arrays |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function merge(...$arrays): array |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * If the given value is not an array and not null, wrap it in one. |
||
| 148 | * |
||
| 149 | * @param null $value |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function wrap($value = null): array |
||
| 154 | { |
||
| 155 | if (is_array($value)) { |
||
| 156 | return $value; |
||
| 157 | } |
||
| 158 | |||
| 159 | return ! empty($value) ? [$value] : []; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get the instance as an array. |
||
| 164 | * |
||
| 165 | * @param mixed $value |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | public function toArray($value = null): array |
||
| 170 | { |
||
| 171 | if (is_object($value)) { |
||
| 172 | $value = method_exists($value, 'toArray') ? $value->toArray() : get_object_vars($value); |
||
| 173 | } |
||
| 174 | |||
| 175 | $array = $this->wrap($value); |
||
| 176 | |||
| 177 | foreach ($array as &$item) { |
||
| 178 | $item = $this->isArrayable($item) ? $this->toArray($item) : $item; |
||
| 179 | } |
||
| 180 | |||
| 181 | return $array; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Determine if the given key exists in the provided array. |
||
| 186 | * |
||
| 187 | * @param array|\ArrayAccess $array |
||
| 188 | * @param mixed $key |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | public function exists($array, $key): bool |
||
| 193 | { |
||
| 194 | if ($array instanceof ArrayAccess) { |
||
| 195 | return $array->offsetExists($key); |
||
| 196 | } |
||
| 197 | |||
| 198 | return isset($array[$key]); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get an item from an array. |
||
| 203 | * |
||
| 204 | * @param array|ArrayAccess $array |
||
| 205 | * @param mixed $key |
||
| 206 | * @param mixed|null $default |
||
| 207 | * |
||
| 208 | * @return mixed|null |
||
| 209 | */ |
||
| 210 | public function get($array, $key, $default = null) |
||
| 211 | { |
||
| 212 | return $array[$key] ?? $default; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * If the element key exists, then return the name of the key, otherwise the default value. |
||
| 217 | * |
||
| 218 | * @param array|ArrayAccess $array |
||
| 219 | * @param mixed $key |
||
| 220 | * @param null $default |
||
| 221 | * |
||
| 222 | * @return mixed|null |
||
| 223 | */ |
||
| 224 | public function getKey($array, $key, $default = null) |
||
| 225 | { |
||
| 226 | return $this->exists($array, $key) ? $key : $default; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get all of the given array except for a specified array of keys. |
||
| 231 | * |
||
| 232 | * @param array|ArrayAccess $array |
||
| 233 | * @param $keys |
||
| 234 | * |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | public function except($array, $keys): array |
||
| 238 | { |
||
| 239 | $keys = (array) $keys; |
||
| 240 | |||
| 241 | return array_filter($array, static function ($key) use ($keys) { |
||
| 242 | return empty($keys) || ! in_array($key, $keys); |
||
| 243 | }, ARRAY_FILTER_USE_KEY); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get a subset of the items from the given array. |
||
| 248 | * |
||
| 249 | * @param array|ArrayAccess $array |
||
| 250 | * @param array|string $keys |
||
| 251 | * |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | public function only($array, $keys): array |
||
| 255 | { |
||
| 256 | return array_intersect_key($array, array_flip((array) $keys)); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Applies the callback to the elements of the given arrays. |
||
| 261 | * |
||
| 262 | * @param array|ArrayAccess $array |
||
| 263 | * @param callable $callback |
||
| 264 | * |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | public function map($array, callable $callback): array |
||
| 268 | { |
||
| 269 | foreach ($array as $key => &$value) { |
||
| 270 | $value = $callback($value, $key); |
||
| 271 | } |
||
| 272 | |||
| 273 | return $array; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Check if the item is an array. |
||
| 278 | * |
||
| 279 | * @param null $value |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function isArrayable($value = null): bool |
||
| 284 | { |
||
| 285 | return is_array($value) || is_object($value) || $value instanceof ArrayAccess; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Save array to php or json file. |
||
| 290 | * |
||
| 291 | * @param array|ArrayAccess $array |
||
| 292 | * @param string $path |
||
| 293 | * @param bool $is_json |
||
| 294 | * @param bool $sort_keys |
||
| 295 | */ |
||
| 296 | public function store($array, string $path, bool $is_json = false, bool $sort_keys = false): void |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Save array to json file. |
||
| 305 | * |
||
| 306 | * @param string $path |
||
| 307 | * @param array|ArrayAccess $array |
||
| 308 | * @param bool $sort_keys |
||
| 309 | */ |
||
| 310 | public function storeAsJson(string $path, $array, bool $sort_keys = false): void |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Save array to php file. |
||
| 319 | * |
||
| 320 | * @param string $path |
||
| 321 | * @param array|ArrayAccess $array |
||
| 322 | * @param bool $sort_keys |
||
| 323 | */ |
||
| 324 | public function storeAsArray(string $path, $array, bool $sort_keys = false): void |
||
| 325 | { |
||
| 326 | $this->prepareToStore($path, StubTool::PHP_ARRAY, $array, static function (array $array) { |
||
| 327 | return var_export($array, true); |
||
| 328 | }, $sort_keys); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Prepare an array for writing to a file. |
||
| 333 | * |
||
| 334 | * @param string $path |
||
| 335 | * @param string $stub |
||
| 336 | * @param array $array |
||
| 337 | * @param callable $replace |
||
| 338 | * @param bool $sort_keys |
||
| 339 | */ |
||
| 340 | protected function prepareToStore(string $path, string $stub, array $array, callable $replace, bool $sort_keys = false): void |
||
| 351 | } |
||
| 352 | } |
||
| 353 |