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 |
||
| 11 | class Collection implements CollectionInterface |
||
| 12 | { |
||
| 13 | private $values; |
||
| 14 | |||
| 15 | 16 | public function __construct(array $values) |
|
| 19 | |||
| 20 | /** |
||
| 21 | * {@inheritdoc} |
||
| 22 | */ |
||
| 23 | 15 | public function toPrimitive() |
|
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
| 31 | 1 | public function filter(callable $filter = null) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | */ |
||
| 49 | 1 | public function intersect(CollectionInterface $collection) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | 1 | public function chunk($size) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | 1 | public function shift() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | 1 | public function reduce(callable $reducer, $initial = null) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | 1 | public function search($needle, $strict = true) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | 1 | public function uintersect(CollectionInterface $collection, callable $intersecter) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | */ |
||
| 107 | 1 | public function keyIntersect(CollectionInterface $collection) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | 1 | public function map(callable $mapper) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | 1 | public function pad($size, $value) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | 1 | public function pop() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | 1 | public function sum() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | 1 | public function diff(CollectionInterface $collection) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | 1 | public function flip() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | 1 | public function keys($search = null, $strict = true) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritdoc} |
||
| 184 | */ |
||
| 185 | public function push($value) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | */ |
||
| 196 | public function rand($num = 1) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * {@inheritdoc} |
||
| 216 | */ |
||
| 217 | public function merge(CollectionInterface $collection) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | public function slice($offset, $length = null, $preserveKeys = false) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | public function udiff(CollectionInterface $collection, callable $differ) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | public function column($key, $indexKey = null) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | */ |
||
| 265 | public function splice($offset, $length = 0, $replacement = []) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | public function unique($flags = SORT_REGULAR) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * {@inheritdoc} |
||
| 283 | */ |
||
| 284 | public function values() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | public function product() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * {@inheritdoc} |
||
| 299 | */ |
||
| 300 | public function replace(CollectionInterface $collection) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | public function reverse($preserveKeys = false) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * {@inheritdoc} |
||
| 318 | */ |
||
| 319 | public function unshift($value) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * {@inheritdoc} |
||
| 329 | */ |
||
| 330 | public function keyDiff(CollectionInterface $collection) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * {@inheritdoc} |
||
| 340 | */ |
||
| 341 | public function ukeyDiff(CollectionInterface $collection, callable $differ) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * {@inheritdoc} |
||
| 352 | */ |
||
| 353 | public function associativeDiff(CollectionInterface $collection) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * {@inheritdoc} |
||
| 363 | */ |
||
| 364 | public function hasKey($key, $strict = true) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * {@inheritdoc} |
||
| 377 | */ |
||
| 378 | public function countValues() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * {@inheritdoc} |
||
| 385 | */ |
||
| 386 | public function ukeyIntersect(CollectionInterface $collection, callable $intersecter) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * {@inheritdoc} |
||
| 397 | */ |
||
| 398 | public function associativeIntersect(CollectionInterface $collection) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * {@inheritdoc} |
||
| 408 | */ |
||
| 409 | View Code Duplication | public function sort($flags = SORT_REGULAR) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * {@inheritdoc} |
||
| 423 | */ |
||
| 424 | View Code Duplication | public function associativeSort($flags = SORT_REGULAR) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * {@inheritdoc} |
||
| 438 | */ |
||
| 439 | View Code Duplication | public function keySort($flags = SORT_REGULAR) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * {@inheritdoc} |
||
| 453 | */ |
||
| 454 | View Code Duplication | public function ukeySort(callable $sorter) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * {@inheritdoc} |
||
| 468 | */ |
||
| 469 | View Code Duplication | public function reverseSort($flags = SORT_REGULAR) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * {@inheritdoc} |
||
| 483 | */ |
||
| 484 | View Code Duplication | public function usort(callable $sorter) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * {@inheritdoc} |
||
| 498 | */ |
||
| 499 | View Code Duplication | public function associativeReverseSort($flags = SORT_REGULAR) |
|
| 510 | |||
| 511 | /** |
||
| 512 | * {@inheritdoc} |
||
| 513 | */ |
||
| 514 | View Code Duplication | public function keyReverseSort($flags = SORT_REGULAR) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * {@inheritdoc} |
||
| 528 | */ |
||
| 529 | View Code Duplication | public function uassociativeSort(callable $sorter) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * {@inheritdoc} |
||
| 543 | */ |
||
| 544 | public function naturalSort() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * {@inheritdoc} |
||
| 558 | */ |
||
| 559 | View Code Duplication | public function first() |
|
| 567 | |||
| 568 | /** |
||
| 569 | * {@inheritdoc} |
||
| 570 | */ |
||
| 571 | View Code Duplication | public function last() |
|
| 581 | |||
| 582 | /** |
||
| 583 | * {@inheritdoc} |
||
| 584 | */ |
||
| 585 | public function each(callable $callback) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * {@inheritdoc} |
||
| 596 | */ |
||
| 597 | public function join($separator) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * {@inheritdoc} |
||
| 604 | */ |
||
| 605 | public function shuffle() |
||
| 616 | |||
| 617 | /** |
||
| 618 | * {@inheritdoc} |
||
| 619 | */ |
||
| 620 | public function take($size, $preserveKeys = false) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * {@inheritdoc} |
||
| 642 | */ |
||
| 643 | public function count() |
||
| 647 | |||
| 648 | /** |
||
| 649 | * {@inheritdoc} |
||
| 650 | */ |
||
| 651 | public function current() |
||
| 655 | |||
| 656 | /** |
||
| 657 | * {@inheritdoc} |
||
| 658 | */ |
||
| 659 | public function key() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * {@inheritdoc} |
||
| 666 | */ |
||
| 667 | public function next() |
||
| 671 | |||
| 672 | /** |
||
| 673 | * {@inheritdoc} |
||
| 674 | */ |
||
| 675 | public function rewind() |
||
| 679 | |||
| 680 | /** |
||
| 681 | * {@inheritdoc} |
||
| 682 | */ |
||
| 683 | public function valid() |
||
| 687 | |||
| 688 | /** |
||
| 689 | * {@inheritdoc} |
||
| 690 | */ |
||
| 691 | public function offsetExists($offset) |
||
| 695 | |||
| 696 | /** |
||
| 697 | * {@inheritdoc} |
||
| 698 | */ |
||
| 699 | public function offsetGet($offset) |
||
| 710 | |||
| 711 | /** |
||
| 712 | * {@inheritdoc} |
||
| 713 | */ |
||
| 714 | public function offsetSet($offset, $value) |
||
| 718 | |||
| 719 | /** |
||
| 720 | * {@inheritdoc} |
||
| 721 | */ |
||
| 722 | public function offsetUnset($offset) |
||
| 726 | } |
||
| 727 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: