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 Stream 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 Stream, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Stream implements StreamInterface |
||
| 16 | { |
||
| 17 | use Type; |
||
| 18 | |||
| 19 | private $type; |
||
| 20 | private $spec; |
||
| 21 | private $values; |
||
| 22 | |||
| 23 | 100 | public function __construct(string $type) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * {@inheritdoc} |
||
| 32 | */ |
||
| 33 | 52 | public function type(): Str |
|
| 37 | |||
| 38 | /** |
||
| 39 | * {@inheritdoc} |
||
| 40 | */ |
||
| 41 | 6 | public function size(): int |
|
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | */ |
||
| 49 | 4 | public function count(): int |
|
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | 38 | public function toPrimitive() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * {@inheritdoc} |
||
| 64 | */ |
||
| 65 | 14 | public function current() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | */ |
||
| 73 | 10 | public function key() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | */ |
||
| 81 | 14 | public function next() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | */ |
||
| 89 | 14 | public function rewind() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritdoc} |
||
| 96 | */ |
||
| 97 | 14 | public function valid() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | 2 | public function offsetExists($offset): bool |
|
| 109 | |||
| 110 | /** |
||
| 111 | * {@inheritdoc} |
||
| 112 | */ |
||
| 113 | 10 | public function offsetGet($offset) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritdoc} |
||
| 120 | */ |
||
| 121 | 2 | public function offsetSet($offset, $value) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | 2 | public function offsetUnset($offset) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritdoc} |
||
| 136 | */ |
||
| 137 | 14 | public function get(int $index) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | 2 | public function diff(StreamInterface $stream): StreamInterface |
|
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | 2 | public function distinct(): StreamInterface |
|
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | 2 | public function drop(int $size): StreamInterface |
|
| 178 | |||
| 179 | /** |
||
| 180 | * {@inheritdoc} |
||
| 181 | */ |
||
| 182 | 2 | public function dropEnd(int $size): StreamInterface |
|
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritdoc} |
||
| 192 | */ |
||
| 193 | 4 | public function equals(StreamInterface $stream): bool |
|
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | 2 | public function filter(callable $predicate): StreamInterface |
|
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritdoc} |
||
| 215 | */ |
||
| 216 | public function foreach(callable $function): StreamInterface |
||
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritdoc} |
||
| 225 | */ |
||
| 226 | 4 | View Code Duplication | public function groupBy(callable $discriminator): MapInterface |
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | */ |
||
| 264 | 4 | public function first() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | 4 | public function last() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritdoc} |
||
| 279 | */ |
||
| 280 | 2 | public function contains($element): bool |
|
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | 2 | public function indexOf($element): int |
|
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritdoc} |
||
| 295 | */ |
||
| 296 | 2 | public function indices(): StreamInterface |
|
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritdoc} |
||
| 306 | */ |
||
| 307 | 6 | public function map(callable $function): StreamInterface |
|
| 318 | |||
| 319 | /** |
||
| 320 | * {@inheritdoc} |
||
| 321 | */ |
||
| 322 | 4 | View Code Duplication | public function pad(int $size, $element): StreamInterface |
| 331 | |||
| 332 | /** |
||
| 333 | * {@inheritdoc} |
||
| 334 | */ |
||
| 335 | 2 | public function partition(callable $predicate): MapInterface |
|
| 352 | |||
| 353 | /** |
||
| 354 | * {@inheritdoc} |
||
| 355 | */ |
||
| 356 | 2 | public function slice(int $from, int $until): StreamInterface |
|
| 363 | |||
| 364 | /** |
||
| 365 | * {@inheritdoc} |
||
| 366 | */ |
||
| 367 | 2 | public function splitAt(int $position): StreamInterface |
|
| 378 | |||
| 379 | /** |
||
| 380 | * {@inheritdoc} |
||
| 381 | */ |
||
| 382 | 2 | public function take(int $size): StreamInterface |
|
| 389 | |||
| 390 | /** |
||
| 391 | * {@inheritdoc} |
||
| 392 | */ |
||
| 393 | 2 | public function takeEnd(int $size): StreamInterface |
|
| 400 | |||
| 401 | /** |
||
| 402 | * {@inheritdoc} |
||
| 403 | */ |
||
| 404 | 4 | View Code Duplication | public function append(StreamInterface $stream): StreamInterface |
| 415 | |||
| 416 | /** |
||
| 417 | * {@inheritdoc} |
||
| 418 | */ |
||
| 419 | 4 | View Code Duplication | public function intersect(StreamInterface $stream): StreamInterface |
| 430 | |||
| 431 | /** |
||
| 432 | * {@inheritdoc} |
||
| 433 | */ |
||
| 434 | 4 | public function join(string $separator): Str |
|
| 438 | |||
| 439 | /** |
||
| 440 | * {@inheritdoc} |
||
| 441 | */ |
||
| 442 | 80 | View Code Duplication | public function add($element): StreamInterface |
| 451 | |||
| 452 | /** |
||
| 453 | * {@inheritdoc} |
||
| 454 | */ |
||
| 455 | 2 | public function sort(callable $function): StreamInterface |
|
| 462 | |||
| 463 | /** |
||
| 464 | * {@inheritdoc} |
||
| 465 | */ |
||
| 466 | 2 | public function reduce($carry, callable $reducer) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Make sure the stream is compatible with the current one |
||
| 473 | * |
||
| 474 | * @param StreamInterface $stream |
||
| 475 | * |
||
| 476 | * @throws InvalidArgumentException |
||
| 477 | * |
||
| 478 | * @return void |
||
| 479 | */ |
||
| 480 | 14 | private function validate(StreamInterface $stream) |
|
| 488 | } |
||
| 489 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.