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 |
||
| 13 | class Arrayy extends ArrayyAbstract implements \Countable, \IteratorAggregate, \ArrayAccess, \Serializable |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Initializes |
||
| 17 | * |
||
| 18 | * @param array $array |
||
| 19 | */ |
||
| 20 | 407 | public function __construct($array = array()) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * magic to string |
||
| 45 | * |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | 14 | public function __toString() |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @return mixed |
||
| 55 | */ |
||
| 56 | public function serialize() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $array |
||
| 63 | */ |
||
| 64 | public function unserialize($array) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get a value by key. |
||
| 71 | * |
||
| 72 | * @param $key |
||
| 73 | * |
||
| 74 | * @return mixed |
||
| 75 | */ |
||
| 76 | public function &__get($key) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Assigns a value to the specified element. |
||
| 83 | * |
||
| 84 | * @param $key |
||
| 85 | * @param $value |
||
| 86 | */ |
||
| 87 | public function __set($key, $value) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Whether or not an element exists by key. |
||
| 94 | * |
||
| 95 | * @param $key |
||
| 96 | * |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | public function __isset($key) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Unset element by key |
||
| 106 | * |
||
| 107 | * @param mixed $key |
||
| 108 | */ |
||
| 109 | public function __unset($key) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Assigns a value to the specified offset. |
||
| 116 | * |
||
| 117 | * @param mixed $offset |
||
| 118 | * @param mixed $value |
||
| 119 | */ |
||
| 120 | 2 | public function offsetSet($offset, $value) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Whether or not an offset exists. |
||
| 131 | * |
||
| 132 | * @param mixed $offset |
||
| 133 | * |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | 9 | public function offsetExists($offset) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Unset an offset. |
||
| 143 | * |
||
| 144 | * @param mixed $offset |
||
| 145 | */ |
||
| 146 | 1 | public function offsetUnset($offset) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Returns the value at specified offset. |
||
| 155 | * |
||
| 156 | * @param mixed $offset |
||
| 157 | * |
||
| 158 | * @return null |
||
| 159 | */ |
||
| 160 | 8 | public function offsetGet($offset) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Returns a new ArrayIterator, thus implementing the IteratorAggregate |
||
| 167 | * interface. |
||
| 168 | * |
||
| 169 | * @return \ArrayIterator An iterator for the values in the array |
||
| 170 | */ |
||
| 171 | 2 | public function getIterator() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * call object as function |
||
| 178 | * |
||
| 179 | * @param mixed $key |
||
| 180 | * |
||
| 181 | * @return mixed |
||
| 182 | */ |
||
| 183 | public function __invoke($key = null) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * get the current array from the "Arrayy"-object |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | 235 | public function getArray() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Creates a Arrayy object |
||
| 208 | * |
||
| 209 | * @param array $array |
||
| 210 | * |
||
| 211 | * @return Arrayy |
||
| 212 | */ |
||
| 213 | 310 | public static function create($array = array()) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Create a new Arrayy object via string. |
||
| 220 | * |
||
| 221 | * @param string $str The input string. |
||
| 222 | * @param string|null $delimiter The boundary string. |
||
| 223 | * @param string|null $regEx Use the $delimiter or the $regEx, so if $pattern is null, $delimiter will be used. |
||
| 224 | * |
||
| 225 | * @return Arrayy |
||
| 226 | */ |
||
| 227 | 2 | public static function createFromString($str, $delimiter, $regEx = null) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * create a new Arrayy object via JSON, |
||
| 255 | * |
||
| 256 | * @param string $json |
||
| 257 | * |
||
| 258 | * @return Arrayy |
||
| 259 | */ |
||
| 260 | 1 | public static function createFromJson($json) |
|
| 266 | |||
| 267 | //////////////////////////////////////////////////////////////////// |
||
| 268 | ///////////////////////////// ANALYZE ////////////////////////////// |
||
| 269 | //////////////////////////////////////////////////////////////////// |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Search for the value of the current array via $index. |
||
| 273 | * |
||
| 274 | * @param mixed $index |
||
| 275 | * |
||
| 276 | * @return Arrayy will return a empty Arrayy if the value wasn't found |
||
| 277 | */ |
||
| 278 | 7 | public function searchValue($index) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Search for the first index of the current array via $value. |
||
| 296 | * |
||
| 297 | * @param mixed $value |
||
| 298 | * |
||
| 299 | * @return Arrayy will return a empty Arrayy if the index wasn't found |
||
| 300 | */ |
||
| 301 | 16 | public function searchIndex($value) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Check if all items in current array match a truth test. |
||
| 316 | * |
||
| 317 | * @param \Closure $closure |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | 9 | View Code Duplication | public function matches(\Closure $closure) |
| 334 | |||
| 335 | /** |
||
| 336 | * Check if any item in the current array matches a truth test. |
||
| 337 | * |
||
| 338 | * @param \Closure $closure |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | 9 | View Code Duplication | public function matchesAny(\Closure $closure) |
| 355 | |||
| 356 | /** |
||
| 357 | * Check if we have named keys in the current array. |
||
| 358 | * |
||
| 359 | * @return bool |
||
| 360 | */ |
||
| 361 | 12 | public function isAssoc() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Check if the current array is a multi-array. |
||
| 372 | * |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | 13 | public function isMultiArray() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Check if an item is in the current array. |
||
| 382 | * |
||
| 383 | * @param mixed $value |
||
| 384 | * |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | 9 | public function contains($value) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Returns the average value of the current array. |
||
| 394 | * |
||
| 395 | * @param int $decimals The number of decimals to return |
||
| 396 | * |
||
| 397 | * @return int|double The average value |
||
| 398 | */ |
||
| 399 | 10 | public function average($decimals = null) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Count the values from the current array. |
||
| 416 | * |
||
| 417 | * INFO: only a alias for "$arrayy->size()" |
||
| 418 | * |
||
| 419 | * @return int |
||
| 420 | */ |
||
| 421 | 10 | public function length() |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Count the values from the current array. |
||
| 428 | * |
||
| 429 | * INFO: only a alias for "$arrayy->size()" |
||
| 430 | * |
||
| 431 | * @return int |
||
| 432 | */ |
||
| 433 | 59 | public function count() |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Get the size of an array. |
||
| 440 | * |
||
| 441 | * @return int |
||
| 442 | */ |
||
| 443 | 59 | public function size() |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Get the max value from an array. |
||
| 450 | * |
||
| 451 | * @return mixed |
||
| 452 | */ |
||
| 453 | 10 | public function max() |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Get the min value from an array. |
||
| 464 | * |
||
| 465 | * @return mixed |
||
| 466 | */ |
||
| 467 | 10 | public function min() |
|
| 475 | |||
| 476 | //////////////////////////////////////////////////////////////////// |
||
| 477 | //////////////////////////// FETCH FROM //////////////////////////// |
||
| 478 | //////////////////////////////////////////////////////////////////// |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Find the first item in an array that passes the truth test, |
||
| 482 | * otherwise return false |
||
| 483 | * |
||
| 484 | * @param \Closure $closure |
||
| 485 | * |
||
| 486 | * @return mixed|false false if we couldn't find the value |
||
| 487 | */ |
||
| 488 | 7 | public function find(\Closure $closure) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Clean all falsy values from an array. |
||
| 501 | * |
||
| 502 | * @return Arrayy |
||
| 503 | */ |
||
| 504 | 8 | public function clean() |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Get a random string from an array. |
||
| 515 | * |
||
| 516 | * @param null|int $take how many values you will take? |
||
| 517 | * |
||
| 518 | * @return Arrayy |
||
| 519 | */ |
||
| 520 | 18 | public function random($take = null) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Get a random value from an array, with the ability to skew the results. |
||
| 537 | * |
||
| 538 | * Example: randomWeighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar. |
||
| 539 | * |
||
| 540 | * @param array $array |
||
| 541 | * @param null|int $take how many values you will take? |
||
| 542 | * |
||
| 543 | * @return Arrayy |
||
| 544 | */ |
||
| 545 | 9 | public function randomWeighted(array $array, $take = null) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Return an array with all elements found in input array. |
||
| 561 | * |
||
| 562 | * @param array $search |
||
| 563 | * |
||
| 564 | * @return Arrayy |
||
| 565 | */ |
||
| 566 | 2 | public function intersection(array $search) |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Return a boolean flag which indicates whether the two input arrays have any common elements. |
||
| 573 | * |
||
| 574 | * @param array $search |
||
| 575 | * |
||
| 576 | * @return bool |
||
| 577 | */ |
||
| 578 | 1 | public function intersects(array $search) |
|
| 582 | |||
| 583 | //////////////////////////////////////////////////////////////////// |
||
| 584 | ///////////////////////////// SLICERS ////////////////////////////// |
||
| 585 | //////////////////////////////////////////////////////////////////// |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Get the first value(s) from the current array. |
||
| 589 | * |
||
| 590 | * @param int|null $take how many values you will take? |
||
| 591 | * |
||
| 592 | * @return Arrayy |
||
| 593 | */ |
||
| 594 | 33 | public function first($take = null) |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Get the last value(s) from the current array. |
||
| 607 | * |
||
| 608 | * @param int|null $take |
||
| 609 | * |
||
| 610 | * @return Arrayy |
||
| 611 | */ |
||
| 612 | 11 | public function last($take = null) |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Get everything but the last..$to items. |
||
| 625 | * |
||
| 626 | * @param int $to |
||
| 627 | * |
||
| 628 | * @return Arrayy |
||
| 629 | */ |
||
| 630 | 12 | public function initial($to = 1) |
|
| 636 | |||
| 637 | /** |
||
| 638 | * Get the last elements from index $from until the end of this array. |
||
| 639 | * |
||
| 640 | * @param int $from |
||
| 641 | * |
||
| 642 | * @return Arrayy |
||
| 643 | */ |
||
| 644 | 16 | public function rest($from = 1) |
|
| 648 | |||
| 649 | //////////////////////////////////////////////////////////////////// |
||
| 650 | ///////////////////////////// ACT UPON ///////////////////////////// |
||
| 651 | //////////////////////////////////////////////////////////////////// |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Iterate over an array and execute a callback for each loop. |
||
| 655 | * |
||
| 656 | * @param \Closure $closure |
||
| 657 | * |
||
| 658 | * @return Arrayy |
||
| 659 | */ |
||
| 660 | 2 | View Code Duplication | public function at(\Closure $closure) |
| 670 | |||
| 671 | //////////////////////////////////////////////////////////////////// |
||
| 672 | ////////////////////////////// ALTER /////////////////////////////// |
||
| 673 | //////////////////////////////////////////////////////////////////// |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Merge the new $array into the current array. |
||
| 677 | * |
||
| 678 | * - replace duplicate assoc-keys from the current array with the key,values from the new $array |
||
| 679 | * - create new indexes |
||
| 680 | * |
||
| 681 | * @param array $array |
||
| 682 | * |
||
| 683 | * @return Arrayy |
||
| 684 | */ |
||
| 685 | 8 | public function mergeAppendNewIndex(array $array = array()) |
|
| 689 | |||
| 690 | /** |
||
| 691 | * Merge the current array into the new $array. |
||
| 692 | * |
||
| 693 | * - replace duplicate assoc-keys from new $array with the key,values from the current array |
||
| 694 | * - create new indexes |
||
| 695 | * |
||
| 696 | * @param array $array |
||
| 697 | * |
||
| 698 | * @return Arrayy |
||
| 699 | */ |
||
| 700 | 8 | public function mergePrependNewIndex(array $array = array()) |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Merge the new $array into the current array. |
||
| 707 | * |
||
| 708 | * - keep key,value from the current array, also if the index is in the new $array |
||
| 709 | * |
||
| 710 | * @param array $array |
||
| 711 | * |
||
| 712 | * @return Arrayy |
||
| 713 | */ |
||
| 714 | 17 | public function mergeAppendKeepIndex(array $array = array()) |
|
| 718 | |||
| 719 | /** |
||
| 720 | * Merge the the current array into the $array. |
||
| 721 | * |
||
| 722 | * - use key,value from the new $array, also if the index is in the current array |
||
| 723 | * |
||
| 724 | * @param array $array |
||
| 725 | * |
||
| 726 | * @return Arrayy |
||
| 727 | */ |
||
| 728 | 8 | public function mergePrependKeepIndex(array $array = array()) |
|
| 732 | |||
| 733 | /** |
||
| 734 | * Return values that are only in the current array. |
||
| 735 | * |
||
| 736 | * @param array $array |
||
| 737 | * |
||
| 738 | * @return Arrayy |
||
| 739 | */ |
||
| 740 | 8 | public function diff(array $array = array()) |
|
| 744 | |||
| 745 | /** |
||
| 746 | * Return values that are only in the new $array. |
||
| 747 | * |
||
| 748 | * @param array $array |
||
| 749 | * |
||
| 750 | * @return Arrayy |
||
| 751 | */ |
||
| 752 | 8 | public function diffReverse(array $array = array()) |
|
| 756 | |||
| 757 | /** |
||
| 758 | * Replace the first matched value in an array. |
||
| 759 | * |
||
| 760 | * @param mixed $search |
||
| 761 | * @param mixed $replacement |
||
| 762 | * |
||
| 763 | * @return Arrayy |
||
| 764 | */ |
||
| 765 | 3 | public function replaceOneValue($search, $replacement = '') |
|
| 776 | |||
| 777 | /** |
||
| 778 | * Replace values in the current array. |
||
| 779 | * |
||
| 780 | * @param string $search The string to replace. |
||
| 781 | * @param string $replacement What to replace it with. |
||
| 782 | * |
||
| 783 | * @return Arrayy |
||
| 784 | */ |
||
| 785 | 1 | public function replaceValues($search, $replacement = '') |
|
| 795 | |||
| 796 | /** |
||
| 797 | * Replace the keys in an array with another set. |
||
| 798 | * |
||
| 799 | * @param array $keys An array of keys matching the array's size |
||
| 800 | * |
||
| 801 | * @return Arrayy |
||
| 802 | */ |
||
| 803 | 1 | public function replaceKeys(array $keys) |
|
| 809 | |||
| 810 | /** |
||
| 811 | * Iterate over the current array and modify the array's value. |
||
| 812 | * |
||
| 813 | * @param \Closure $closure |
||
| 814 | * |
||
| 815 | * @return array |
||
| 816 | */ |
||
| 817 | 22 | public function each(\Closure $closure) |
|
| 827 | |||
| 828 | /** |
||
| 829 | * Shuffle the current array. |
||
| 830 | * |
||
| 831 | * @return Arrayy |
||
| 832 | */ |
||
| 833 | 1 | public function shuffle() |
|
| 841 | |||
| 842 | |||
| 843 | /** |
||
| 844 | * Split an array in the given amount of pieces. |
||
| 845 | * |
||
| 846 | * @param int $numberOfPieces |
||
| 847 | * @param bool $keepKeys |
||
| 848 | * |
||
| 849 | * @return array |
||
| 850 | */ |
||
| 851 | 1 | public function split($numberOfPieces = 2, $keepKeys = false) |
|
| 861 | |||
| 862 | /** |
||
| 863 | * Implodes an array. |
||
| 864 | * |
||
| 865 | * @param string $with What to implode it with |
||
| 866 | * |
||
| 867 | * @return string |
||
| 868 | */ |
||
| 869 | 22 | public function implode($with = '') |
|
| 873 | |||
| 874 | /** |
||
| 875 | * Returns the values from a single column of the input array, identified by |
||
| 876 | * the $columnKey, can be used to extract data-columns from multi-arrays. |
||
| 877 | * |
||
| 878 | * Info: Optionally, you may provide an $indexKey to index the values in the returned |
||
| 879 | * array by the values from the $indexKey column in the input array. |
||
| 880 | * |
||
| 881 | * @param mixed $columnKey |
||
| 882 | * @param mixed $indexKey |
||
| 883 | * |
||
| 884 | * @return Arrayy |
||
| 885 | */ |
||
| 886 | 1 | public function getColumn($columnKey = null, $indexKey = null) |
|
| 890 | |||
| 891 | /** |
||
| 892 | * Find all items in an array that pass the truth test. |
||
| 893 | * |
||
| 894 | * @param \Closure|null $closure |
||
| 895 | * |
||
| 896 | * @return Arrayy |
||
| 897 | */ |
||
| 898 | 8 | public function filter($closure = null) |
|
| 908 | |||
| 909 | /** |
||
| 910 | * Invoke a function on all of an array's values. |
||
| 911 | * |
||
| 912 | * @param mixed $callable |
||
| 913 | * @param array $arguments |
||
| 914 | * |
||
| 915 | * @return Arrayy |
||
| 916 | */ |
||
| 917 | 1 | public function invoke($callable, $arguments = array()) |
|
| 933 | |||
| 934 | /** |
||
| 935 | * Return all items that fail the truth test. |
||
| 936 | * |
||
| 937 | * @param \Closure $closure |
||
| 938 | * |
||
| 939 | * @return Arrayy |
||
| 940 | */ |
||
| 941 | 1 | View Code Duplication | public function reject(\Closure $closure) |
| 953 | |||
| 954 | /** |
||
| 955 | * Remove the first value from the current array. |
||
| 956 | * |
||
| 957 | * @return Arrayy |
||
| 958 | */ |
||
| 959 | 8 | public function removeFirst() |
|
| 965 | |||
| 966 | /** |
||
| 967 | * Remove the last value from the current array. |
||
| 968 | * |
||
| 969 | * @return Arrayy |
||
| 970 | */ |
||
| 971 | 7 | public function removeLast() |
|
| 977 | |||
| 978 | /** |
||
| 979 | * Removes a particular value from an array (numeric or associative). |
||
| 980 | * |
||
| 981 | * @param mixed $value |
||
| 982 | * |
||
| 983 | * @return Arrayy |
||
| 984 | */ |
||
| 985 | 7 | public function removeValue($value) |
|
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Prepend a value to an array. |
||
| 1006 | * |
||
| 1007 | * @param mixed $value |
||
| 1008 | * |
||
| 1009 | * @return Arrayy |
||
| 1010 | */ |
||
| 1011 | 7 | public function prepend($value) |
|
| 1017 | |||
| 1018 | /** |
||
| 1019 | * Append a value to an array. |
||
| 1020 | * |
||
| 1021 | * @param mixed $value |
||
| 1022 | * |
||
| 1023 | * @return Arrayy |
||
| 1024 | */ |
||
| 1025 | 8 | public function append($value) |
|
| 1031 | |||
| 1032 | /** |
||
| 1033 | * Return the array in the reverse order. |
||
| 1034 | * |
||
| 1035 | * @return Arrayy |
||
| 1036 | */ |
||
| 1037 | 7 | public function reverse() |
|
| 1043 | |||
| 1044 | /** |
||
| 1045 | * Custom sort by value via "usort" |
||
| 1046 | * |
||
| 1047 | * @link http://php.net/manual/en/function.usort.php |
||
| 1048 | * |
||
| 1049 | * @param callable $func |
||
| 1050 | * |
||
| 1051 | * @return Arrayy |
||
| 1052 | */ |
||
| 1053 | public function customSortValues(callable $func) |
||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * Custom sort by index via "uksort" |
||
| 1062 | * |
||
| 1063 | * @link http://php.net/manual/en/function.uksort.php |
||
| 1064 | * |
||
| 1065 | * @param callable $func |
||
| 1066 | * |
||
| 1067 | * @return Arrayy |
||
| 1068 | */ |
||
| 1069 | public function customSortKeys(callable $func) |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * sorting keys |
||
| 1078 | * |
||
| 1079 | * @param array $elements |
||
| 1080 | * @param int $direction |
||
| 1081 | * @param int $strategy |
||
| 1082 | */ |
||
| 1083 | 10 | protected function sorterKeys(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
| 1098 | |||
| 1099 | /** |
||
| 1100 | * Sort the current array by key. |
||
| 1101 | * |
||
| 1102 | * @link http://php.net/manual/en/function.ksort.php |
||
| 1103 | * @link http://php.net/manual/en/function.krsort.php |
||
| 1104 | * |
||
| 1105 | * @param int|string $direction use SORT_ASC or SORT_DESC |
||
| 1106 | * @param int $strategy use e.g.: SORT_REGULAR or SORT_NATURAL |
||
| 1107 | * |
||
| 1108 | * @return Arrayy |
||
| 1109 | */ |
||
| 1110 | 10 | public function sortKeys($direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
| 1116 | |||
| 1117 | /** |
||
| 1118 | * Sort the current array by value. |
||
| 1119 | * |
||
| 1120 | * @param int $direction use SORT_ASC or SORT_DESC |
||
| 1121 | * @param int $strategy use e.g.: SORT_REGULAR or SORT_NATURAL |
||
| 1122 | * |
||
| 1123 | * @return Arrayy |
||
| 1124 | */ |
||
| 1125 | 1 | public function sortValueKeepIndex($direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
| 1129 | |||
| 1130 | /** |
||
| 1131 | * Sort the current array by value. |
||
| 1132 | * |
||
| 1133 | * @param int $direction use SORT_ASC or SORT_DESC |
||
| 1134 | * @param int $strategy use e.g.: SORT_REGULAR or SORT_NATURAL |
||
| 1135 | * |
||
| 1136 | * @return Arrayy |
||
| 1137 | */ |
||
| 1138 | 1 | public function sortValueNewIndex($direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
| 1142 | |||
| 1143 | /** |
||
| 1144 | * Get correct PHP constant for direction. |
||
| 1145 | * |
||
| 1146 | * @param int|string $direction |
||
| 1147 | * |
||
| 1148 | * @return int |
||
| 1149 | */ |
||
| 1150 | 14 | protected function getDirection($direction) |
|
| 1172 | |||
| 1173 | /** |
||
| 1174 | * Sort a array by value, by a closure or by a property. |
||
| 1175 | * |
||
| 1176 | * - If the sorter is null, the array is sorted naturally. |
||
| 1177 | * - Associative (string) keys will be maintained, but numeric keys will be re-indexed. |
||
| 1178 | * |
||
| 1179 | * @param null $sorter |
||
| 1180 | * @param string|int $direction |
||
| 1181 | * @param int $strategy |
||
| 1182 | * |
||
| 1183 | * @return Arrayy |
||
| 1184 | */ |
||
| 1185 | 1 | public function sorter($sorter = null, $direction = SORT_ASC, $strategy = SORT_REGULAR) |
|
| 1209 | |||
| 1210 | /** |
||
| 1211 | * @param array &$elements |
||
| 1212 | * @param int|string $direction |
||
| 1213 | * @param int $strategy |
||
| 1214 | * @param bool $keepKeys |
||
| 1215 | */ |
||
| 1216 | 3 | protected function sorting(array &$elements, $direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false) |
|
| 1243 | |||
| 1244 | /** |
||
| 1245 | * Sort the current array and optional you can keep the keys. |
||
| 1246 | * |
||
| 1247 | * @param string|int $direction use SORT_ASC or SORT_DESC |
||
| 1248 | * @param int|string $strategy |
||
| 1249 | * @param bool $keepKeys |
||
| 1250 | * |
||
| 1251 | * @return Arrayy |
||
| 1252 | */ |
||
| 1253 | 3 | public function sort($direction = SORT_ASC, $strategy = SORT_REGULAR, $keepKeys = false) |
|
| 1259 | |||
| 1260 | /** |
||
| 1261 | * Exchanges all keys with their associated values in an array. |
||
| 1262 | * |
||
| 1263 | * @return Arrayy |
||
| 1264 | */ |
||
| 1265 | 1 | public function flip() |
|
| 1271 | |||
| 1272 | /** |
||
| 1273 | * Reduce the current array via callable e.g. anonymous-function. |
||
| 1274 | * |
||
| 1275 | * @param mixed $predicate |
||
| 1276 | * @param array $init |
||
| 1277 | * |
||
| 1278 | * @return Arrayy |
||
| 1279 | */ |
||
| 1280 | 2 | public function reduce($predicate, array $init = array()) |
|
| 1286 | |||
| 1287 | /** |
||
| 1288 | * Return a duplicate free copy of the current array. |
||
| 1289 | * |
||
| 1290 | * @return Arrayy |
||
| 1291 | */ |
||
| 1292 | 7 | public function unique() |
|
| 1308 | |||
| 1309 | /** |
||
| 1310 | * Convert the current array to JSON. |
||
| 1311 | * |
||
| 1312 | * @param null $options e.g. JSON_PRETTY_PRINT |
||
| 1313 | * |
||
| 1314 | * @return string |
||
| 1315 | */ |
||
| 1316 | 1 | public function toJson($options = null) |
|
| 1320 | } |
||
| 1321 |
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.