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 Arrayy 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 Arrayy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Arrayy extends ArrayyAbstract implements \Countable, \IteratorAggregate, \ArrayAccess |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Initializes |
||
| 18 | * |
||
| 19 | * @param array $array |
||
| 20 | */ |
||
| 21 | 383 | public function __construct($array = array()) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * magic to string |
||
| 46 | * |
||
| 47 | * @return string |
||
| 48 | */ |
||
| 49 | 14 | public function __toString() |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Get a data by key |
||
| 56 | * |
||
| 57 | * @param $key |
||
| 58 | * |
||
| 59 | * @return mixed |
||
| 60 | */ |
||
| 61 | public function &__get($key) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Assigns a value to the specified data |
||
| 68 | * |
||
| 69 | * @param $key |
||
| 70 | * @param $value |
||
| 71 | */ |
||
| 72 | public function __set($key, $value) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Whether or not an data exists by key |
||
| 79 | * |
||
| 80 | * @param $key |
||
| 81 | * |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | public function __isset($key) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Unsets an data by key |
||
| 91 | * |
||
| 92 | * @param mixed $key |
||
| 93 | */ |
||
| 94 | public function __unset($key) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Assigns a value to the specified offset |
||
| 101 | * |
||
| 102 | * |
||
| 103 | * @param mixed $offset |
||
| 104 | * @param mixed $value |
||
| 105 | */ |
||
| 106 | 1 | public function offsetSet($offset, $value) |
|
| 107 | { |
||
| 108 | 1 | if (null === $offset) { |
|
| 109 | $this->array[] = $value; |
||
| 110 | } else { |
||
| 111 | 1 | $this->array[$offset] = $value; |
|
| 112 | } |
||
| 113 | 1 | } |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Whether or not an offset exists |
||
| 117 | * |
||
| 118 | * @param mixed $offset |
||
| 119 | * |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | 9 | public function offsetExists($offset) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Unsets an offset |
||
| 129 | * |
||
| 130 | * @param mixed $offset |
||
| 131 | */ |
||
| 132 | 1 | public function offsetUnset($offset) |
|
| 133 | { |
||
| 134 | 1 | if ($this->offsetExists($offset)) { |
|
| 135 | 1 | unset($this->array[$offset]); |
|
| 136 | 1 | } |
|
| 137 | 1 | } |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Returns the value at specified offset |
||
| 141 | * |
||
| 142 | * @param mixed $offset |
||
| 143 | * |
||
| 144 | * @return null |
||
| 145 | */ |
||
| 146 | 8 | public function offsetGet($offset) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Returns a new ArrayIterator, thus implementing the IteratorAggregate |
||
| 153 | * interface. |
||
| 154 | * |
||
| 155 | * @return \ArrayIterator An iterator for the values in the array |
||
| 156 | */ |
||
| 157 | 2 | public function getIterator() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * call object as function |
||
| 164 | * |
||
| 165 | * @param mixed $key |
||
| 166 | * |
||
| 167 | * @return mixed |
||
| 168 | */ |
||
| 169 | public function __invoke($key = null) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * get the current array from the "Arrayy"-object |
||
| 184 | * |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | 216 | public function getArray() |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Creates a Arrayy object |
||
| 194 | * |
||
| 195 | * @param array $array |
||
| 196 | * |
||
| 197 | * @return Arrayy |
||
| 198 | */ |
||
| 199 | 286 | public static function create($array = array()) |
|
| 203 | |||
| 204 | //////////////////////////////////////////////////////////////////// |
||
| 205 | ///////////////////////////// ANALYZE ////////////////////////////// |
||
| 206 | //////////////////////////////////////////////////////////////////// |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Search for the value of the current array via $index. |
||
| 210 | * |
||
| 211 | * @param mixed $index |
||
| 212 | * |
||
| 213 | * @return Arrayy |
||
| 214 | */ |
||
| 215 | 7 | public function searchValue($index) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Search for the first index of the current array via $value. |
||
| 233 | * |
||
| 234 | * @param mixed $value |
||
| 235 | * |
||
| 236 | * @return Arrayy |
||
| 237 | */ |
||
| 238 | 7 | public function searchIndex($value) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Check if all items in current array match a truth test. |
||
| 253 | * |
||
| 254 | * @param \Closure $closure |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | 9 | View Code Duplication | public function matches(\Closure $closure) |
| 271 | |||
| 272 | /** |
||
| 273 | * Check if any item in the current array matches a truth test. |
||
| 274 | * |
||
| 275 | * @param \Closure $closure |
||
| 276 | * |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | 9 | View Code Duplication | public function matchesAny(\Closure $closure) |
| 292 | |||
| 293 | /** |
||
| 294 | * Check if we have named keys in the current array. |
||
| 295 | * |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | 12 | public function isAssoc() |
|
| 299 | { |
||
| 300 | 12 | if (count($this->array) === 0) { |
|
| 301 | 1 | return false; |
|
| 302 | } |
||
| 303 | |||
| 304 | 11 | return (bool)count(array_filter(array_keys($this->array), 'is_string')); |
|
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Check if the current array is a multi-array. |
||
| 309 | * |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | 13 | public function isMultiArray() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Check if an item is in the current array. |
||
| 319 | * |
||
| 320 | * @param mixed $value |
||
| 321 | * |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | 9 | public function contains($value) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Returns the average value of the current array. |
||
| 331 | * |
||
| 332 | * @param int $decimals The number of decimals to return |
||
| 333 | * |
||
| 334 | * @return int|double The average value |
||
| 335 | */ |
||
| 336 | 10 | public function average($decimals = null) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Count the values from the current array. |
||
| 353 | * |
||
| 354 | * INFO: only a alias for "$arrayy->size()" |
||
| 355 | * |
||
| 356 | * @return int |
||
| 357 | */ |
||
| 358 | 10 | public function length() |
|
| 359 | { |
||
| 360 | 10 | return $this->size(); |
|
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Count the values from the current array. |
||
| 365 | * |
||
| 366 | * INFO: only a alias for "$arrayy->size()" |
||
| 367 | * |
||
| 368 | * @return int |
||
| 369 | */ |
||
| 370 | 42 | public function count() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Get the size of an array. |
||
| 377 | * |
||
| 378 | * @return int |
||
| 379 | */ |
||
| 380 | 42 | public function size() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Get the max value from an array. |
||
| 387 | * |
||
| 388 | * @return mixed |
||
| 389 | */ |
||
| 390 | 10 | public function max() |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Get the min value from an array. |
||
| 401 | * |
||
| 402 | * @return mixed |
||
| 403 | */ |
||
| 404 | 10 | public function min() |
|
| 412 | |||
| 413 | //////////////////////////////////////////////////////////////////// |
||
| 414 | //////////////////////////// FETCH FROM //////////////////////////// |
||
| 415 | //////////////////////////////////////////////////////////////////// |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Find the first item in an array that passes the truth test, |
||
| 419 | * otherwise return false |
||
| 420 | * |
||
| 421 | * @param \Closure $closure |
||
| 422 | * |
||
| 423 | * @return mixed|false false if we couldn't find the value |
||
| 424 | */ |
||
| 425 | 7 | public function find(\Closure $closure) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Clean all falsy values from an array. |
||
| 438 | * |
||
| 439 | * @return Arrayy |
||
| 440 | */ |
||
| 441 | 8 | public function clean() |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Get a random string from an array. |
||
| 452 | * |
||
| 453 | * @param null|int $take how many values you will take? |
||
| 454 | * |
||
| 455 | * @return Arrayy |
||
| 456 | */ |
||
| 457 | 9 | public function random($take = null) |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Return an array with all elements found in input array. |
||
| 470 | * |
||
| 471 | * @param array $search |
||
| 472 | * |
||
| 473 | * @return Arrayy |
||
| 474 | */ |
||
| 475 | 2 | public function intersection(array $search) |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Return a boolean flag which indicates whether the two input arrays have any common elements. |
||
| 482 | * |
||
| 483 | * @param array $search |
||
| 484 | * |
||
| 485 | * @return bool |
||
| 486 | */ |
||
| 487 | 1 | public function intersects(array $search) |
|
| 491 | |||
| 492 | //////////////////////////////////////////////////////////////////// |
||
| 493 | ///////////////////////////// SLICERS ////////////////////////////// |
||
| 494 | //////////////////////////////////////////////////////////////////// |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get the first value(s) from the current array. |
||
| 498 | * |
||
| 499 | * @param int|null $take how many values you will take? |
||
| 500 | * |
||
| 501 | * @return Arrayy |
||
| 502 | */ |
||
| 503 | 29 | public function first($take = null) |
|
| 513 | |||
| 514 | /** |
||
| 515 | * Get the last value(s) from the current array. |
||
| 516 | * |
||
| 517 | * @param int|null $take |
||
| 518 | * |
||
| 519 | * @return Arrayy |
||
| 520 | */ |
||
| 521 | 11 | public function last($take = null) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Get everything but the last..$to items. |
||
| 534 | * |
||
| 535 | * @param int $to |
||
| 536 | * |
||
| 537 | * @return Arrayy |
||
| 538 | */ |
||
| 539 | 12 | public function initial($to = 1) |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Get the last elements from index $from until the end of this array. |
||
| 548 | * |
||
| 549 | * @param int $from |
||
| 550 | * |
||
| 551 | * @return Arrayy |
||
| 552 | */ |
||
| 553 | 16 | public function rest($from = 1) |
|
| 557 | |||
| 558 | //////////////////////////////////////////////////////////////////// |
||
| 559 | ///////////////////////////// ACT UPON ///////////////////////////// |
||
| 560 | //////////////////////////////////////////////////////////////////// |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Iterate over an array and execute a callback for each loop. |
||
| 564 | * |
||
| 565 | * @param \Closure $closure |
||
| 566 | * |
||
| 567 | * @return Arrayy |
||
| 568 | */ |
||
| 569 | 1 | View Code Duplication | public function at(\Closure $closure) |
| 579 | |||
| 580 | //////////////////////////////////////////////////////////////////// |
||
| 581 | ////////////////////////////// ALTER /////////////////////////////// |
||
| 582 | //////////////////////////////////////////////////////////////////// |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Merge the new $array into the current array. |
||
| 586 | * |
||
| 587 | * - replace duplicate keys from the current array with the key,values from the new $array |
||
| 588 | * - create new indexes |
||
| 589 | * |
||
| 590 | * @param array $array |
||
| 591 | * |
||
| 592 | * @return Arrayy |
||
| 593 | */ |
||
| 594 | 8 | public function mergeAppendNewIndex(array $array = array()) |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Merge the current array into the new $array. |
||
| 601 | * |
||
| 602 | * - replace duplicate keys from new $array with the key,values from the current array |
||
| 603 | * - create new indexes |
||
| 604 | * |
||
| 605 | * @param array $array |
||
| 606 | * |
||
| 607 | * @return Arrayy |
||
| 608 | */ |
||
| 609 | 8 | public function mergePrependNewIndex(array $array = array()) |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Merge the new $array into the current array. |
||
| 616 | * |
||
| 617 | * - keep key,value from the current array, also if the index is in the new $array |
||
| 618 | * |
||
| 619 | * @param array $array |
||
| 620 | * |
||
| 621 | * @return Arrayy |
||
| 622 | */ |
||
| 623 | 8 | public function mergeAppendKeepIndex(array $array = array()) |
|
| 627 | |||
| 628 | /** |
||
| 629 | * Merge the the current array into the $array. |
||
| 630 | * |
||
| 631 | * - use key,value from the new $array, also if the index is in the current array |
||
| 632 | * |
||
| 633 | * @param array $array |
||
| 634 | * |
||
| 635 | * @return Arrayy |
||
| 636 | */ |
||
| 637 | 8 | public function mergePrependKeepIndex(array $array = array()) |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Return values that are only in the current array. |
||
| 644 | * |
||
| 645 | * @param array $array |
||
| 646 | * |
||
| 647 | * @return Arrayy |
||
| 648 | */ |
||
| 649 | 8 | public function diff(array $array = array()) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Return values that are only in the new $array. |
||
| 656 | * |
||
| 657 | * @param array $array |
||
| 658 | * |
||
| 659 | * @return Arrayy |
||
| 660 | */ |
||
| 661 | 8 | public function diffReverse(array $array = array()) |
|
| 665 | |||
| 666 | /** |
||
| 667 | * Replace the first matched value in an array. |
||
| 668 | * |
||
| 669 | * @param string $search The string to replace |
||
| 670 | * @param string $replacement What to replace it with |
||
| 671 | * |
||
| 672 | * @return Arrayy |
||
| 673 | */ |
||
| 674 | 3 | public function replaceOneValue($search, $replacement = '') |
|
| 685 | |||
| 686 | /** |
||
| 687 | * Replace values in an array. |
||
| 688 | * |
||
| 689 | * @param string $search The string to replace |
||
| 690 | * @param string $replacement What to replace it with |
||
| 691 | * |
||
| 692 | * @return Arrayy |
||
| 693 | */ |
||
| 694 | 1 | public function replaceValues($search, $replacement = '') |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Replace the keys in an array with another set. |
||
| 707 | * |
||
| 708 | * @param array $keys An array of keys matching the array's size |
||
| 709 | * |
||
| 710 | * @return Arrayy |
||
| 711 | */ |
||
| 712 | 1 | public function replaceKeys(array $keys) |
|
| 718 | |||
| 719 | /** |
||
| 720 | * Iterate over an array and modify the array's value. |
||
| 721 | * |
||
| 722 | * @param \Closure $closure |
||
| 723 | * |
||
| 724 | * @return array |
||
| 725 | */ |
||
| 726 | 21 | public function each(\Closure $closure) |
|
| 727 | { |
||
| 728 | 21 | $array = $this->array; |
|
| 729 | |||
| 730 | 21 | foreach ($array as $key => &$value) { |
|
| 731 | 17 | $value = $closure($value, $key); |
|
| 732 | 21 | } |
|
| 733 | |||
| 734 | 21 | return $array; |
|
| 735 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Shuffle an array. |
||
| 739 | * |
||
| 740 | * @return Arrayy |
||
| 741 | */ |
||
| 742 | 1 | public function shuffle() |
|
| 750 | |||
| 751 | /** |
||
| 752 | * Sort an array by key. |
||
| 753 | * |
||
| 754 | * @param string $direction |
||
| 755 | * |
||
| 756 | * @return Arrayy |
||
| 757 | */ |
||
| 758 | 8 | public function sortKeys($direction = 'ASC') |
|
| 777 | |||
| 778 | /** |
||
| 779 | * Implodes an array. |
||
| 780 | * |
||
| 781 | * @param string $with What to implode it with |
||
| 782 | * |
||
| 783 | * @return string |
||
| 784 | */ |
||
| 785 | 22 | public function implode($with = '') |
|
| 789 | |||
| 790 | /** |
||
| 791 | * Find all items in an array that pass the truth test. |
||
| 792 | * |
||
| 793 | * @param \Closure $closure |
||
| 794 | * |
||
| 795 | * @return Arrayy |
||
| 796 | */ |
||
| 797 | 8 | public function filter($closure = null) |
|
| 798 | { |
||
| 799 | 8 | if (!$closure) { |
|
| 800 | 1 | return $this->clean(); |
|
| 801 | } |
||
| 802 | |||
| 803 | 8 | $array = array_filter($this->array, $closure); |
|
| 804 | |||
| 805 | 8 | return static::create($array); |
|
| 806 | } |
||
| 807 | |||
| 808 | /** |
||
| 809 | * Invoke a function on all of an array's values. |
||
| 810 | * |
||
| 811 | * @param mixed $callable |
||
| 812 | * @param array $arguments |
||
| 813 | * |
||
| 814 | * @return Arrayy |
||
| 815 | */ |
||
| 816 | 1 | public function invoke($callable, $arguments = array()) |
|
| 832 | |||
| 833 | /** |
||
| 834 | * Return all items that fail the truth test. |
||
| 835 | * |
||
| 836 | * @param \Closure $closure |
||
| 837 | * |
||
| 838 | * @return Arrayy |
||
| 839 | */ |
||
| 840 | 1 | View Code Duplication | public function reject(\Closure $closure) |
| 852 | |||
| 853 | /** |
||
| 854 | * Remove the first value from an array. |
||
| 855 | * |
||
| 856 | * @return Arrayy |
||
| 857 | */ |
||
| 858 | 8 | public function removeFirst() |
|
| 864 | |||
| 865 | /** |
||
| 866 | * Remove the last value from an array. |
||
| 867 | * |
||
| 868 | * @return Arrayy |
||
| 869 | */ |
||
| 870 | 7 | public function removeLast() |
|
| 876 | |||
| 877 | /** |
||
| 878 | * Removes a particular value from an array (numeric or associative). |
||
| 879 | * |
||
| 880 | * @param mixed $value |
||
| 881 | * |
||
| 882 | * @return Arrayy |
||
| 883 | */ |
||
| 884 | 7 | public function removeValue($value) |
|
| 902 | |||
| 903 | /** |
||
| 904 | * Prepend a value to an array. |
||
| 905 | * |
||
| 906 | * @param mixed $value |
||
| 907 | * |
||
| 908 | * @return Arrayy |
||
| 909 | */ |
||
| 910 | 7 | public function prepend($value) |
|
| 916 | |||
| 917 | /** |
||
| 918 | * Append a value to an array. |
||
| 919 | * |
||
| 920 | * @param mixed $value |
||
| 921 | * |
||
| 922 | * @return Arrayy |
||
| 923 | */ |
||
| 924 | 8 | public function append($value) |
|
| 930 | |||
| 931 | /** |
||
| 932 | * Return the array in the reverse order. |
||
| 933 | * |
||
| 934 | * @return Arrayy |
||
| 935 | */ |
||
| 936 | 7 | public function reverse() |
|
| 942 | |||
| 943 | /** |
||
| 944 | * duplicate free copy of an array |
||
| 945 | * |
||
| 946 | * @return Arrayy |
||
| 947 | */ |
||
| 948 | 7 | public function unique() |
|
| 964 | } |
||
| 965 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.