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 | 341 | public function __construct($array = array()) |
|
| 22 | { |
||
| 23 | 341 | if (!$array) { |
|
|
|
|||
| 24 | 71 | $array = array(); |
|
| 25 | } |
||
| 26 | |||
| 27 | if ( |
||
| 28 | 341 | is_string($array) |
|
| 29 | || |
||
| 30 | 341 | (is_object($array) && method_exists($array, '__toString')) |
|
| 31 | ) { |
||
| 32 | 1 | $array = (array)$array; |
|
| 33 | } |
||
| 34 | |||
| 35 | 341 | if (!is_array($array)) { |
|
| 36 | 2 | throw new \InvalidArgumentException( |
|
| 37 | 2 | 'Passed value must be a array' |
|
| 38 | ); |
||
| 39 | } |
||
| 40 | |||
| 41 | 339 | $this->array = $array; |
|
| 42 | 339 | } |
|
| 43 | |||
| 44 | /** |
||
| 45 | * magic to string |
||
| 46 | * |
||
| 47 | * @return string |
||
| 48 | */ |
||
| 49 | 13 | 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 | public function offsetSet($offset, $value) |
||
| 107 | { |
||
| 108 | if (null === $offset) { |
||
| 109 | $this->array[] = $value; |
||
| 110 | } else { |
||
| 111 | $this->array[$offset] = $value; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Whether or not an offset exists |
||
| 117 | * |
||
| 118 | * @param mixed $offset |
||
| 119 | * |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | 5 | public function offsetExists($offset) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Unsets an offset |
||
| 129 | * |
||
| 130 | * @param mixed $offset |
||
| 131 | */ |
||
| 132 | public function offsetUnset($offset) |
||
| 133 | { |
||
| 134 | if ($this->offsetExists($offset)) { |
||
| 135 | unset($this->array[$offset]); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns the value at specified offset |
||
| 141 | * |
||
| 142 | * @param mixed $offset |
||
| 143 | * |
||
| 144 | * @return null |
||
| 145 | */ |
||
| 146 | 5 | 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 | 1 | 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 | 207 | public function getArray() |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Creates a Arrayy object |
||
| 194 | * |
||
| 195 | * @param array $array |
||
| 196 | * |
||
| 197 | * @return self |
||
| 198 | */ |
||
| 199 | 249 | 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 self |
||
| 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 self::create((array)$return); |
|
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Search for the index of the current array via $value. |
||
| 233 | * |
||
| 234 | * @param mixed $value |
||
| 235 | * |
||
| 236 | * @return self |
||
| 237 | */ |
||
| 238 | 7 | public function searchIndex($value) |
|
| 239 | { |
||
| 240 | 7 | $key = array_search($value, $this->array, true); |
|
| 241 | |||
| 242 | 7 | if ($key === false) { |
|
| 243 | 2 | $return = array(); |
|
| 244 | } else { |
||
| 245 | 5 | $return = array($key); |
|
| 246 | } |
||
| 247 | |||
| 248 | 7 | return self::create((array)$return); |
|
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Check if all items in an array match a truth test. |
||
| 253 | * |
||
| 254 | * @param \Closure $closure |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | 8 | View Code Duplication | public function matches(\Closure $closure) |
| 271 | |||
| 272 | /** |
||
| 273 | * Check if any item in an array matches a truth test. |
||
| 274 | * |
||
| 275 | * @param \Closure $closure |
||
| 276 | * |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | 8 | View Code Duplication | public function matchesAny(\Closure $closure) |
| 292 | |||
| 293 | /** |
||
| 294 | * Check if an item is in an array. |
||
| 295 | * |
||
| 296 | * @param mixed $value |
||
| 297 | * |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | 9 | public function contains($value) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Returns the average value of an array. |
||
| 307 | * |
||
| 308 | * @param int $decimals The number of decimals to return |
||
| 309 | * |
||
| 310 | * @return int The average value |
||
| 311 | */ |
||
| 312 | 10 | public function average($decimals = null) |
|
| 313 | { |
||
| 314 | 10 | $count = $this->count(); |
|
| 315 | |||
| 316 | 10 | if (!$count) { |
|
| 317 | 2 | return 0; |
|
| 318 | } |
||
| 319 | |||
| 320 | 8 | if (!is_int($decimals)) { |
|
| 321 | 3 | $decimals = null; |
|
| 322 | } |
||
| 323 | |||
| 324 | 8 | return round(array_sum($this->array) / $count, $decimals); |
|
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Count the values from the current array. |
||
| 329 | * |
||
| 330 | * INFO: only a alias for "$arrayy->size()" |
||
| 331 | * |
||
| 332 | * @return int |
||
| 333 | */ |
||
| 334 | public function length() |
||
| 335 | { |
||
| 336 | return $this->size(); |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Count the values from the current array. |
||
| 341 | * |
||
| 342 | * INFO: only a alias for "$arrayy->size()" |
||
| 343 | * |
||
| 344 | * @return int |
||
| 345 | */ |
||
| 346 | 41 | public function count() |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Get the size of an array. |
||
| 353 | * |
||
| 354 | * @return int |
||
| 355 | */ |
||
| 356 | 41 | public function size() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Get the max value from an array. |
||
| 363 | * |
||
| 364 | * @return mixed |
||
| 365 | */ |
||
| 366 | 10 | public function max() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Get the min value from an array. |
||
| 377 | * |
||
| 378 | * @return mixed |
||
| 379 | */ |
||
| 380 | 10 | public function min() |
|
| 388 | |||
| 389 | //////////////////////////////////////////////////////////////////// |
||
| 390 | //////////////////////////// FETCH FROM //////////////////////////// |
||
| 391 | //////////////////////////////////////////////////////////////////// |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Find the first item in an array that passes the truth test, |
||
| 395 | * otherwise return false |
||
| 396 | * |
||
| 397 | * @param \Closure $closure |
||
| 398 | * |
||
| 399 | * @return mixed|false false if we couldn't find the value |
||
| 400 | */ |
||
| 401 | 7 | public function find(\Closure $closure) |
|
| 402 | { |
||
| 403 | 7 | foreach ($this->array as $key => $value) { |
|
| 404 | 5 | if ($closure($value, $key)) { |
|
| 405 | 5 | return $value; |
|
| 406 | } |
||
| 407 | } |
||
| 408 | |||
| 409 | 3 | return false; |
|
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Clean all falsy values from an array. |
||
| 414 | * |
||
| 415 | * @return self |
||
| 416 | */ |
||
| 417 | 7 | public function clean() |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Get a random string from an array. |
||
| 428 | * |
||
| 429 | * @param null $take |
||
| 430 | * |
||
| 431 | * @return self |
||
| 432 | */ |
||
| 433 | 5 | public function random($take = null) |
|
| 434 | { |
||
| 435 | 5 | if ($take !== null) { |
|
| 436 | return $this->array[array_rand($this->array)]; |
||
| 437 | } |
||
| 438 | |||
| 439 | 5 | shuffle($this->array); |
|
| 440 | |||
| 441 | 5 | return $this->first($take); |
|
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Return an array with all elements found in input array. |
||
| 446 | * |
||
| 447 | * @param array $search |
||
| 448 | * |
||
| 449 | * @return self |
||
| 450 | */ |
||
| 451 | 2 | public function intersection(array $search) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Return a boolean flag which indicates whether the two input arrays have any common elements. |
||
| 458 | * |
||
| 459 | * @param array $search |
||
| 460 | * |
||
| 461 | * @return bool |
||
| 462 | */ |
||
| 463 | 1 | public function intersects(array $search) |
|
| 467 | |||
| 468 | //////////////////////////////////////////////////////////////////// |
||
| 469 | ///////////////////////////// SLICERS ////////////////////////////// |
||
| 470 | //////////////////////////////////////////////////////////////////// |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get the first value from an array. |
||
| 474 | * |
||
| 475 | * @param int|null $take |
||
| 476 | * |
||
| 477 | * @return self |
||
| 478 | */ |
||
| 479 | 27 | public function first($take = null) |
|
| 480 | { |
||
| 481 | 27 | if ($take === null) { |
|
| 482 | 13 | $array = array_shift($this->array); |
|
| 483 | } else { |
||
| 484 | 14 | $array = array_splice($this->array, 0, $take, true); |
|
| 485 | } |
||
| 486 | |||
| 487 | 27 | return self::create((array)$array); |
|
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Get the last value from an array. |
||
| 492 | * |
||
| 493 | * @param int|null $take |
||
| 494 | * |
||
| 495 | * @return self |
||
| 496 | */ |
||
| 497 | 10 | public function last($take = null) |
|
| 498 | { |
||
| 499 | 10 | if ($take === null) { |
|
| 500 | 7 | $array = self::create((array)array_pop($this->array)); |
|
| 501 | } else { |
||
| 502 | 3 | $array = $this->rest(-$take); |
|
| 503 | } |
||
| 504 | |||
| 505 | 10 | return $array; |
|
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Get everything but the last..$to items. |
||
| 510 | * |
||
| 511 | * @param int $to |
||
| 512 | * |
||
| 513 | * @return self |
||
| 514 | */ |
||
| 515 | 10 | public function initial($to = 1) |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Get the last elements from index $from until the end of this array. |
||
| 524 | * |
||
| 525 | * @param int $from |
||
| 526 | * |
||
| 527 | * @return self |
||
| 528 | */ |
||
| 529 | 13 | public function rest($from = 1) |
|
| 533 | |||
| 534 | //////////////////////////////////////////////////////////////////// |
||
| 535 | ///////////////////////////// ACT UPON ///////////////////////////// |
||
| 536 | //////////////////////////////////////////////////////////////////// |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Iterate over an array and execute a callback for each loop. |
||
| 540 | * |
||
| 541 | * @param \Closure $closure |
||
| 542 | * |
||
| 543 | * @return Arrayy |
||
| 544 | */ |
||
| 545 | 1 | View Code Duplication | public function at(\Closure $closure) |
| 546 | { |
||
| 547 | 1 | $array = $this->array; |
|
| 548 | |||
| 549 | 1 | foreach ($array as $key => $value) { |
|
| 550 | 1 | $closure($value, $key); |
|
| 551 | } |
||
| 552 | |||
| 553 | 1 | return self::create($array); |
|
| 554 | } |
||
| 555 | |||
| 556 | //////////////////////////////////////////////////////////////////// |
||
| 557 | ////////////////////////////// ALTER /////////////////////////////// |
||
| 558 | //////////////////////////////////////////////////////////////////// |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Merge the new $array into the current array. |
||
| 562 | * |
||
| 563 | * - replace duplicate keys from the current array with the key,values from the new $array |
||
| 564 | * - create new indexes |
||
| 565 | * |
||
| 566 | * @param array $array |
||
| 567 | * |
||
| 568 | * @return self |
||
| 569 | */ |
||
| 570 | 8 | public function mergeAppendNewIndex(array $array = array()) |
|
| 574 | |||
| 575 | /** |
||
| 576 | * Merge the current array into the new $array. |
||
| 577 | * |
||
| 578 | * - replace duplicate keys from new $array with the key,values from the current array |
||
| 579 | * - create new indexes |
||
| 580 | * |
||
| 581 | * @param array $array |
||
| 582 | * |
||
| 583 | * @return self |
||
| 584 | */ |
||
| 585 | 8 | public function mergePrependNewIndex(array $array = array()) |
|
| 589 | |||
| 590 | /** |
||
| 591 | * Merge the new $array into the current array. |
||
| 592 | * |
||
| 593 | * - keep key,value from the current array, also if the index is in the new $array |
||
| 594 | * |
||
| 595 | * @param array $array |
||
| 596 | * |
||
| 597 | * @return self |
||
| 598 | */ |
||
| 599 | 8 | public function mergeAppendKeepIndex(array $array = array()) |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Merge the the current array into the $array. |
||
| 606 | * |
||
| 607 | * - use key,value from the new $array, also if the index is in the current array |
||
| 608 | * |
||
| 609 | * @param array $array |
||
| 610 | * |
||
| 611 | * @return self |
||
| 612 | */ |
||
| 613 | 8 | public function mergePrependKeepIndex(array $array = array()) |
|
| 617 | |||
| 618 | /** |
||
| 619 | * Return values that are only in the current array. |
||
| 620 | * |
||
| 621 | * @param array $array |
||
| 622 | * |
||
| 623 | * @return self |
||
| 624 | */ |
||
| 625 | 8 | public function diff(array $array = array()) |
|
| 629 | |||
| 630 | /** |
||
| 631 | * Return values that are only in the new $array. |
||
| 632 | * |
||
| 633 | * @param array $array |
||
| 634 | * |
||
| 635 | * @return self |
||
| 636 | */ |
||
| 637 | 8 | public function diffReverse(array $array = array()) |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Replace the first matched value in an array. |
||
| 644 | * |
||
| 645 | * @param string $search The string to replace |
||
| 646 | * @param string $replacement What to replace it with |
||
| 647 | * |
||
| 648 | * @return self |
||
| 649 | */ |
||
| 650 | 3 | public function replaceOneValue($search, $replacement = '') |
|
| 651 | { |
||
| 652 | 3 | $array = $this->array; |
|
| 653 | 3 | $key = array_search($search, $array, true); |
|
| 654 | |||
| 655 | 3 | if ($key !== false) { |
|
| 656 | 3 | $array[$key] = $replacement; |
|
| 657 | } |
||
| 658 | |||
| 659 | 3 | return self::create((array)$array); |
|
| 660 | } |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Replace values in an array. |
||
| 664 | * |
||
| 665 | * @param string $search The string to replace |
||
| 666 | * @param string $replacement What to replace it with |
||
| 667 | * |
||
| 668 | * @return self |
||
| 669 | */ |
||
| 670 | 1 | public function replaceValues($search, $replacement = '') |
|
| 680 | |||
| 681 | /** |
||
| 682 | * Replace the keys in an array with another set. |
||
| 683 | * |
||
| 684 | * @param array $keys An array of keys matching the array's size |
||
| 685 | * |
||
| 686 | * @return self |
||
| 687 | */ |
||
| 688 | 1 | public function replaceKeys(array $keys) |
|
| 694 | |||
| 695 | /** |
||
| 696 | * Iterate over an array and modify the array's value. |
||
| 697 | * |
||
| 698 | * @param \Closure $closure |
||
| 699 | * |
||
| 700 | * @return array |
||
| 701 | */ |
||
| 702 | 19 | View Code Duplication | public function each(\Closure $closure) |
| 703 | { |
||
| 704 | 19 | $array = $this->array; |
|
| 705 | |||
| 706 | 19 | foreach ($array as $key => &$value) { |
|
| 707 | 15 | $value = $closure($value, $key); |
|
| 708 | } |
||
| 709 | |||
| 710 | 19 | return $array; |
|
| 711 | } |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Shuffle an array. |
||
| 715 | * |
||
| 716 | * @return self |
||
| 717 | */ |
||
| 718 | 1 | public function shuffle() |
|
| 726 | |||
| 727 | /** |
||
| 728 | * Sort an array by key. |
||
| 729 | * |
||
| 730 | * @param string $direction |
||
| 731 | * |
||
| 732 | * @return self |
||
| 733 | */ |
||
| 734 | 8 | public function sortKeys($direction = 'ASC') |
|
| 735 | { |
||
| 736 | 8 | $array = $this->array; |
|
| 737 | 8 | $direction = strtolower($direction); |
|
| 738 | |||
| 739 | 8 | if ($direction === 'desc') { |
|
| 740 | 1 | $directionType = SORT_DESC; |
|
| 741 | } else { |
||
| 742 | 7 | $directionType = SORT_ASC; |
|
| 743 | } |
||
| 744 | |||
| 745 | 8 | if ($directionType === SORT_ASC) { |
|
| 746 | 7 | ksort($array); |
|
| 747 | } else { |
||
| 748 | 1 | krsort($array); |
|
| 749 | } |
||
| 750 | |||
| 751 | 8 | return self::create($array); |
|
| 752 | } |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Implodes an array. |
||
| 756 | * |
||
| 757 | * @param string $with What to implode it with |
||
| 758 | * |
||
| 759 | * @return string |
||
| 760 | */ |
||
| 761 | 21 | public function implode($with = '') |
|
| 765 | |||
| 766 | /** |
||
| 767 | * Find all items in an array that pass the truth test. |
||
| 768 | * |
||
| 769 | * @param \Closure $closure |
||
| 770 | * |
||
| 771 | * @return self |
||
| 772 | */ |
||
| 773 | 8 | public function filter($closure = null) |
|
| 774 | { |
||
| 775 | 8 | if (!$closure) { |
|
| 776 | return $this->clean(); |
||
| 777 | } |
||
| 778 | |||
| 779 | 8 | $array = array_filter($this->array, $closure); |
|
| 780 | |||
| 781 | 8 | return self::create($array); |
|
| 782 | } |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Invoke a function on all of an array's values. |
||
| 786 | * |
||
| 787 | * @param mixed $callable |
||
| 788 | * @param array $arguments |
||
| 789 | * |
||
| 790 | * @return self |
||
| 791 | */ |
||
| 792 | 1 | public function invoke($callable, $arguments = array()) |
|
| 793 | { |
||
| 794 | // If one argument given for each iteration, create an array for it. |
||
| 795 | 1 | if (!is_array($arguments)) { |
|
| 796 | 1 | $arguments = StaticArrayy::repeat($arguments, count($this->array))->getArray(); |
|
| 797 | } |
||
| 798 | |||
| 799 | // If the callable has arguments, pass them. |
||
| 800 | 1 | if ($arguments) { |
|
| 801 | 1 | $array = array_map($callable, $this->array, $arguments); |
|
| 802 | } else { |
||
| 803 | 1 | $array = array_map($callable, $this->array); |
|
| 804 | } |
||
| 805 | |||
| 806 | 1 | return self::create($array); |
|
| 807 | } |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Return all items that fail the truth test. |
||
| 811 | * |
||
| 812 | * @param \Closure $closure |
||
| 813 | * |
||
| 814 | * @return self |
||
| 815 | */ |
||
| 816 | 1 | View Code Duplication | public function reject(\Closure $closure) |
| 817 | { |
||
| 818 | 1 | $filtered = array(); |
|
| 819 | |||
| 820 | 1 | foreach ($this->array as $key => $value) { |
|
| 821 | 1 | if (!$closure($value, $key)) { |
|
| 822 | 1 | $filtered[$key] = $value; |
|
| 823 | } |
||
| 824 | } |
||
| 825 | |||
| 826 | 1 | return self::create($filtered); |
|
| 827 | } |
||
| 828 | |||
| 829 | /** |
||
| 830 | * Remove the first value from an array. |
||
| 831 | * |
||
| 832 | * @return self |
||
| 833 | */ |
||
| 834 | 8 | public function removeFirst() |
|
| 840 | |||
| 841 | /** |
||
| 842 | * Remove the last value from an array. |
||
| 843 | * |
||
| 844 | * @return self |
||
| 845 | */ |
||
| 846 | 7 | public function removeLast() |
|
| 852 | |||
| 853 | /** |
||
| 854 | * Removes a particular value from an array (numeric or associative). |
||
| 855 | * |
||
| 856 | * @param mixed $value |
||
| 857 | * |
||
| 858 | * @return self |
||
| 859 | */ |
||
| 860 | 7 | public function removeValue($value) |
|
| 861 | { |
||
| 862 | 7 | $isNumericArray = true; |
|
| 863 | 7 | foreach ($this->array as $key => $item) { |
|
| 864 | 6 | if ($item === $value) { |
|
| 865 | 6 | if (!is_int($key)) { |
|
| 866 | $isNumericArray = false; |
||
| 867 | } |
||
| 868 | 6 | unset($this->array[$key]); |
|
| 869 | } |
||
| 870 | } |
||
| 871 | |||
| 872 | 7 | if ($isNumericArray) { |
|
| 873 | 7 | $this->array = array_values($this->array); |
|
| 874 | } |
||
| 875 | |||
| 876 | 7 | return self::create($this->array); |
|
| 877 | } |
||
| 878 | |||
| 879 | /** |
||
| 880 | * Prepend a value to an array. |
||
| 881 | * |
||
| 882 | * @param mixed $value |
||
| 883 | * |
||
| 884 | * @return self |
||
| 885 | */ |
||
| 886 | 7 | public function prepend($value) |
|
| 892 | |||
| 893 | /** |
||
| 894 | * Append a value to an array. |
||
| 895 | * |
||
| 896 | * @param mixed $value |
||
| 897 | * |
||
| 898 | * @return self |
||
| 899 | */ |
||
| 900 | 8 | public function append($value) |
|
| 906 | |||
| 907 | /** |
||
| 908 | * Return the array in the reverse order. |
||
| 909 | * |
||
| 910 | * @return self |
||
| 911 | */ |
||
| 912 | 7 | public function reverse() |
|
| 918 | |||
| 919 | /** |
||
| 920 | * duplicate free copy of an array |
||
| 921 | * |
||
| 922 | * @return self |
||
| 923 | */ |
||
| 924 | 7 | public function unique() |
|
| 925 | { |
||
| 926 | 7 | $this->array = array_reduce( |
|
| 927 | 7 | $this->array, |
|
| 928 | 7 | function ($resultArray, $value) { |
|
| 929 | 6 | if (in_array($value, $resultArray, true) === false) { |
|
| 930 | 6 | $resultArray[] = $value; |
|
| 931 | } |
||
| 932 | |||
| 933 | 6 | return $resultArray; |
|
| 934 | 7 | }, |
|
| 935 | 7 | array() |
|
| 936 | ); |
||
| 937 | |||
| 938 | 7 | return self::create($this->array); |
|
| 939 | } |
||
| 940 | } |
||
| 941 |
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.