Complex classes like AbstractArray 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 AbstractArray, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | abstract class AbstractArray implements |
||
| 27 | ArrayAccess, |
||
| 28 | ConvertibleInterface, |
||
| 29 | Countable, |
||
| 30 | DebuggableInterface, |
||
| 31 | DoubleEndedQueueInterface, |
||
| 32 | IteratorAggregate, |
||
| 33 | SortableInterface, |
||
| 34 | TraversableInterface |
||
| 35 | { |
||
| 36 | use ConvertibleTrait; |
||
| 37 | |||
| 38 | use DebuggableTrait; |
||
| 39 | |||
| 40 | use DoubleEndedQueueTrait; |
||
| 41 | |||
| 42 | use SortableTrait; |
||
| 43 | |||
| 44 | use TraversableTrait; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @const string |
||
| 48 | */ |
||
| 49 | const DEFAULT_SEPARATOR = ', '; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $elements = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Construct new instance |
||
| 58 | * |
||
| 59 | * @param array $elements |
||
| 60 | */ |
||
| 61 | 307 | public function __construct(array $elements = []) |
|
| 65 | |||
| 66 | // The abstract public method list order by ASC |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Create a chunked version of current array. |
||
| 70 | * |
||
| 71 | * @param int $size Size of each chunk |
||
| 72 | * @param bool $preserveKeys Whether array keys are preserved or no |
||
| 73 | * |
||
| 74 | * @return AbstractArray An array of chunks from the current array |
||
| 75 | */ |
||
| 76 | abstract public function chunk($size, $preserveKeys = false); |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Clear the current array. |
||
| 80 | * |
||
| 81 | * @return AbstractArray The current empty array |
||
| 82 | */ |
||
| 83 | abstract public function clear(); |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Create an array using the current array as keys and the other array as values. |
||
| 87 | * |
||
| 88 | * @param array $array Values array |
||
| 89 | * |
||
| 90 | * @return AbstractArray An array with values from the other array |
||
| 91 | */ |
||
| 92 | abstract public function combine(array $array); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Compute the current array values which not present in the given one. |
||
| 96 | * |
||
| 97 | * @param array $array Array for diff |
||
| 98 | * |
||
| 99 | * @return AbstractArray An array containing all the entries from this array |
||
| 100 | * that are not present in $array |
||
| 101 | */ |
||
| 102 | abstract public function diff(array $array); |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Filter the current array for elements satisfying the predicate $func. |
||
| 106 | * |
||
| 107 | * @param callable $func |
||
| 108 | * |
||
| 109 | * @return AbstractArray An array with only element satisfying $func |
||
| 110 | */ |
||
| 111 | abstract public function filter(callable $func); |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Exchanges all keys of current array with their associated values. |
||
| 115 | * |
||
| 116 | * @return AbstractArray An array with flipped elements |
||
| 117 | */ |
||
| 118 | abstract public function flip(); |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Compute the current array values which present in the given one. |
||
| 122 | * |
||
| 123 | * @param array $array Array for intersect |
||
| 124 | * |
||
| 125 | * @return AbstractArray An array with containing all the entries from this array |
||
| 126 | * that are present in $array |
||
| 127 | */ |
||
| 128 | abstract public function intersect(array $array); |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Compute the current array values with additional index check which present in the given one. |
||
| 132 | * |
||
| 133 | * @param array $array Array for intersect |
||
| 134 | * |
||
| 135 | * @return AbstractArray An array with containing all the entries from this array |
||
| 136 | * that are present in $array. Note that the keys are also used in the comparison |
||
| 137 | * unlike in intersect(). |
||
| 138 | */ |
||
| 139 | abstract public function intersectAssoc(array $array); |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Compute the current array using keys for comparison which present in the given one. |
||
| 143 | * |
||
| 144 | * @param array $array Array for intersect |
||
| 145 | * |
||
| 146 | * @return AbstractArray An array with containing all the entries from this array |
||
| 147 | * which have keys that are present in $array. |
||
| 148 | */ |
||
| 149 | abstract public function intersectKey(array $array); |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Apply the given function to the every element of the current array, |
||
| 153 | * collecting the results. |
||
| 154 | * |
||
| 155 | * @param callable $func |
||
| 156 | * |
||
| 157 | * @return AbstractArray An array with modified elements |
||
| 158 | */ |
||
| 159 | abstract public function map(callable $func); |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Merge the current array with the provided one. The latter array is overwriting. |
||
| 163 | * |
||
| 164 | * @param array $array Array to merge with (overwrites) |
||
| 165 | * @param bool $recursively Whether array will be merged recursively or no |
||
| 166 | * |
||
| 167 | * @return AbstractArray An array with the keys/values from $array added |
||
| 168 | */ |
||
| 169 | abstract public function merge(array $array, $recursively = false); |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Pad the current array to the specified size with a given value. |
||
| 173 | * |
||
| 174 | * @param int $size Size of the result array |
||
| 175 | * @param mixed $value Empty value by default |
||
| 176 | * |
||
| 177 | * @return AbstractArray An array padded to $size with $value |
||
| 178 | */ |
||
| 179 | abstract public function pad($size, $value); |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Create a numerically re-indexed array based on the current array. |
||
| 183 | * |
||
| 184 | * @return AbstractArray An array with re-indexed elements |
||
| 185 | */ |
||
| 186 | abstract public function reindex(); |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Replace values in the current array with values in the given one |
||
| 190 | * that have the same key. |
||
| 191 | * |
||
| 192 | * @param array $array Array of replacing values |
||
| 193 | * @param bool $recursively Whether array will be replaced recursively or no |
||
| 194 | * |
||
| 195 | * @return AbstractArray An array with the same keys but new values |
||
| 196 | */ |
||
| 197 | abstract public function replace(array $array, $recursively = false); |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Reverse the values order of the current array. |
||
| 201 | * |
||
| 202 | * @param bool $preserveKeys Whether array keys are preserved or no |
||
| 203 | * |
||
| 204 | * @return AbstractArray An array with the order of the elements reversed |
||
| 205 | */ |
||
| 206 | abstract public function reverse($preserveKeys = false); |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Randomize elements order of the current array. |
||
| 210 | * |
||
| 211 | * @return AbstractArray An array with the shuffled elements order |
||
| 212 | */ |
||
| 213 | abstract public function shuffle(); |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Extract a slice of the current array. |
||
| 217 | * |
||
| 218 | * @param int $offset Slice begin index |
||
| 219 | * @param int|null $length Length of the slice |
||
| 220 | * @param bool $preserveKeys Whether array keys are preserved or no |
||
| 221 | * |
||
| 222 | * @return AbstractArray A new array, which is slice of the current array |
||
| 223 | * with specified $length |
||
| 224 | */ |
||
| 225 | abstract public function slice($offset, $length = null, $preserveKeys = false); |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Remove duplicate values from the current array. |
||
| 229 | * |
||
| 230 | * @param int|null $sortFlags |
||
| 231 | * |
||
| 232 | * @return AbstractArray An array with only unique elements |
||
| 233 | */ |
||
| 234 | abstract public function unique($sortFlags = null); |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Apply the given function to the every element of the current array, |
||
| 238 | * discarding the results. |
||
| 239 | * |
||
| 240 | * @param callable $func |
||
| 241 | * @param bool $recursively Whether array will be walked recursively or no |
||
| 242 | * |
||
| 243 | * @return AbstractArray An array with modified elements |
||
| 244 | */ |
||
| 245 | abstract public function walk(callable $func, $recursively = false); |
||
| 246 | |||
| 247 | // The public static method list order by ASC |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Create a new instance. |
||
| 251 | * |
||
| 252 | * @param array $elements |
||
| 253 | * |
||
| 254 | * @return AbstractArray Returns created instance |
||
| 255 | */ |
||
| 256 | 19 | public static function create(array $elements = []) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Decode a JSON string to new instance. |
||
| 263 | * |
||
| 264 | * @param string $json The JSON string being decoded |
||
| 265 | * @param int $options Bitmask of JSON decode options |
||
| 266 | * @param int $depth Specified recursion depth |
||
| 267 | * |
||
| 268 | * @return AbstractArray The created array |
||
| 269 | */ |
||
| 270 | 8 | public static function createFromJson($json, $options = 0, $depth = 512) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Create a new instance filled with values from an object implementing ArrayAccess. |
||
| 277 | * |
||
| 278 | * @param ArrayAccess $elements Object that implements ArrayAccess |
||
| 279 | * |
||
| 280 | * @return AbstractArray Returns created instance |
||
| 281 | */ |
||
| 282 | 8 | public static function createFromObject(ArrayAccess $elements) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Explode a string to new instance by specified separator. |
||
| 295 | * |
||
| 296 | * @param string $string Converted string |
||
| 297 | * @param string $separator Element's separator |
||
| 298 | * |
||
| 299 | * @return AbstractArray The created array |
||
| 300 | */ |
||
| 301 | 6 | public static function createFromString($string, $separator) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Create a new instance containing a range of elements. |
||
| 308 | * |
||
| 309 | * @param mixed $low First value of the sequence |
||
| 310 | * @param mixed $high The sequence is ended upon reaching the end value |
||
| 311 | * @param int $step Used as the increment between elements in the sequence |
||
| 312 | * |
||
| 313 | * @return AbstractArray The created array |
||
| 314 | */ |
||
| 315 | 1 | public static function createWithRange($low, $high, $step = 1) |
|
| 319 | |||
| 320 | // The public method list order by ASC |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Check if the given value exists in the array. |
||
| 324 | * |
||
| 325 | * @param mixed $element Value to search for |
||
| 326 | * |
||
| 327 | * @return bool Returns true if the given value exists in the array, false otherwise |
||
| 328 | */ |
||
| 329 | 4 | public function contains($element) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Check if the given key/index exists in the array. |
||
| 336 | * |
||
| 337 | * @param mixed $key Key/index to search for |
||
| 338 | * |
||
| 339 | * @return bool Returns true if the given key/index exists in the array, false otherwise |
||
| 340 | */ |
||
| 341 | 4 | public function containsKey($key) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Returns the number of values in the array. |
||
| 348 | * |
||
| 349 | * @link http://php.net/manual/en/function.count.php |
||
| 350 | * |
||
| 351 | * @return int total number of values |
||
| 352 | */ |
||
| 353 | 31 | public function count() |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Clone current instance to new instance. |
||
| 360 | * |
||
| 361 | * @deprecated Should be removed |
||
| 362 | * |
||
| 363 | * @return AbstractArray Shallow copy of $this |
||
| 364 | */ |
||
| 365 | 4 | public function createClone() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Return slice of an array except given keys. |
||
| 372 | * |
||
| 373 | * @param array $keys List of keys to exclude |
||
| 374 | * |
||
| 375 | * @return AbstractArray The created array |
||
| 376 | */ |
||
| 377 | 3 | public function except(array $keys) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Find the given value in An array using a closure |
||
| 384 | * |
||
| 385 | * @param callable $func |
||
| 386 | * |
||
| 387 | * @return bool Returns true if the given value is found, false otherwise |
||
| 388 | */ |
||
| 389 | 4 | public function exists(callable $func) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Returns the first occurrence of a value that satisfies the predicate $func. |
||
| 405 | * |
||
| 406 | * @param callable $func |
||
| 407 | * |
||
| 408 | * @return mixed The first occurrence found |
||
| 409 | */ |
||
| 410 | 1 | public function find(callable $func) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Create an iterator over this array. |
||
| 426 | * |
||
| 427 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 428 | * |
||
| 429 | * @return Traversable An instance of an object implementing <b>Iterator</b> |
||
| 430 | */ |
||
| 431 | 12 | public function getIterator() |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Return an array all the keys of this array. |
||
| 438 | * |
||
| 439 | * @return array An array of all keys |
||
| 440 | */ |
||
| 441 | 10 | public function getKeys() |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Pick a random value out of this array. |
||
| 448 | * |
||
| 449 | * @return mixed Random value of array |
||
| 450 | * |
||
| 451 | * @throws \RangeException If array is empty |
||
| 452 | */ |
||
| 453 | 3 | public function getRandom() |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Pick a random key/index from the keys of this array. |
||
| 460 | * |
||
| 461 | * @return mixed Random key/index of array |
||
| 462 | * |
||
| 463 | * @throws \RangeException If array is empty |
||
| 464 | */ |
||
| 465 | 6 | public function getRandomKey() |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Pick a given number of random keys/indexes out of this array. |
||
| 472 | * |
||
| 473 | * @param int $number The number of keys/indexes (should be <= $this->count()) |
||
| 474 | * |
||
| 475 | * @return mixed Random keys or key of array |
||
| 476 | * |
||
| 477 | * @throws \RangeException |
||
| 478 | */ |
||
| 479 | 27 | public function getRandomKeys($number) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Pick a given number of random values with non-duplicate indexes out of the array. |
||
| 497 | * |
||
| 498 | * @param int $number The number of values (should be > 1 and < $this->count()) |
||
| 499 | * |
||
| 500 | * @return array Random values of array |
||
| 501 | * |
||
| 502 | * @throws \RangeException |
||
| 503 | */ |
||
| 504 | 6 | public function getRandomValues($number) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Return an array of all values from this array numerically indexed. |
||
| 518 | * |
||
| 519 | * @return mixed An array of all values |
||
| 520 | */ |
||
| 521 | 4 | public function getValues() |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Alias of search() method. Search for a given element and return |
||
| 528 | * the index of its first occurrence. |
||
| 529 | * |
||
| 530 | * @param mixed $element Value to search for |
||
| 531 | * |
||
| 532 | * @return mixed The corresponding key/index |
||
| 533 | */ |
||
| 534 | 4 | public function indexOf($element) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Check whether array is associative or not. |
||
| 541 | * |
||
| 542 | * @return bool Returns true if associative, false otherwise |
||
| 543 | */ |
||
| 544 | 4 | public function isAssoc() |
|
| 554 | |||
| 555 | /** |
||
| 556 | * Check whether the array is empty or not. |
||
| 557 | * |
||
| 558 | * @return bool Returns true if empty, false otherwise |
||
| 559 | */ |
||
| 560 | 12 | public function isEmpty() |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Check if an array keys are in a given variable type. |
||
| 567 | * |
||
| 568 | * @param string $type |
||
| 569 | * |
||
| 570 | * @return bool |
||
| 571 | */ |
||
| 572 | 6 | private function checkType($type) |
|
| 585 | |||
| 586 | /** |
||
| 587 | * Check whether array is numeric or not. |
||
| 588 | * |
||
| 589 | * @return bool Returns true if numeric, false otherwise |
||
| 590 | */ |
||
| 591 | 4 | public function isNumeric() |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Whether an offset exists. |
||
| 604 | * |
||
| 605 | * @param mixed $offset An offset to check for. |
||
| 606 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 607 | * |
||
| 608 | * @return boolean true on success or false on failure. |
||
| 609 | */ |
||
| 610 | 8 | public function offsetExists($offset) |
|
| 614 | |||
| 615 | /** |
||
| 616 | * Retrieve the current offset or null. |
||
| 617 | * |
||
| 618 | * @param mixed $offset The offset to retrieve. |
||
| 619 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 620 | * |
||
| 621 | * @return mixed Can return all value types. |
||
| 622 | */ |
||
| 623 | 13 | public function offsetGet($offset) |
|
| 630 | |||
| 631 | /** |
||
| 632 | * Set an offset for this array. |
||
| 633 | * |
||
| 634 | * @param mixed $offset The offset to assign the value to. |
||
| 635 | * @param mixed $value The value to set. |
||
| 636 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 637 | * |
||
| 638 | * @return $this |
||
| 639 | */ |
||
| 640 | 14 | public function offsetSet($offset, $value) |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Remove a present offset. |
||
| 653 | * |
||
| 654 | * @param mixed $offset The offset to unset. |
||
| 655 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 656 | * |
||
| 657 | * @return $this |
||
| 658 | */ |
||
| 659 | 10 | public function offsetUnset($offset) |
|
| 665 | |||
| 666 | /** |
||
| 667 | * Return slice of an array with just a given keys. |
||
| 668 | * |
||
| 669 | * @param array $keys List of keys to return |
||
| 670 | * |
||
| 671 | * @return AbstractArray The created array |
||
| 672 | */ |
||
| 673 | 3 | public function only(array $keys) |
|
| 677 | |||
| 678 | /** |
||
| 679 | * Reduce the array to a single value iteratively combining all values using $func. |
||
| 680 | * |
||
| 681 | * @param callable $func callback ($carry, $item) -> next $carry |
||
| 682 | * @param mixed|null $initial starting value of the $carry |
||
| 683 | * |
||
| 684 | * @return mixed Final value of $carry |
||
| 685 | */ |
||
| 686 | 1 | public function reduce(callable $func, $initial = null) |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Search for a given element and return the index of its first occurrence. |
||
| 693 | * |
||
| 694 | * @param mixed $element Value to search for |
||
| 695 | * |
||
| 696 | * @return mixed The corresponding key/index |
||
| 697 | */ |
||
| 698 | 4 | public function search($element) |
|
| 702 | } |
||
| 703 |