Complex classes like CollectionTrait 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 CollectionTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | trait CollectionTrait |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Converts $collection to array. If there are multiple items with the same key, only the last will be preserved. |
||
| 11 | * |
||
| 12 | * @return array |
||
| 13 | */ |
||
| 14 | 63 | public function toArray() |
|
| 18 | |||
| 19 | /** |
||
| 20 | * Returns a lazy collection of items for which $function returned true. |
||
| 21 | * |
||
| 22 | * @param callable|null $function ($value, $key) |
||
| 23 | * @return Collection |
||
| 24 | */ |
||
| 25 | 2 | public function filter(callable $function = null) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Returns a lazy collection of distinct items. The comparison is the same as in in_array. |
||
| 32 | * |
||
| 33 | * @return Collection |
||
| 34 | */ |
||
| 35 | 1 | public function distinct() |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Returns a lazy collection with items from all $collections passed as argument appended together |
||
| 42 | * |
||
| 43 | * @param \Traversable[]|array ...$collections |
||
| 44 | * @return Collection |
||
| 45 | */ |
||
| 46 | 1 | public function concat(...$collections) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Returns collection where each item is changed to the output of executing $function on each key/item. |
||
| 53 | * |
||
| 54 | * @param callable $function |
||
| 55 | * @return Collection |
||
| 56 | */ |
||
| 57 | 4 | public function map(callable $function) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Reduces the collection to single value by iterating over the collection and calling $function while |
||
| 64 | * passing $startValue and current key/item as parameters. The output of $function is used as $startValue in |
||
| 65 | * next iteration. The output of $function on last element is the return value of this function. If |
||
| 66 | * $convertToCollection is true and the return value is a collection (array|Traversable) an instance of Collection |
||
| 67 | * is returned. |
||
| 68 | * |
||
| 69 | * @param callable $function ($tmpValue, $value, $key) |
||
| 70 | * @param mixed $startValue |
||
| 71 | * @param bool $convertToCollection |
||
| 72 | * @return mixed|Collection |
||
| 73 | */ |
||
| 74 | 2 | public function reduce(callable $function, $startValue, $convertToCollection = false) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Returns a lazy collection with one or multiple levels of nesting flattened. Removes all nesting when no value |
||
| 83 | * is passed. |
||
| 84 | * |
||
| 85 | * @param int $depth How many levels should be flatten, default (-1) is infinite. |
||
| 86 | * @return Collection |
||
| 87 | */ |
||
| 88 | 1 | public function flatten($depth = -1) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Returns a non-lazy collection sorted using $function($item1, $item2, $key1, $key2 ). $function should |
||
| 95 | * return true if first item is larger than the second and false otherwise. |
||
| 96 | * |
||
| 97 | * @param callable $function ($value1, $value2, $key1, $key2) |
||
| 98 | * @return Collection |
||
| 99 | */ |
||
| 100 | 2 | public function sort(callable $function) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Returns lazy collection items of which are part of the original collection from item number $from to item |
||
| 107 | * number $to. The items before $from are also iterated over, just not returned. |
||
| 108 | * |
||
| 109 | * @param int $from |
||
| 110 | * @param int $to If omitted, will slice until end |
||
| 111 | * @return Collection |
||
| 112 | */ |
||
| 113 | 1 | public function slice($from, $to = -1) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Returns collection which items are separated into groups indexed by the return value of $function. |
||
| 120 | * |
||
| 121 | * @param callable $function ($value, $key) |
||
| 122 | * @return Collection |
||
| 123 | */ |
||
| 124 | 1 | public function groupBy(callable $function) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Returns collection where items are separated into groups indexed by the value at given key. |
||
| 131 | * |
||
| 132 | * @param $key |
||
| 133 | * @return Collection |
||
| 134 | */ |
||
| 135 | 1 | public function groupByKey($key) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Returns a lazy collection in which $function is executed for each item. |
||
| 142 | * |
||
| 143 | * @param callable $function ($value, $key) |
||
| 144 | * @return Collection |
||
| 145 | */ |
||
| 146 | 1 | public function each(callable $function) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Returns the number of items in this collection. |
||
| 153 | * |
||
| 154 | * @return int |
||
| 155 | */ |
||
| 156 | 6 | public function size() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Returns value at the key $key. If multiple values have this key, return first. If no value has this key, throw |
||
| 163 | * ItemNotFound. If $convertToCollection is true and the return value is a collection (array|Traversable an |
||
| 164 | * instance of Collection will be returned. |
||
| 165 | * |
||
| 166 | * @param mixed $key |
||
| 167 | * @param bool $convertToCollection |
||
| 168 | * @return Collection|mixed |
||
| 169 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
| 170 | */ |
||
| 171 | 5 | public function get($key, $convertToCollection = false) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Returns item at the key $key. If multiple items have this key, return first. If no item has this key, return |
||
| 180 | * $ifNotFound. If no value has this key, throw ItemNotFound. If $convertToCollection is true and the return value |
||
| 181 | * is a collection (array|Traversable) an instance of Collection will be returned. |
||
| 182 | * |
||
| 183 | * @param mixed $key |
||
| 184 | * @param mixed $default |
||
| 185 | * @param bool $convertToCollection |
||
| 186 | * @return mixed|Collection |
||
| 187 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
| 188 | */ |
||
| 189 | 1 | public function getOrDefault($key, $default = null, $convertToCollection = false) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Returns first value matched by $function. If no value matches, return $default. If $convertToCollection is true |
||
| 198 | * and the return value is a collection (array|Traversable an instance of Collection will be returned. |
||
| 199 | * |
||
| 200 | * @param callable $function |
||
| 201 | * @param mixed|null $default |
||
| 202 | * @param bool $convertToCollection |
||
| 203 | * @return Collection|mixed |
||
| 204 | */ |
||
| 205 | 1 | public function find(callable $function, $default = null, $convertToCollection = false) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Returns a non-lazy collection of items whose keys are the return values of $function and values are the number of |
||
| 214 | * items in this collection for which the $function returned this value. |
||
| 215 | * |
||
| 216 | * @param callable $function |
||
| 217 | * @return Collection |
||
| 218 | */ |
||
| 219 | 1 | public function countBy(callable $function) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Returns a lazy collection by changing keys of this collection for each item to the result of $function for |
||
| 226 | * that item. |
||
| 227 | * |
||
| 228 | * @param callable $function |
||
| 229 | * @return Collection |
||
| 230 | */ |
||
| 231 | 1 | public function indexBy(callable $function) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Returns true if $function returns true for every item in this collection, false otherwise. |
||
| 238 | * |
||
| 239 | * @param callable $function |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | 1 | public function every(callable $function) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Returns true if $function returns true for at least one item in this collection, false otherwise. |
||
| 249 | * |
||
| 250 | * @param callable $function |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | 1 | public function some(callable $function) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Returns true if $value is present in the collection. |
||
| 260 | * |
||
| 261 | * @param mixed $value |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | 1 | public function contains($value) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Returns collection of items in this collection in reverse order. |
||
| 271 | * |
||
| 272 | * @return Collection |
||
| 273 | */ |
||
| 274 | 1 | public function reverse() |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Reduce the collection to single value. Walks from right to left. |
||
| 281 | * |
||
| 282 | * @param callable $function Must take 2 arguments, intermediate value and item from the iterator. If |
||
| 283 | * $convertToCollection is true and the return value is a collection (array|Traversable) an instance of Collection |
||
| 284 | * is returned. |
||
| 285 | * @param mixed $startValue |
||
| 286 | * @param bool $convertToCollection |
||
| 287 | * @return mixed|Collection |
||
| 288 | */ |
||
| 289 | 1 | public function reduceRight(callable $function, $startValue, $convertToCollection = false) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * A form of slice that returns first $numberOfItems items. |
||
| 298 | * |
||
| 299 | * @param int $numberOfItems |
||
| 300 | * @return Collection |
||
| 301 | */ |
||
| 302 | 9 | public function take($numberOfItems) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * A form of slice that returns all but first $numberOfItems items. |
||
| 309 | * |
||
| 310 | * @param int $numberOfItems |
||
| 311 | * @return Collection |
||
| 312 | */ |
||
| 313 | 2 | public function drop($numberOfItems) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Returns collection of values from this collection but with keys being numerical from 0 upwards. |
||
| 320 | * |
||
| 321 | * @return Collection |
||
| 322 | */ |
||
| 323 | 13 | public function values() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Returns a lazy collection without elements matched by $function. |
||
| 330 | * |
||
| 331 | * @param callable $function |
||
| 332 | * @return Collection |
||
| 333 | */ |
||
| 334 | 1 | public function reject(callable $function) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Returns a lazy collection of the keys of this collection. |
||
| 341 | * |
||
| 342 | * @return Collection |
||
| 343 | */ |
||
| 344 | 1 | public function keys() |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Returns a lazy collection of items of this collection separated by $separator |
||
| 351 | * |
||
| 352 | * @param mixed $separator |
||
| 353 | * @return Collection |
||
| 354 | */ |
||
| 355 | 1 | public function interpose($separator) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Returns a lazy collection with last $numberOfItems items skipped. These are still iterated over, just skipped. |
||
| 362 | * |
||
| 363 | * @param int $numberOfItems |
||
| 364 | * @return Collection |
||
| 365 | */ |
||
| 366 | 1 | public function dropLast($numberOfItems = 1) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Returns a lazy collection of first item from first collection, first item from second, second from first and |
||
| 373 | * so on. Accepts any number of collections. |
||
| 374 | * |
||
| 375 | * @param array|\Traversable ...$collections |
||
| 376 | * @return Collection |
||
| 377 | */ |
||
| 378 | 1 | public function interleave(...$collections) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Returns an infinite lazy collection of items in this collection repeated infinitely. |
||
| 385 | * |
||
| 386 | * @return Collection |
||
| 387 | */ |
||
| 388 | 1 | public function cycle() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Returns a lazy collection of items of this collection with $value added as first element. If $key is not provided |
||
| 395 | * it will be next integer index. |
||
| 396 | * |
||
| 397 | * @param mixed $value |
||
| 398 | * @param mixed|null $key |
||
| 399 | * @return Collection |
||
| 400 | */ |
||
| 401 | 2 | public function prepend($value, $key = null) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Returns a lazy collection of items of this collection with $value added as last element. If $key is not provided |
||
| 408 | * it will be next integer index. |
||
| 409 | * |
||
| 410 | * @param mixed $value |
||
| 411 | * @param mixed $key |
||
| 412 | * @return Collection |
||
| 413 | */ |
||
| 414 | 10 | public function append($value, $key = null) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Returns a lazy collection by removing items from this collection until first item for which $function returns |
||
| 421 | * false. |
||
| 422 | * |
||
| 423 | * @param callable $function |
||
| 424 | * @return Collection |
||
| 425 | */ |
||
| 426 | 1 | public function dropWhile(callable $function) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Returns a lazy collection which is a result of calling map($function) and then flatten(1) |
||
| 433 | * |
||
| 434 | * @param callable $function |
||
| 435 | * @return Collection |
||
| 436 | */ |
||
| 437 | 1 | public function mapcat(callable $function) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Returns a lazy collection of items from the start of the ollection until the first item for which $function |
||
| 444 | * returns false. |
||
| 445 | * |
||
| 446 | * @param callable $function |
||
| 447 | * @return Collection |
||
| 448 | */ |
||
| 449 | 1 | public function takeWhile(callable $function) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Returns a collection of [take($position), drop($position)] |
||
| 456 | * |
||
| 457 | * @param int $position |
||
| 458 | * @return Collection |
||
| 459 | */ |
||
| 460 | 1 | public function splitAt($position) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Returns a collection of [takeWhile($predicament), dropWhile($predicament] |
||
| 467 | * |
||
| 468 | * @param callable $function |
||
| 469 | * @return Collection |
||
| 470 | */ |
||
| 471 | 5 | public function splitWith(callable $function) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Returns a lazy collection with items from this collection but values that are found in keys of $replacementMap |
||
| 478 | * are replaced by their values. |
||
| 479 | * |
||
| 480 | * @param \Traversable|array $replacementMap |
||
| 481 | * @return Collection |
||
| 482 | */ |
||
| 483 | 1 | public function replace($replacementMap) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Returns a lazy collection of reduction steps. |
||
| 490 | * |
||
| 491 | * @param callable $function |
||
| 492 | * @param mixed $startValue |
||
| 493 | * @return Collection |
||
| 494 | */ |
||
| 495 | 1 | public function reductions(callable $function, $startValue) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Returns a lazy collection of every nth item in this collection |
||
| 502 | * |
||
| 503 | * @param int $step |
||
| 504 | * @return Collection |
||
| 505 | */ |
||
| 506 | 1 | public function takeNth($step) |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Returns a non-collection of shuffled items from this collection |
||
| 513 | * |
||
| 514 | * @return Collection |
||
| 515 | */ |
||
| 516 | 1 | public function shuffle() |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Returns a lazy collection of collections of $numberOfItems items each, at $step step |
||
| 523 | * apart. If $step is not supplied, defaults to $numberOfItems, i.e. the partitions |
||
| 524 | * do not overlap. If a $padding collection is supplied, use its elements as |
||
| 525 | * necessary to complete last partition up to $numberOfItems items. In case there are |
||
| 526 | * not enough padding elements, return a partition with less than $numberOfItems items. |
||
| 527 | * |
||
| 528 | * @param int $numberOfItems |
||
| 529 | * @param int $step |
||
| 530 | * @param array|\Traversable $padding |
||
| 531 | * @return Collection |
||
| 532 | */ |
||
| 533 | 1 | public function partition($numberOfItems, $step = 0, $padding = []) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Creates a lazy collection of collections created by partitioning this collection every time $function will |
||
| 540 | * return different result. |
||
| 541 | * |
||
| 542 | * @param callable $function |
||
| 543 | * @return Collection |
||
| 544 | */ |
||
| 545 | 1 | public function partitionBy(callable $function) |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Returns true if this collection is empty. False otherwise. |
||
| 552 | * |
||
| 553 | * @return bool |
||
| 554 | */ |
||
| 555 | 3 | public function isEmpty() |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Opposite of isEmpty. |
||
| 562 | * |
||
| 563 | * @return bool |
||
| 564 | */ |
||
| 565 | 2 | public function isNotEmpty() |
|
| 569 | |||
| 570 | /** |
||
| 571 | * Returns a collection where keys are distinct items from this collection and their values are number of |
||
| 572 | * occurrences of each value. |
||
| 573 | * |
||
| 574 | * @return Collection |
||
| 575 | */ |
||
| 576 | 1 | public function frequencies() |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Returns first item of this collection. If the collection is empty, throws ItemNotFound. If $convertToCollection |
||
| 583 | * is true and the return value is a collection (array|Traversable an instance of Collection is returned. |
||
| 584 | * |
||
| 585 | * @param bool $convertToCollection |
||
| 586 | * @return mixed|Collection |
||
| 587 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
| 588 | */ |
||
| 589 | 11 | public function first($convertToCollection = false) |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Returns last item of this collection. If the collection is empty, throws ItemNotFound. If $convertToCollection |
||
| 597 | * is true and the return value is a collection (array|Traversable) it is converted to Collection. |
||
| 598 | * |
||
| 599 | * @param bool $convertToCollection |
||
| 600 | * @return mixed|Collection |
||
| 601 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
| 602 | */ |
||
| 603 | 2 | public function last($convertToCollection = false) |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Realizes collection - turns lazy collection into non-lazy one by iterating over it and storing the key/values. |
||
| 611 | * |
||
| 612 | * @return Collection |
||
| 613 | */ |
||
| 614 | 1 | public function realize() |
|
| 618 | |||
| 619 | /** |
||
| 620 | * Returns the second item in this collection or throws ItemNotFound if the collection is empty or has 1 item. If |
||
| 621 | * $convertToCollection is true and the return value is a collection (array|Traversable) it is converted to |
||
| 622 | * Collection. |
||
| 623 | * |
||
| 624 | * @param bool $convertToCollection |
||
| 625 | * @return Collection|mixed |
||
| 626 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
| 627 | */ |
||
| 628 | 6 | public function second($convertToCollection = false) |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Combines the values of this collection as keys, with values of $collection as values. The resulting collection |
||
| 636 | * has length equal to the size of smaller collection. |
||
| 637 | * |
||
| 638 | * @param array|\Traversable $collection |
||
| 639 | * @return Collection |
||
| 640 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
| 641 | */ |
||
| 642 | 1 | public function combine($collection) |
|
| 646 | |||
| 647 | /** |
||
| 648 | * Returns a lazy collection without the items associated to any of the keys from $keys. |
||
| 649 | * |
||
| 650 | * @param array|\Traversable $keys |
||
| 651 | * @return Collection |
||
| 652 | */ |
||
| 653 | 1 | public function except($keys) |
|
| 657 | |||
| 658 | /** |
||
| 659 | * Returns a lazy collection of items associated to any of the keys from $keys. |
||
| 660 | * |
||
| 661 | * @param array|\Traversable $keys |
||
| 662 | * @return Collection |
||
| 663 | */ |
||
| 664 | 1 | public function only($keys) |
|
| 668 | |||
| 669 | /** |
||
| 670 | * Returns a lazy collection of items that are in $this but are not in any of the other arguments. Note that the |
||
| 671 | * ...$collections are iterated non-lazily. |
||
| 672 | * |
||
| 673 | * @param array|\Traversable ...$collections |
||
| 674 | * @return Collection |
||
| 675 | */ |
||
| 676 | 1 | public function diff(...$collections) |
|
| 680 | |||
| 681 | |||
| 682 | /** |
||
| 683 | * Returns a lazy collection where keys and values are flipped. |
||
| 684 | * |
||
| 685 | * @return Collection |
||
| 686 | */ |
||
| 687 | 1 | public function flip() |
|
| 691 | |||
| 692 | /** |
||
| 693 | * Checks for the existence of an item with$key in this collection. |
||
| 694 | * |
||
| 695 | * @param mixed $key |
||
| 696 | * @return bool |
||
| 697 | */ |
||
| 698 | 1 | public function has($key) |
|
| 702 | |||
| 703 | |||
| 704 | /** |
||
| 705 | * Returns a lazy collection of non-lazy collections of items from nth position from this collection and each |
||
| 706 | * passed collection. Stops when any of the collections don't have an item at the nth position. |
||
| 707 | * |
||
| 708 | * @param array|\Traversable[] ...$collections |
||
| 709 | * @return Collection |
||
| 710 | */ |
||
| 711 | 1 | public function zip(...$collections) |
|
| 716 | |||
| 717 | /** |
||
| 718 | * Uses a $transformer callable that takes a Collection and returns Collection on itself. |
||
| 719 | * |
||
| 720 | * @param callable $transformer Collection => Collection |
||
| 721 | * @return Collection |
||
| 722 | * @throws InvalidReturnValue |
||
| 723 | */ |
||
| 724 | 1 | public function transform(callable $transformer) |
|
| 736 | |||
| 737 | /** |
||
| 738 | * Extracts data from collection items by dot separated key path. Supports the * wildcard. If a key contains \ or |
||
| 739 | * it must be escaped using \ character. |
||
| 740 | * |
||
| 741 | * @param mixed $keyPath |
||
| 742 | * @return Collection |
||
| 743 | */ |
||
| 744 | 1 | public function extract($keyPath) |
|
| 748 | |||
| 749 | /** |
||
| 750 | * Returns a lazy collection of items that are in $collection and all the other arguments, indexed by the keys from |
||
| 751 | * the first collection. Note that the ...$collections are iterated non-lazily. |
||
| 752 | * |
||
| 753 | * @param array|\Traversable[] ...$collections |
||
| 754 | * @return Collection |
||
| 755 | */ |
||
| 756 | 1 | public function intersect(...$collections) |
|
| 760 | |||
| 761 | |||
| 762 | |||
| 763 | /** |
||
| 764 | * Checks whether this collection has exactly $size items. |
||
| 765 | * |
||
| 766 | * @param int $size |
||
| 767 | * @return bool |
||
| 768 | */ |
||
| 769 | 1 | public function sizeIs($size) |
|
| 773 | |||
| 774 | /** |
||
| 775 | * Checks whether this collection has less than $size items. |
||
| 776 | * |
||
| 777 | * @param int $size |
||
| 778 | * @return bool |
||
| 779 | */ |
||
| 780 | 1 | public function sizeIsLessThan($size) |
|
| 784 | |||
| 785 | /** |
||
| 786 | * Checks whether this collection has more than $size items. |
||
| 787 | * |
||
| 788 | * @param int $size |
||
| 789 | * @return bool |
||
| 790 | */ |
||
| 791 | 1 | public function sizeIsGreaterThan($size) |
|
| 795 | |||
| 796 | /** |
||
| 797 | * Checks whether this collection has between $fromSize to $toSize items. $toSize can be |
||
| 798 | * smaller than $fromSize. |
||
| 799 | * |
||
| 800 | * @param int $fromSize |
||
| 801 | * @param int $toSize |
||
| 802 | * @return bool |
||
| 803 | */ |
||
| 804 | 1 | public function sizeIsBetween($fromSize, $toSize) |
|
| 808 | |||
| 809 | /** |
||
| 810 | * Returns a sum of all values in this collection. |
||
| 811 | * |
||
| 812 | * @return int|float |
||
| 813 | */ |
||
| 814 | 1 | public function sum() |
|
| 818 | |||
| 819 | /** |
||
| 820 | * Returns average of values from this collection. |
||
| 821 | * |
||
| 822 | * @return int|float |
||
| 823 | */ |
||
| 824 | 2 | public function average() |
|
| 828 | |||
| 829 | /** |
||
| 830 | * Returns maximal value from this collection. |
||
| 831 | * |
||
| 832 | * @return mixed |
||
| 833 | */ |
||
| 834 | 2 | public function max() |
|
| 838 | |||
| 839 | /** |
||
| 840 | * Returns minimal value from this collection. |
||
| 841 | * |
||
| 842 | * @return mixed |
||
| 843 | */ |
||
| 844 | 2 | public function min() |
|
| 848 | |||
| 849 | /** |
||
| 850 | * Returns a string by concatenating the collection values into a string. |
||
| 851 | * |
||
| 852 | * @return string |
||
| 853 | */ |
||
| 854 | 1 | public function toString() |
|
| 858 | |||
| 859 | /** |
||
| 860 | * Returns a lazy collection with items from $collection, but items with keys that are found in keys of |
||
| 861 | * $replacementMap are replaced by their values. |
||
| 862 | * |
||
| 863 | * @param array|\Traversable $replacementMap |
||
| 864 | * @return Collection |
||
| 865 | */ |
||
| 866 | 1 | public function replaceByKeys($replacementMap) |
|
| 870 | |||
| 871 | |||
| 872 | |||
| 873 | /** |
||
| 874 | * /** |
||
| 875 | * Dumps this collection into array (recursively). |
||
| 876 | * |
||
| 877 | * - scalars are returned as they are, |
||
| 878 | * - array of class name => properties (name => value and only properties accessible for this class) |
||
| 879 | * is returned for objects, |
||
| 880 | * - arrays or Traversables are returned as arrays, |
||
| 881 | * - for anything else result of calling gettype($input) is returned |
||
| 882 | * |
||
| 883 | * If specified, $maxItemsPerCollection will only leave specified number of items in collection, |
||
| 884 | * appending a new element at end '>>>' if original collection was longer. |
||
| 885 | * |
||
| 886 | * If specified, $maxDepth will only leave specified n levels of nesting, replacing elements |
||
| 887 | * with '^^^' once the maximum nesting level was reached. |
||
| 888 | * |
||
| 889 | * If a collection with duplicate keys is encountered, the duplicate keys (except the first one) |
||
| 890 | * will be change into a format originalKey//duplicateCounter where duplicateCounter starts from |
||
| 891 | * 1 at the first duplicate. So [0 => 1, 0 => 2] will become [0 => 1, '0//1' => 2] |
||
| 892 | * |
||
| 893 | * @param int|null $maxItemsPerCollection |
||
| 894 | * @param int|null $maxDepth |
||
| 895 | * @return array |
||
| 896 | */ |
||
| 897 | 1 | public function dump($maxItemsPerCollection = null, $maxDepth = null) |
|
| 901 | |||
| 902 | /** |
||
| 903 | * Calls dump on this collection and then prints it using the var_export. |
||
| 904 | * |
||
| 905 | * @param null|int $maxItemsPerCollection |
||
| 906 | * @param null|int $maxDepth |
||
| 907 | * @return Collection |
||
| 908 | */ |
||
| 909 | 1 | public function printDump($maxItemsPerCollection = null, $maxDepth = null) |
|
| 913 | |||
| 914 | /** |
||
| 915 | * @return array|\Traversable |
||
| 916 | */ |
||
| 917 | 96 | protected function getItems() |
|
| 921 | } |
||
| 922 |