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 | 375 | public function __construct(string $type) |
|
| 29 | |||
| 30 | 3 | public static function of(string $type, ...$values): self |
|
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | 141 | public function type(): Str |
|
| 48 | |||
| 49 | /** |
||
| 50 | * {@inheritdoc} |
||
| 51 | */ |
||
| 52 | 48 | public function size(): int |
|
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | 18 | public function count(): int |
|
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | 135 | public function toPrimitive() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | 102 | public function current() |
|
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | 72 | public function key() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | 96 | public function next() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | 102 | public function rewind() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | 102 | public function valid() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | 3 | public function offsetExists($offset): bool |
|
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | 27 | public function offsetGet($offset) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * {@inheritdoc} |
||
| 131 | */ |
||
| 132 | 3 | public function offsetSet($offset, $value) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | 3 | public function offsetUnset($offset) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | 78 | public function get(int $index) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | 6 | public function diff(StreamInterface $stream): StreamInterface |
|
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | 3 | public function distinct(): StreamInterface |
|
| 178 | |||
| 179 | /** |
||
| 180 | * {@inheritdoc} |
||
| 181 | */ |
||
| 182 | 24 | public function drop(int $size): StreamInterface |
|
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritdoc} |
||
| 192 | */ |
||
| 193 | 3 | public function dropEnd(int $size): StreamInterface |
|
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | */ |
||
| 204 | 33 | public function equals(StreamInterface $stream): bool |
|
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritdoc} |
||
| 215 | */ |
||
| 216 | 6 | public function filter(callable $predicate): StreamInterface |
|
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | 6 | public function foreach(callable $function): StreamInterface |
|
| 233 | |||
| 234 | /** |
||
| 235 | * {@inheritdoc} |
||
| 236 | */ |
||
| 237 | 9 | View Code Duplication | public function groupBy(callable $discriminator): MapInterface |
| 270 | |||
| 271 | /** |
||
| 272 | * {@inheritdoc} |
||
| 273 | */ |
||
| 274 | 6 | public function first() |
|
| 278 | |||
| 279 | /** |
||
| 280 | * {@inheritdoc} |
||
| 281 | */ |
||
| 282 | 6 | public function last() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * {@inheritdoc} |
||
| 289 | */ |
||
| 290 | 153 | public function contains($element): bool |
|
| 294 | |||
| 295 | /** |
||
| 296 | * {@inheritdoc} |
||
| 297 | */ |
||
| 298 | 57 | public function indexOf($element): int |
|
| 302 | |||
| 303 | /** |
||
| 304 | * {@inheritdoc} |
||
| 305 | */ |
||
| 306 | 3 | public function indices(): StreamInterface |
|
| 313 | |||
| 314 | /** |
||
| 315 | * {@inheritdoc} |
||
| 316 | */ |
||
| 317 | 9 | public function map(callable $function): StreamInterface |
|
| 328 | |||
| 329 | /** |
||
| 330 | * {@inheritdoc} |
||
| 331 | */ |
||
| 332 | 6 | View Code Duplication | public function pad(int $size, $element): StreamInterface |
| 341 | |||
| 342 | /** |
||
| 343 | * {@inheritdoc} |
||
| 344 | */ |
||
| 345 | 6 | public function partition(callable $predicate): MapInterface |
|
| 362 | |||
| 363 | /** |
||
| 364 | * {@inheritdoc} |
||
| 365 | */ |
||
| 366 | 9 | public function slice(int $from, int $until): StreamInterface |
|
| 373 | |||
| 374 | /** |
||
| 375 | * {@inheritdoc} |
||
| 376 | */ |
||
| 377 | 3 | public function splitAt(int $position): StreamInterface |
|
| 388 | |||
| 389 | /** |
||
| 390 | * {@inheritdoc} |
||
| 391 | */ |
||
| 392 | 24 | public function take(int $size): StreamInterface |
|
| 399 | |||
| 400 | /** |
||
| 401 | * {@inheritdoc} |
||
| 402 | */ |
||
| 403 | 3 | public function takeEnd(int $size): StreamInterface |
|
| 410 | |||
| 411 | /** |
||
| 412 | * {@inheritdoc} |
||
| 413 | */ |
||
| 414 | 33 | View Code Duplication | public function append(StreamInterface $stream): StreamInterface |
| 425 | |||
| 426 | /** |
||
| 427 | * {@inheritdoc} |
||
| 428 | */ |
||
| 429 | 9 | View Code Duplication | public function intersect(StreamInterface $stream): StreamInterface |
| 440 | |||
| 441 | /** |
||
| 442 | * {@inheritdoc} |
||
| 443 | */ |
||
| 444 | 21 | public function join(string $separator): Str |
|
| 448 | |||
| 449 | /** |
||
| 450 | * {@inheritdoc} |
||
| 451 | */ |
||
| 452 | 297 | View Code Duplication | public function add($element): StreamInterface |
| 461 | |||
| 462 | /** |
||
| 463 | * {@inheritdoc} |
||
| 464 | */ |
||
| 465 | 6 | public function sort(callable $function): StreamInterface |
|
| 472 | |||
| 473 | /** |
||
| 474 | * {@inheritdoc} |
||
| 475 | */ |
||
| 476 | 60 | public function reduce($carry, callable $reducer) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * {@inheritdoc} |
||
| 483 | */ |
||
| 484 | 45 | public function clear(): StreamInterface |
|
| 491 | |||
| 492 | /** |
||
| 493 | * {@inheritdoc} |
||
| 494 | */ |
||
| 495 | 6 | public function reverse(): StreamInterface |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Make sure the stream is compatible with the current one |
||
| 505 | * |
||
| 506 | * @param StreamInterface $stream |
||
| 507 | * |
||
| 508 | * @throws InvalidArgumentException |
||
| 509 | * |
||
| 510 | * @return void |
||
| 511 | */ |
||
| 512 | 75 | private function validate(StreamInterface $stream) |
|
| 520 | } |
||
| 521 |
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: