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 | 394 | public function __construct($array = array()) |
|
| 22 | { |
||
| 23 | 394 | if (!$array) { |
|
|
|
|||
| 24 | 80 | $array = array(); |
|
| 25 | } |
||
| 26 | |||
| 27 | if ( |
||
| 28 | 394 | is_string($array) |
|
| 29 | || |
||
| 30 | 394 | (is_object($array) && method_exists($array, '__toString')) |
|
| 31 | ) { |
||
| 32 | 1 | $array = (array)$array; |
|
| 33 | } |
||
| 34 | |||
| 35 | 394 | if (!is_array($array)) { |
|
| 36 | 2 | throw new \InvalidArgumentException( |
|
| 37 | 2 | 'Passed value must be a array' |
|
| 38 | ); |
||
| 39 | } |
||
| 40 | |||
| 41 | 392 | $this->array = $array; |
|
| 42 | 392 | } |
|
| 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) |
||
| 62 | { |
||
| 63 | return $this->array[$key]; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Assigns a value to the specified data |
||
| 68 | * |
||
| 69 | * @param $key |
||
| 70 | * @param $value |
||
| 71 | */ |
||
| 72 | public function __set($key, $value) |
||
| 73 | { |
||
| 74 | $this->array[$key] = $value; |
||
| 75 | } |
||
| 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) |
||
| 85 | { |
||
| 86 | return isset($this->array[$key]); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Unsets an data by key |
||
| 91 | * |
||
| 92 | * @param mixed $key |
||
| 93 | */ |
||
| 94 | public function __unset($key) |
||
| 95 | { |
||
| 96 | unset($this->array[$key]); |
||
| 97 | } |
||
| 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 | } |
||
| 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) |
||
| 170 | { |
||
| 171 | if ($key !== null) { |
||
| 172 | if (isset($this->array[$key])) { |
||
| 173 | return $this->array[$key]; |
||
| 174 | } else { |
||
| 175 | return false; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | return (array)$this->array; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * get the current array from the "Arrayy"-object |
||
| 184 | * |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | 224 | public function getArray() |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Creates a Arrayy object |
||
| 194 | * |
||
| 195 | * @param array $array |
||
| 196 | * |
||
| 197 | * @return Arrayy |
||
| 198 | */ |
||
| 199 | 297 | 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 will return a empty Arrayy if the value wasn't found |
||
| 214 | */ |
||
| 215 | 7 | public function searchValue($index) |
|
| 216 | { |
||
| 217 | // init |
||
| 218 | 7 | $return = array(); |
|
| 219 | |||
| 220 | 7 | if (null !== $index) { |
|
| 221 | 7 | $keyExists = isset($this->array[$index]); |
|
| 222 | |||
| 223 | 7 | if ($keyExists !== false) { |
|
| 224 | 5 | $return = array($this->array[$index]); |
|
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | 7 | return static::create((array)$return); |
|
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Search for the first index of the current array via $value. |
||
| 233 | * |
||
| 234 | * @param mixed $value |
||
| 235 | * |
||
| 236 | * @return Arrayy will return a empty Arrayy if the index wasn't found |
||
| 237 | */ |
||
| 238 | 16 | public function searchIndex($value) |
|
| 239 | { |
||
| 240 | 16 | $key = array_search($value, $this->array, true); |
|
| 241 | |||
| 242 | 16 | if ($key === false) { |
|
| 243 | 9 | $return = array(); |
|
| 244 | } else { |
||
| 245 | 7 | $return = array($key); |
|
| 246 | } |
||
| 247 | |||
| 248 | 16 | return static::create((array)$return); |
|
| 249 | } |
||
| 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) |
|
| 337 | { |
||
| 338 | 10 | $count = $this->count(); |
|
| 339 | |||
| 340 | 10 | if (!$count) { |
|
| 341 | 2 | return 0; |
|
| 342 | } |
||
| 343 | |||
| 344 | 8 | if (!is_int($decimals)) { |
|
| 345 | 3 | $decimals = null; |
|
| 346 | } |
||
| 347 | |||
| 348 | 8 | return round(array_sum($this->array) / $count, $decimals); |
|
| 349 | } |
||
| 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 | 59 | public function count() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Get the size of an array. |
||
| 377 | * |
||
| 378 | * @return int |
||
| 379 | */ |
||
| 380 | 59 | 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) |
|
| 426 | { |
||
| 427 | 7 | foreach ($this->array as $key => $value) { |
|
| 428 | 5 | if ($closure($value, $key)) { |
|
| 429 | 5 | return $value; |
|
| 430 | } |
||
| 431 | } |
||
| 432 | |||
| 433 | 3 | return false; |
|
| 434 | } |
||
| 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 | 18 | public function random($take = null) |
|
| 458 | { |
||
| 459 | 18 | if ($this->count() === 0) { |
|
| 460 | return self::create(array()); |
||
| 461 | } |
||
| 462 | |||
| 463 | 18 | if ($take === null) { |
|
| 464 | 12 | return static::create((array)$this->array[array_rand($this->array)]); |
|
| 465 | } |
||
| 466 | |||
| 467 | 8 | shuffle($this->array); |
|
| 468 | |||
| 469 | 8 | return $this->first($take); |
|
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get a random value from an array, with the ability to skew the results. |
||
| 474 | * |
||
| 475 | * Example: randomWeighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar. |
||
| 476 | * |
||
| 477 | * @param array $array |
||
| 478 | * @param null|int $take how many values you will take? |
||
| 479 | * |
||
| 480 | * @return Arrayy |
||
| 481 | */ |
||
| 482 | 9 | public function randomWeighted(array $array, $take = null) |
|
| 483 | { |
||
| 484 | 9 | $options = array(); |
|
| 485 | 9 | foreach ($array as $option => $weight) { |
|
| 486 | 9 | if ($this->searchIndex($option)->count() > 0) { |
|
| 487 | 9 | for ($i = 0; $i < $weight; ++$i) { |
|
| 488 | 1 | $options[] = $option; |
|
| 489 | } |
||
| 490 | } |
||
| 491 | } |
||
| 492 | |||
| 493 | 9 | return $this->mergeAppendKeepIndex($options)->random($take); |
|
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Return an array with all elements found in input array. |
||
| 498 | * |
||
| 499 | * @param array $search |
||
| 500 | * |
||
| 501 | * @return Arrayy |
||
| 502 | */ |
||
| 503 | 2 | public function intersection(array $search) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Return a boolean flag which indicates whether the two input arrays have any common elements. |
||
| 510 | * |
||
| 511 | * @param array $search |
||
| 512 | * |
||
| 513 | * @return bool |
||
| 514 | */ |
||
| 515 | 1 | public function intersects(array $search) |
|
| 519 | |||
| 520 | //////////////////////////////////////////////////////////////////// |
||
| 521 | ///////////////////////////// SLICERS ////////////////////////////// |
||
| 522 | //////////////////////////////////////////////////////////////////// |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Get the first value(s) from the current array. |
||
| 526 | * |
||
| 527 | * @param int|null $take how many values you will take? |
||
| 528 | * |
||
| 529 | * @return Arrayy |
||
| 530 | */ |
||
| 531 | 33 | public function first($take = null) |
|
| 532 | { |
||
| 533 | 33 | if ($take === null) { |
|
| 534 | 8 | $array = array_shift($this->array); |
|
| 535 | } else { |
||
| 536 | 25 | $array = array_splice($this->array, 0, $take, true); |
|
| 537 | } |
||
| 538 | |||
| 539 | 33 | return static::create((array)$array); |
|
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Get the last value(s) from the current array. |
||
| 544 | * |
||
| 545 | * @param int|null $take |
||
| 546 | * |
||
| 547 | * @return Arrayy |
||
| 548 | */ |
||
| 549 | 11 | public function last($take = null) |
|
| 550 | { |
||
| 551 | 11 | if ($take === null) { |
|
| 552 | 7 | $array = static::create((array)array_pop($this->array)); |
|
| 553 | } else { |
||
| 554 | 4 | $array = $this->rest(-$take); |
|
| 555 | } |
||
| 556 | |||
| 557 | 11 | return $array; |
|
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Get everything but the last..$to items. |
||
| 562 | * |
||
| 563 | * @param int $to |
||
| 564 | * |
||
| 565 | * @return Arrayy |
||
| 566 | */ |
||
| 567 | 12 | public function initial($to = 1) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Get the last elements from index $from until the end of this array. |
||
| 576 | * |
||
| 577 | * @param int $from |
||
| 578 | * |
||
| 579 | * @return Arrayy |
||
| 580 | */ |
||
| 581 | 16 | public function rest($from = 1) |
|
| 585 | |||
| 586 | //////////////////////////////////////////////////////////////////// |
||
| 587 | ///////////////////////////// ACT UPON ///////////////////////////// |
||
| 588 | //////////////////////////////////////////////////////////////////// |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Iterate over an array and execute a callback for each loop. |
||
| 592 | * |
||
| 593 | * @param \Closure $closure |
||
| 594 | * |
||
| 595 | * @return Arrayy |
||
| 596 | */ |
||
| 597 | 1 | View Code Duplication | public function at(\Closure $closure) |
| 598 | { |
||
| 599 | 1 | $array = $this->array; |
|
| 600 | |||
| 601 | 1 | foreach ($array as $key => $value) { |
|
| 602 | 1 | $closure($value, $key); |
|
| 603 | } |
||
| 604 | |||
| 605 | 1 | return static::create($array); |
|
| 606 | } |
||
| 607 | |||
| 608 | //////////////////////////////////////////////////////////////////// |
||
| 609 | ////////////////////////////// ALTER /////////////////////////////// |
||
| 610 | //////////////////////////////////////////////////////////////////// |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Merge the new $array into the current array. |
||
| 614 | * |
||
| 615 | * - replace duplicate keys from the current array with the key,values from the new $array |
||
| 616 | * - create new indexes |
||
| 617 | * |
||
| 618 | * @param array $array |
||
| 619 | * |
||
| 620 | * @return Arrayy |
||
| 621 | */ |
||
| 622 | 8 | public function mergeAppendNewIndex(array $array = array()) |
|
| 626 | |||
| 627 | /** |
||
| 628 | * Merge the current array into the new $array. |
||
| 629 | * |
||
| 630 | * - replace duplicate keys from new $array with the key,values from the current array |
||
| 631 | * - create new indexes |
||
| 632 | * |
||
| 633 | * @param array $array |
||
| 634 | * |
||
| 635 | * @return Arrayy |
||
| 636 | */ |
||
| 637 | 8 | public function mergePrependNewIndex(array $array = array()) |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Merge the new $array into the current array. |
||
| 644 | * |
||
| 645 | * - keep key,value from the current array, also if the index is in the new $array |
||
| 646 | * |
||
| 647 | * @param array $array |
||
| 648 | * |
||
| 649 | * @return Arrayy |
||
| 650 | */ |
||
| 651 | 17 | public function mergeAppendKeepIndex(array $array = array()) |
|
| 655 | |||
| 656 | /** |
||
| 657 | * Merge the the current array into the $array. |
||
| 658 | * |
||
| 659 | * - use key,value from the new $array, also if the index is in the current array |
||
| 660 | * |
||
| 661 | * @param array $array |
||
| 662 | * |
||
| 663 | * @return Arrayy |
||
| 664 | */ |
||
| 665 | 8 | public function mergePrependKeepIndex(array $array = array()) |
|
| 669 | |||
| 670 | /** |
||
| 671 | * Return values that are only in the current array. |
||
| 672 | * |
||
| 673 | * @param array $array |
||
| 674 | * |
||
| 675 | * @return Arrayy |
||
| 676 | */ |
||
| 677 | 8 | public function diff(array $array = array()) |
|
| 681 | |||
| 682 | /** |
||
| 683 | * Return values that are only in the new $array. |
||
| 684 | * |
||
| 685 | * @param array $array |
||
| 686 | * |
||
| 687 | * @return Arrayy |
||
| 688 | */ |
||
| 689 | 8 | public function diffReverse(array $array = array()) |
|
| 693 | |||
| 694 | /** |
||
| 695 | * Replace the first matched value in an array. |
||
| 696 | * |
||
| 697 | * @param string $search The string to replace |
||
| 698 | * @param string $replacement What to replace it with |
||
| 699 | * |
||
| 700 | * @return Arrayy |
||
| 701 | */ |
||
| 702 | 3 | public function replaceOneValue($search, $replacement = '') |
|
| 703 | { |
||
| 704 | 3 | $array = $this->array; |
|
| 705 | 3 | $key = array_search($search, $array, true); |
|
| 706 | |||
| 707 | 3 | if ($key !== false) { |
|
| 708 | 3 | $array[$key] = $replacement; |
|
| 709 | } |
||
| 710 | |||
| 711 | 3 | return static::create((array)$array); |
|
| 712 | } |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Replace values in an array. |
||
| 716 | * |
||
| 717 | * @param string $search The string to replace |
||
| 718 | * @param string $replacement What to replace it with |
||
| 719 | * |
||
| 720 | * @return Arrayy |
||
| 721 | */ |
||
| 722 | 1 | public function replaceValues($search, $replacement = '') |
|
| 732 | |||
| 733 | /** |
||
| 734 | * Replace the keys in an array with another set. |
||
| 735 | * |
||
| 736 | * @param array $keys An array of keys matching the array's size |
||
| 737 | * |
||
| 738 | * @return Arrayy |
||
| 739 | */ |
||
| 740 | 1 | public function replaceKeys(array $keys) |
|
| 746 | |||
| 747 | /** |
||
| 748 | * Iterate over an array and modify the array's value. |
||
| 749 | * |
||
| 750 | * @param \Closure $closure |
||
| 751 | * |
||
| 752 | * @return array |
||
| 753 | */ |
||
| 754 | 21 | public function each(\Closure $closure) |
|
| 755 | { |
||
| 756 | 21 | $array = $this->array; |
|
| 757 | |||
| 758 | 21 | foreach ($array as $key => &$value) { |
|
| 759 | 17 | $value = $closure($value, $key); |
|
| 760 | } |
||
| 761 | |||
| 762 | 21 | return $array; |
|
| 763 | } |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Shuffle an array. |
||
| 767 | * |
||
| 768 | * @return Arrayy |
||
| 769 | */ |
||
| 770 | 1 | public function shuffle() |
|
| 778 | |||
| 779 | |||
| 780 | /** |
||
| 781 | * Split an array in the given amount of pieces. |
||
| 782 | * |
||
| 783 | * @param int $numberOfPieces |
||
| 784 | * @param bool $preserveKeys |
||
| 785 | * |
||
| 786 | * @return array |
||
| 787 | */ |
||
| 788 | 1 | public function split($numberOfPieces = 2, $preserveKeys = false) |
|
| 798 | |||
| 799 | /** |
||
| 800 | * Sort an array by key. |
||
| 801 | * |
||
| 802 | * @param string $direction |
||
| 803 | * |
||
| 804 | * @return Arrayy |
||
| 805 | */ |
||
| 806 | 8 | public function sortKeys($direction = 'ASC') |
|
| 807 | { |
||
| 808 | 8 | $array = $this->array; |
|
| 809 | 8 | $direction = strtolower($direction); |
|
| 810 | |||
| 811 | 8 | if ($direction === 'desc') { |
|
| 812 | 1 | $directionType = SORT_DESC; |
|
| 813 | } else { |
||
| 814 | 7 | $directionType = SORT_ASC; |
|
| 815 | } |
||
| 816 | |||
| 817 | 8 | if ($directionType === SORT_ASC) { |
|
| 818 | 7 | ksort($array); |
|
| 819 | } else { |
||
| 820 | 1 | krsort($array); |
|
| 821 | } |
||
| 822 | |||
| 823 | 8 | return static::create($array); |
|
| 824 | } |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Implodes an array. |
||
| 828 | * |
||
| 829 | * @param string $with What to implode it with |
||
| 830 | * |
||
| 831 | * @return string |
||
| 832 | */ |
||
| 833 | 22 | public function implode($with = '') |
|
| 837 | |||
| 838 | /** |
||
| 839 | * Returns the values from a single column of the input array, identified by |
||
| 840 | * the $columnKey, can be used to extract data-columns from multi-arrays. |
||
| 841 | * |
||
| 842 | * Info: Optionally, you may provide an $indexKey to index the values in the returned |
||
| 843 | * array by the values from the $indexKey column in the input array. |
||
| 844 | * |
||
| 845 | * @param mixed $columnKey |
||
| 846 | * @param mixed $indexKey |
||
| 847 | * |
||
| 848 | * @return Arrayy |
||
| 849 | */ |
||
| 850 | 1 | public function getColumn($columnKey = null, $indexKey = null) |
|
| 854 | |||
| 855 | /** |
||
| 856 | * Find all items in an array that pass the truth test. |
||
| 857 | * |
||
| 858 | * @param \Closure $closure |
||
| 859 | * |
||
| 860 | * @return Arrayy |
||
| 861 | */ |
||
| 862 | 8 | public function filter($closure = null) |
|
| 872 | |||
| 873 | /** |
||
| 874 | * Invoke a function on all of an array's values. |
||
| 875 | * |
||
| 876 | * @param mixed $callable |
||
| 877 | * @param array $arguments |
||
| 878 | * |
||
| 879 | * @return Arrayy |
||
| 880 | */ |
||
| 881 | 1 | public function invoke($callable, $arguments = array()) |
|
| 897 | |||
| 898 | /** |
||
| 899 | * Return all items that fail the truth test. |
||
| 900 | * |
||
| 901 | * @param \Closure $closure |
||
| 902 | * |
||
| 903 | * @return Arrayy |
||
| 904 | */ |
||
| 905 | 1 | View Code Duplication | public function reject(\Closure $closure) |
| 917 | |||
| 918 | /** |
||
| 919 | * Remove the first value from an array. |
||
| 920 | * |
||
| 921 | * @return Arrayy |
||
| 922 | */ |
||
| 923 | 8 | public function removeFirst() |
|
| 929 | |||
| 930 | /** |
||
| 931 | * Remove the last value from an array. |
||
| 932 | * |
||
| 933 | * @return Arrayy |
||
| 934 | */ |
||
| 935 | 7 | public function removeLast() |
|
| 941 | |||
| 942 | /** |
||
| 943 | * Removes a particular value from an array (numeric or associative). |
||
| 944 | * |
||
| 945 | * @param mixed $value |
||
| 946 | * |
||
| 947 | * @return Arrayy |
||
| 948 | */ |
||
| 949 | 7 | public function removeValue($value) |
|
| 967 | |||
| 968 | /** |
||
| 969 | * Prepend a value to an array. |
||
| 970 | * |
||
| 971 | * @param mixed $value |
||
| 972 | * |
||
| 973 | * @return Arrayy |
||
| 974 | */ |
||
| 975 | 7 | public function prepend($value) |
|
| 981 | |||
| 982 | /** |
||
| 983 | * Append a value to an array. |
||
| 984 | * |
||
| 985 | * @param mixed $value |
||
| 986 | * |
||
| 987 | * @return Arrayy |
||
| 988 | */ |
||
| 989 | 8 | public function append($value) |
|
| 995 | |||
| 996 | /** |
||
| 997 | * Return the array in the reverse order. |
||
| 998 | * |
||
| 999 | * @return Arrayy |
||
| 1000 | */ |
||
| 1001 | 7 | public function reverse() |
|
| 1007 | |||
| 1008 | /** |
||
| 1009 | * duplicate free copy of an array |
||
| 1010 | * |
||
| 1011 | * @return Arrayy |
||
| 1012 | */ |
||
| 1013 | 7 | public function unique() |
|
| 1029 | } |
||
| 1030 |
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.