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 | 37 | public function __construct(array $values) |
|
| 19 | |||
| 20 | /** |
||
| 21 | * {@inheritdoc} |
||
| 22 | */ |
||
| 23 | 33 | 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 | 1 | public function push($value) |
|
| 186 | { |
||
| 187 | 1 | $values = $this->values; |
|
| 188 | 1 | array_push($values, $value); |
|
| 189 | |||
| 190 | 1 | return new self($values); |
|
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | */ |
||
| 196 | 2 | public function rand($num = 1) |
|
| 197 | { |
||
| 198 | 2 | if ((int) $num > $this->count()) { |
|
| 199 | 1 | throw new OutOfBoundException( |
|
| 200 | 'Trying to return a wider collection than the current one' |
||
| 201 | 1 | ); |
|
| 202 | } |
||
| 203 | |||
| 204 | 1 | $keys = (array) array_rand($this->values, $num); |
|
| 205 | 1 | $values = []; |
|
| 206 | |||
| 207 | 1 | foreach ($keys as $key) { |
|
| 208 | 1 | $values[$key] = $this->values[$key]; |
|
| 209 | 1 | } |
|
| 210 | |||
| 211 | 1 | return new self($values); |
|
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * {@inheritdoc} |
||
| 216 | */ |
||
| 217 | 1 | public function merge(CollectionInterface $collection) |
|
| 218 | { |
||
| 219 | 1 | return new self(array_merge( |
|
| 220 | 1 | $this->values, |
|
| 221 | 1 | $collection->toPrimitive() |
|
| 222 | 1 | )); |
|
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | 1 | public function slice($offset, $length = null, $preserveKeys = false) |
|
| 229 | { |
||
| 230 | 1 | return new self(array_slice( |
|
| 231 | 1 | $this->values, |
|
| 232 | 1 | (int) $offset, |
|
| 233 | 1 | $length === null ? null : (int) $length, |
|
| 234 | (bool) $preserveKeys |
||
| 235 | 1 | )); |
|
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | 1 | public function udiff(CollectionInterface $collection, callable $differ) |
|
| 242 | { |
||
| 243 | 1 | return new self(array_udiff( |
|
| 244 | 1 | $this->values, |
|
| 245 | 1 | $collection->toPrimitive(), |
|
| 246 | $differ |
||
| 247 | 1 | )); |
|
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | 1 | public function column($key, $indexKey = null) |
|
| 254 | { |
||
| 255 | 1 | return new self(array_column( |
|
| 256 | 1 | $this->values, |
|
| 257 | 1 | $key, |
|
| 258 | $indexKey |
||
| 259 | 1 | )); |
|
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | */ |
||
| 265 | 1 | public function splice($offset, $length = 0, $replacement = []) |
|
| 266 | { |
||
| 267 | 1 | $values = $this->values; |
|
| 268 | 1 | array_splice($values, (int) $offset, (int) $length, $replacement); |
|
| 269 | |||
| 270 | 1 | return new self($values); |
|
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | 1 | public function unique($flags = SORT_REGULAR) |
|
| 277 | { |
||
| 278 | 1 | return new self(array_unique($this->values, (int) $flags)); |
|
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * {@inheritdoc} |
||
| 283 | */ |
||
| 284 | 1 | public function values() |
|
| 285 | { |
||
| 286 | 1 | return new self(array_values($this->values)); |
|
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | 1 | public function product() |
|
| 293 | { |
||
| 294 | 1 | return array_product($this->values); |
|
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * {@inheritdoc} |
||
| 299 | */ |
||
| 300 | 1 | public function replace(CollectionInterface $collection) |
|
| 301 | { |
||
| 302 | 1 | return new self(array_replace( |
|
| 303 | 1 | $this->values, |
|
| 304 | 1 | $collection->toPrimitive() |
|
| 305 | 1 | )); |
|
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | 1 | public function reverse($preserveKeys = false) |
|
| 312 | { |
||
| 313 | 1 | return new self(array_reverse($this->values, (bool) $preserveKeys)); |
|
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * {@inheritdoc} |
||
| 318 | */ |
||
| 319 | 1 | public function unshift($value) |
|
| 320 | { |
||
| 321 | 1 | $values = $this->values; |
|
| 322 | 1 | array_unshift($values, $value); |
|
| 323 | |||
| 324 | 1 | return new self($values); |
|
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * {@inheritdoc} |
||
| 329 | */ |
||
| 330 | 1 | public function keyDiff(CollectionInterface $collection) |
|
| 331 | { |
||
| 332 | 1 | return new self(array_diff_key( |
|
| 333 | 1 | $this->values, |
|
| 334 | 1 | $collection->toPrimitive() |
|
| 335 | 1 | )); |
|
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * {@inheritdoc} |
||
| 340 | */ |
||
| 341 | 1 | public function ukeyDiff(CollectionInterface $collection, callable $differ) |
|
| 342 | { |
||
| 343 | 1 | return new self(array_diff_ukey( |
|
| 344 | 1 | $this->values, |
|
| 345 | 1 | $collection->toPrimitive(), |
|
| 346 | $differ |
||
| 347 | 1 | )); |
|
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * {@inheritdoc} |
||
| 352 | */ |
||
| 353 | 1 | public function associativeDiff(CollectionInterface $collection) |
|
| 354 | { |
||
| 355 | 1 | return new self(array_diff_assoc( |
|
| 356 | 1 | $this->values, |
|
| 357 | 1 | $collection->toPrimitive() |
|
| 358 | 1 | )); |
|
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * {@inheritdoc} |
||
| 363 | */ |
||
| 364 | 1 | public function hasKey($key, $strict = true) |
|
| 365 | { |
||
| 366 | 1 | if ((bool) $strict === true) { |
|
| 367 | 1 | $bool = array_key_exists($key, $this->values); |
|
| 368 | 1 | } else { |
|
| 369 | 1 | $bool = isset($this->values[$key]); |
|
| 370 | } |
||
| 371 | |||
| 372 | 1 | return $bool; |
|
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * {@inheritdoc} |
||
| 377 | */ |
||
| 378 | 1 | public function countValues() |
|
| 379 | { |
||
| 380 | 1 | return new self(array_count_values($this->values)); |
|
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * {@inheritdoc} |
||
| 385 | */ |
||
| 386 | 1 | public function ukeyIntersect(CollectionInterface $collection, callable $intersecter) |
|
| 387 | { |
||
| 388 | 1 | return new self(array_intersect_ukey( |
|
| 389 | 1 | $this->values, |
|
| 390 | 1 | $collection->toPrimitive(), |
|
| 391 | $intersecter |
||
| 392 | 1 | )); |
|
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * {@inheritdoc} |
||
| 397 | */ |
||
| 398 | 1 | public function associativeIntersect(CollectionInterface $collection) |
|
| 399 | { |
||
| 400 | 1 | return new self(array_intersect_assoc( |
|
| 401 | 1 | $this->values, |
|
| 402 | 1 | $collection->toPrimitive() |
|
| 403 | 1 | )); |
|
| 404 | } |
||
| 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 | 2 | public function count() |
|
| 644 | { |
||
| 645 | 2 | return count($this->values); |
|
| 646 | } |
||
| 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: