Complex classes like Collection 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 Collection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Collection implements CollectionContract { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Collection elements. |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $elements = array(); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Collection type to enforce. |
||
| 27 | * |
||
| 28 | * @var Type |
||
| 29 | */ |
||
| 30 | private $type; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Where Collection is in loop. |
||
| 34 | * |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | protected $position = 0; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Collection constructor. |
||
| 41 | * |
||
| 42 | * @param string $type |
||
| 43 | * @param array $elements |
||
| 44 | */ |
||
| 45 | 30 | public function __construct( $type, array $elements = array() ) { |
|
| 46 | 30 | $this->type = new Type( $type ); |
|
| 47 | |||
| 48 | if ( $elements ) { |
||
| 49 | $this->type->validate_elements( $elements ); |
||
| 50 | } |
||
| 51 | |||
| 52 | $this->elements = $elements; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | * |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public function get_type() { |
||
| 61 | return $this->type->get_type(); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritdoc} |
||
| 66 | * |
||
| 67 | * @param mixed $element |
||
| 68 | * |
||
| 69 | * @return Collection |
||
| 70 | * |
||
| 71 | * @throws InvalidArgumentException |
||
| 72 | */ |
||
| 73 | public function add( $element ) { |
||
| 74 | if ( $this->type->is_model() && is_array( $element ) ) { |
||
| 75 | $element = $this->type->create_model( $element ); |
||
| 76 | } |
||
| 77 | |||
| 78 | $this->type->validate_element( $element ); |
||
| 79 | |||
| 80 | $elements = $this->elements; |
||
| 81 | $elements[] = $element; |
||
| 82 | |||
| 83 | $collection = new static( $this->get_type() ); |
||
| 84 | $collection->set_from_trusted( $elements ); |
||
| 85 | |||
| 86 | return $collection; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | * |
||
| 92 | * @return Collection |
||
| 93 | */ |
||
| 94 | public function clear() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | * |
||
| 101 | * @param callable $condition Condition to satisfy. |
||
| 102 | * |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | public function contains( callable $condition ) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | * |
||
| 112 | * @param callable $condition Condition to satisfy. |
||
| 113 | * |
||
| 114 | * @return mixed |
||
| 115 | */ |
||
| 116 | public function find( callable $condition ) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | * |
||
| 125 | * @param callable $condition Condition to satisfy. |
||
| 126 | * |
||
| 127 | * @return int |
||
| 128 | */ |
||
| 129 | public function find_index( callable $condition ) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Fetches the element at the provided index. |
||
| 144 | * |
||
| 145 | * @param int $index Index to get element from. |
||
| 146 | * |
||
| 147 | * @return mixed |
||
| 148 | * |
||
| 149 | * @throws OutOfRangeException |
||
| 150 | */ |
||
| 151 | public function at( $index ) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | * |
||
| 160 | * @param int $index Index to check for existence. |
||
| 161 | * |
||
| 162 | * @return bool |
||
| 163 | * |
||
| 164 | * @throws InvalidArgumentException |
||
| 165 | */ |
||
| 166 | public function index_exists( $index ) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | * |
||
| 181 | * @param callable $condition Condition to satisfy. |
||
| 182 | * |
||
| 183 | * @return mixed |
||
| 184 | */ |
||
| 185 | public function filter( callable $condition ) { |
||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | * |
||
| 199 | * @param callable $condition Condition to satisfy. |
||
| 200 | * |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | public function find_last( callable $condition ) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | * |
||
| 212 | * @param callable $condition |
||
| 213 | * @return int |
||
| 214 | */ |
||
| 215 | public function find_last_index( callable $condition ) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | * |
||
| 231 | * @param int $start Begining index to slice from. |
||
| 232 | * @param int $end End index to slice to. |
||
| 233 | * |
||
| 234 | * @return Collection |
||
| 235 | * |
||
| 236 | * @throws InvalidArgumentException |
||
| 237 | */ |
||
| 238 | public function slice( $start, $end ) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | * |
||
| 267 | * @param int $index Index to start at. |
||
| 268 | * @param mixed $element Element to insert. |
||
| 269 | * |
||
| 270 | * @return Collection |
||
| 271 | * |
||
| 272 | * @throws InvalidArgumentException |
||
| 273 | * @throws OutOfRangeException |
||
| 274 | */ |
||
| 275 | public function insert( $index, $element ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * {@inheritdoc} |
||
| 289 | * |
||
| 290 | * @param int $index Index to start insertion at. |
||
| 291 | * @param array $elements Elements in insert. |
||
| 292 | * |
||
| 293 | * @return Collection |
||
| 294 | * |
||
| 295 | * @throws OutOfRangeException |
||
| 296 | */ |
||
| 297 | public function insert_range( $index, array $elements ) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * {@inheritdoc} |
||
| 318 | * |
||
| 319 | * @param callable $condition Condition to satisfy. |
||
| 320 | * |
||
| 321 | * @return Collection |
||
| 322 | */ |
||
| 323 | public function without( callable $condition ) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * {@inheritdoc} |
||
| 333 | * |
||
| 334 | * @param int $index Index to remove. |
||
| 335 | * |
||
| 336 | * @return Collection |
||
| 337 | * |
||
| 338 | * @throws OutOfRangeException |
||
| 339 | */ |
||
| 340 | public function remove_at( $index ) { |
||
| 352 | /** |
||
| 353 | * {@inheritdoc} |
||
| 354 | * |
||
| 355 | * @return Collection |
||
| 356 | */ |
||
| 357 | public function reverse() { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * {@inheritdoc} |
||
| 365 | * |
||
| 366 | * @param callable $callback Sort callback. |
||
| 367 | * |
||
| 368 | * @return Collection |
||
| 369 | */ |
||
| 370 | public function sort( callable $callback ) { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * {@inheritdoc} |
||
| 378 | * |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | public function to_array() { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * {@inheritdoc} |
||
| 387 | * |
||
| 388 | * @param callable $callable Reducer function. |
||
| 389 | * |
||
| 390 | * @param null $initial Initial reducer value. |
||
| 391 | * |
||
| 392 | * @return mixed |
||
| 393 | */ |
||
| 394 | public function reduce( callable $callable, $initial = null ) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * {@inheritdoc} |
||
| 400 | * |
||
| 401 | * @param callable $condition Condition callback. |
||
| 402 | * |
||
| 403 | * @return bool |
||
| 404 | */ |
||
| 405 | public function every( callable $condition ) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * {@inheritdoc} |
||
| 422 | * |
||
| 423 | * @param int $num Number of elements to drop. |
||
| 424 | * |
||
| 425 | * @return Collection |
||
| 426 | * |
||
| 427 | * @throws InvalidArgumentException |
||
| 428 | */ |
||
| 429 | public function drop( $num ) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * {@inheritdoc} |
||
| 435 | * |
||
| 436 | * @param int $num Number of elements to drop. |
||
| 437 | * |
||
| 438 | * @return Collection |
||
| 439 | * |
||
| 440 | * @throws InvalidArgumentException |
||
| 441 | */ |
||
| 442 | public function drop_right( $num ) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * {@inheritdoc} |
||
| 450 | * |
||
| 451 | * @param callable $condition Condition callback. |
||
| 452 | * |
||
| 453 | * @return Collection |
||
| 454 | */ |
||
| 455 | public function drop_while( callable $condition ) { |
||
| 459 | /** |
||
| 460 | * {@inheritdoc} |
||
| 461 | * |
||
| 462 | * @return Collection |
||
| 463 | * |
||
| 464 | * @throws InvalidArgumentException |
||
| 465 | */ |
||
| 466 | public function tail() { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * {@inheritdoc} |
||
| 472 | * |
||
| 473 | * @param int $num Number of elements to take. |
||
| 474 | * |
||
| 475 | * @return Collection |
||
| 476 | * |
||
| 477 | * @throws InvalidArgumentException |
||
| 478 | */ |
||
| 479 | public function take( $num ) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * {@inheritdoc} |
||
| 485 | * |
||
| 486 | * @param int $num Number of elements to take. |
||
| 487 | * |
||
| 488 | * @return Collection |
||
| 489 | * |
||
| 490 | * @throws InvalidArgumentException |
||
| 491 | */ |
||
| 492 | public function take_right( $num ) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * {@inheritdoc} |
||
| 498 | * |
||
| 499 | * @param callable $condition Callback function. |
||
| 500 | * |
||
| 501 | * @return Collection |
||
| 502 | */ |
||
| 503 | public function take_while( callable $condition ) { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * {@inheritdoc} |
||
| 511 | * |
||
| 512 | * @param callable $callable Callback function. |
||
| 513 | */ |
||
| 514 | public function each( callable $callable ) { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * {@inheritdoc} |
||
| 522 | * |
||
| 523 | * @param callable $callable Callback function. |
||
| 524 | * |
||
| 525 | * @return Collection |
||
| 526 | */ |
||
| 527 | public function map( callable $callable ) { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * {@inheritdoc} |
||
| 549 | * |
||
| 550 | * @param callable $callable Reducer function. |
||
| 551 | * @param null $initial Initial value. |
||
| 552 | * |
||
| 553 | * @return mixed |
||
| 554 | */ |
||
| 555 | public function reduce_right( callable $callable, $initial = null ) { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * {@inheritdoc} |
||
| 565 | * |
||
| 566 | * @return Collection |
||
| 567 | */ |
||
| 568 | public function shuffle() { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * {@inheritdoc} |
||
| 577 | * |
||
| 578 | * @param array|Collection $elements Array of elements to merge. |
||
| 579 | * |
||
| 580 | * @return Collection |
||
| 581 | * |
||
| 582 | * @throws InvalidArgumentException |
||
| 583 | */ |
||
| 584 | public function merge( $elements ) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * {@inheritdoc} |
||
| 602 | * |
||
| 603 | * @return mixed |
||
| 604 | * |
||
| 605 | * @throws OutOfBoundsException |
||
| 606 | */ |
||
| 607 | public function first() { |
||
| 614 | |||
| 615 | /** |
||
| 616 | * {@inheritdoc} |
||
| 617 | * |
||
| 618 | * @return mixed |
||
| 619 | * |
||
| 620 | * @throws OutOfBoundsException |
||
| 621 | */ |
||
| 622 | public function last() { |
||
| 629 | |||
| 630 | /** |
||
| 631 | * {@inheritdoc} |
||
| 632 | * |
||
| 633 | * @return int |
||
| 634 | */ |
||
| 635 | public function count() { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * {@inheritDoc} |
||
| 641 | * |
||
| 642 | * @return array |
||
| 643 | */ |
||
| 644 | public function serialize() { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Return the current element. |
||
| 656 | * |
||
| 657 | * @return mixed |
||
| 658 | */ |
||
| 659 | public function current() { |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Move forward to next element. |
||
| 665 | */ |
||
| 666 | public function next() { |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Return the key of the current element. |
||
| 672 | * |
||
| 673 | * @return mixed |
||
| 674 | */ |
||
| 675 | public function key() { |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Checks if current position is valid. |
||
| 681 | * |
||
| 682 | * @return bool |
||
| 683 | */ |
||
| 684 | public function valid() { |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Rewind the Iterator to the first element. |
||
| 690 | */ |
||
| 691 | public function rewind() { |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Creates a new instance of the Collection |
||
| 697 | * from a trusted set of elements. |
||
| 698 | * |
||
| 699 | * @param array $elements Array of elements to pass into new collection. |
||
| 700 | * @param null|mixed $type |
||
| 701 | * |
||
| 702 | * @return static |
||
| 703 | */ |
||
| 704 | protected function new_from_trusted( array $elements, $type = null ) { |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Sets the elements without validating them. |
||
| 713 | * |
||
| 714 | * @param array $elements Pre-validated elements to set. |
||
| 715 | */ |
||
| 716 | protected function set_from_trusted( array $elements ) { |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Number of elements true for the condition. |
||
| 722 | * |
||
| 723 | * @param callable $condition Condition to check. |
||
| 724 | * @return int |
||
| 725 | */ |
||
| 726 | protected function count_while_true( callable $condition ) { |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Validates a number to be used as an index. |
||
| 741 | * |
||
| 742 | * @param integer $index The number to be validated as an index. |
||
| 743 | * |
||
| 744 | * @throws OutOfRangeException |
||
| 745 | */ |
||
| 746 | protected function validate_index( $index ) { |
||
| 753 | } |
||
| 754 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: