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 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 |
||
| 15 | class Collection implements CollectionInterface |
||
|
|
|||
| 16 | { |
||
| 17 | private $values; |
||
| 18 | |||
| 19 | 139 | public function __construct(array $values) |
|
| 28 | |||
| 29 | /** |
||
| 30 | * {@inheritdoc} |
||
| 31 | */ |
||
| 32 | 97 | public function toPrimitive() |
|
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritdoc} |
||
| 39 | */ |
||
| 40 | 2 | public function filter(callable $filter = null): CollectionInterface |
|
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | 2 | public function intersect(CollectionInterface $collection): CollectionInterface |
|
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | 2 | View Code Duplication | public function chunk(int $size): CollectionInterface |
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | 2 | public function shift(): CollectionInterface |
|
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | 1 | public function reduce(callable $reducer, $initial = null) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | 1 | public function search($needle, bool $strict = true) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | 2 | public function uintersect(CollectionInterface $collection, callable $intersecter): CollectionInterface |
|
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritdoc} |
||
| 122 | */ |
||
| 123 | 2 | public function keyIntersect(CollectionInterface $collection): CollectionInterface |
|
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | 3 | public function map(callable $mapper): CollectionInterface |
|
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | 2 | public function pad(int $size, $value): CollectionInterface |
|
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | 2 | public function pop(): CollectionInterface |
|
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | 1 | public function sum() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | 2 | public function diff(CollectionInterface $collection): CollectionInterface |
|
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | 1 | public function flip(): CollectionInterface |
|
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritdoc} |
||
| 184 | */ |
||
| 185 | 1 | public function keys($search = null, bool $strict = true): CollectionInterface |
|
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | */ |
||
| 201 | 2 | public function push($value): CollectionInterface |
|
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | 4 | public function rand(int $num = 1): CollectionInterface |
|
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | */ |
||
| 233 | 2 | public function merge(CollectionInterface $collection): CollectionInterface |
|
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritdoc} |
||
| 243 | */ |
||
| 244 | 2 | public function slice(int $offset, int $length = null, bool $preserveKeys = false): CollectionInterface |
|
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | */ |
||
| 257 | 2 | public function udiff(CollectionInterface $collection, callable $differ): CollectionInterface |
|
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritdoc} |
||
| 268 | */ |
||
| 269 | 1 | public function column($key, $indexKey = null): CollectionInterface |
|
| 277 | |||
| 278 | /** |
||
| 279 | * {@inheritdoc} |
||
| 280 | */ |
||
| 281 | 2 | public function splice(int $offset, int $length = 0, $replacement = []): CollectionInterface |
|
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | 2 | public function unique(int $flags = self::SORT_REGULAR): CollectionInterface |
|
| 296 | |||
| 297 | /** |
||
| 298 | * {@inheritdoc} |
||
| 299 | */ |
||
| 300 | 2 | public function values(): CollectionInterface |
|
| 304 | |||
| 305 | /** |
||
| 306 | * {@inheritdoc} |
||
| 307 | */ |
||
| 308 | 1 | public function product() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * {@inheritdoc} |
||
| 315 | */ |
||
| 316 | 2 | public function replace(CollectionInterface $collection): CollectionInterface |
|
| 323 | |||
| 324 | /** |
||
| 325 | * {@inheritdoc} |
||
| 326 | */ |
||
| 327 | 2 | public function reverse(bool $preserveKeys = false): CollectionInterface |
|
| 331 | |||
| 332 | /** |
||
| 333 | * {@inheritdoc} |
||
| 334 | */ |
||
| 335 | 2 | public function unshift($value): CollectionInterface |
|
| 342 | |||
| 343 | /** |
||
| 344 | * {@inheritdoc} |
||
| 345 | */ |
||
| 346 | 2 | public function keyDiff(CollectionInterface $collection): CollectionInterface |
|
| 353 | |||
| 354 | /** |
||
| 355 | * {@inheritdoc} |
||
| 356 | */ |
||
| 357 | 2 | public function ukeyDiff(CollectionInterface $collection, callable $differ): CollectionInterface |
|
| 365 | |||
| 366 | /** |
||
| 367 | * {@inheritdoc} |
||
| 368 | */ |
||
| 369 | 2 | public function associativeDiff(CollectionInterface $collection): CollectionInterface |
|
| 376 | |||
| 377 | /** |
||
| 378 | * {@inheritdoc} |
||
| 379 | */ |
||
| 380 | 47 | public function hasKey($key, bool $strict = true): bool |
|
| 390 | |||
| 391 | /** |
||
| 392 | * {@inheritdoc} |
||
| 393 | */ |
||
| 394 | 1 | public function countValues(): CollectionInterface |
|
| 398 | |||
| 399 | /** |
||
| 400 | * {@inheritdoc} |
||
| 401 | */ |
||
| 402 | 2 | public function ukeyIntersect(CollectionInterface $collection, callable $intersecter): CollectionInterface |
|
| 410 | |||
| 411 | /** |
||
| 412 | * {@inheritdoc} |
||
| 413 | */ |
||
| 414 | 2 | public function associativeIntersect(CollectionInterface $collection): CollectionInterface |
|
| 421 | |||
| 422 | /** |
||
| 423 | * {@inheritdoc} |
||
| 424 | */ |
||
| 425 | 2 | View Code Duplication | public function sort(int $flags = self::SORT_REGULAR): CollectionInterface |
| 436 | |||
| 437 | /** |
||
| 438 | * {@inheritdoc} |
||
| 439 | */ |
||
| 440 | 2 | View Code Duplication | public function associativeSort(int $flags = self::SORT_REGULAR): CollectionInterface |
| 451 | |||
| 452 | /** |
||
| 453 | * {@inheritdoc} |
||
| 454 | */ |
||
| 455 | 2 | View Code Duplication | public function keySort(int $flags = self::SORT_REGULAR): CollectionInterface |
| 466 | |||
| 467 | /** |
||
| 468 | * {@inheritdoc} |
||
| 469 | */ |
||
| 470 | 2 | View Code Duplication | public function ukeySort(callable $sorter): CollectionInterface |
| 481 | |||
| 482 | /** |
||
| 483 | * {@inheritdoc} |
||
| 484 | */ |
||
| 485 | 2 | View Code Duplication | public function reverseSort(int $flags = self::SORT_REGULAR): CollectionInterface |
| 496 | |||
| 497 | /** |
||
| 498 | * {@inheritdoc} |
||
| 499 | */ |
||
| 500 | 2 | View Code Duplication | public function usort(callable $sorter): CollectionInterface |
| 511 | |||
| 512 | /** |
||
| 513 | * {@inheritdoc} |
||
| 514 | */ |
||
| 515 | 2 | View Code Duplication | public function associativeReverseSort(int $flags = self::SORT_REGULAR): CollectionInterface |
| 526 | |||
| 527 | /** |
||
| 528 | * {@inheritdoc} |
||
| 529 | */ |
||
| 530 | 2 | View Code Duplication | public function keyReverseSort(int $flags = self::SORT_REGULAR): CollectionInterface |
| 541 | |||
| 542 | /** |
||
| 543 | * {@inheritdoc} |
||
| 544 | */ |
||
| 545 | 2 | View Code Duplication | public function uassociativeSort(callable $sorter): CollectionInterface |
| 556 | |||
| 557 | /** |
||
| 558 | * {@inheritdoc} |
||
| 559 | */ |
||
| 560 | 2 | View Code Duplication | public function naturalSort(): CollectionInterface |
| 571 | |||
| 572 | /** |
||
| 573 | * {@inheritdoc} |
||
| 574 | */ |
||
| 575 | 2 | View Code Duplication | public function first() |
| 583 | |||
| 584 | /** |
||
| 585 | * {@inheritdoc} |
||
| 586 | */ |
||
| 587 | 2 | View Code Duplication | public function last() |
| 597 | |||
| 598 | /** |
||
| 599 | * {@inheritdoc} |
||
| 600 | */ |
||
| 601 | 1 | public function each(callable $callback): CollectionInterface |
|
| 609 | |||
| 610 | /** |
||
| 611 | * {@inheritdoc} |
||
| 612 | */ |
||
| 613 | 2 | public function join(string $separator): string |
|
| 617 | |||
| 618 | /** |
||
| 619 | * {@inheritdoc} |
||
| 620 | */ |
||
| 621 | 2 | View Code Duplication | public function shuffle(): CollectionInterface |
| 632 | |||
| 633 | /** |
||
| 634 | * {@inheritdoc} |
||
| 635 | */ |
||
| 636 | 2 | public function take(int $size, bool $preserveKeys = false): CollectionInterface |
|
| 656 | |||
| 657 | /** |
||
| 658 | * {@inheritdoc} |
||
| 659 | */ |
||
| 660 | 2 | public function grep(string $pattern, bool $revert = false): CollectionInterface |
|
| 668 | |||
| 669 | /** |
||
| 670 | * {@inheritdoc} |
||
| 671 | */ |
||
| 672 | 2 | public function set($key, $value): CollectionInterface |
|
| 679 | |||
| 680 | /** |
||
| 681 | * {@inheritdoc} |
||
| 682 | */ |
||
| 683 | 2 | public function contains($value): bool |
|
| 687 | |||
| 688 | /** |
||
| 689 | * {@inheritdoc} |
||
| 690 | */ |
||
| 691 | 2 | public function get($key) |
|
| 695 | |||
| 696 | /** |
||
| 697 | * {@inheritdoc} |
||
| 698 | */ |
||
| 699 | 2 | View Code Duplication | public function walk(callable $walker): CollectionInterface |
| 709 | |||
| 710 | /** |
||
| 711 | * {@inheritdoc} |
||
| 712 | */ |
||
| 713 | 4 | public function unset($index): CollectionInterface |
|
| 727 | |||
| 728 | /** |
||
| 729 | * {@inheritdoc} |
||
| 730 | */ |
||
| 731 | 15 | public function count(): int |
|
| 735 | |||
| 736 | /** |
||
| 737 | * {@inheritdoc} |
||
| 738 | */ |
||
| 739 | 4 | public function current() |
|
| 743 | |||
| 744 | /** |
||
| 745 | * {@inheritdoc} |
||
| 746 | */ |
||
| 747 | 4 | public function key() |
|
| 751 | |||
| 752 | /** |
||
| 753 | * {@inheritdoc} |
||
| 754 | */ |
||
| 755 | 4 | public function next() |
|
| 759 | |||
| 760 | /** |
||
| 761 | * {@inheritdoc} |
||
| 762 | */ |
||
| 763 | 4 | public function rewind() |
|
| 767 | |||
| 768 | /** |
||
| 769 | * {@inheritdoc} |
||
| 770 | */ |
||
| 771 | 4 | public function valid(): bool |
|
| 775 | |||
| 776 | /** |
||
| 777 | * {@inheritdoc} |
||
| 778 | */ |
||
| 779 | 1 | public function offsetExists($offset): bool |
|
| 783 | |||
| 784 | /** |
||
| 785 | * {@inheritdoc} |
||
| 786 | */ |
||
| 787 | 42 | public function offsetGet($offset) |
|
| 798 | |||
| 799 | /** |
||
| 800 | * {@inheritdoc} |
||
| 801 | */ |
||
| 802 | 1 | public function offsetSet($offset, $value) |
|
| 806 | |||
| 807 | /** |
||
| 808 | * {@inheritdoc} |
||
| 809 | */ |
||
| 810 | 1 | public function offsetUnset($offset) |
|
| 814 | } |
||
| 815 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.