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:
1 | <?php |
||
7 | View Code Duplication | final class EventStreamViaPersistentSubscriptionIterator implements \Iterator |
|
|
|||
8 | { |
||
9 | /** |
||
10 | * @var EventStreamViaPersistentSubscriptionFeedIterator |
||
11 | */ |
||
12 | private $feedIterator; |
||
13 | |||
14 | /** |
||
15 | * @var \ArrayIterator |
||
16 | */ |
||
17 | private $eventsIterator; |
||
18 | |||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | private $currentKey; |
||
23 | |||
24 | /** |
||
25 | * @param EventStreamViaPersistentSubscriptionFeedIterator $feedIterator |
||
26 | */ |
||
27 | public function __construct(EventStreamViaPersistentSubscriptionFeedIterator $feedIterator) |
||
31 | |||
32 | /** |
||
33 | * @return EventRecord |
||
34 | */ |
||
35 | public function current(): EventRecord |
||
39 | |||
40 | public function next() |
||
55 | |||
56 | /** |
||
57 | * @return \ArrayIterator |
||
58 | */ |
||
59 | private function newEventsIterator() |
||
67 | |||
68 | /** |
||
69 | * @return int |
||
70 | */ |
||
71 | public function key() |
||
75 | |||
76 | /** |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function valid(): bool |
||
83 | |||
84 | public function rewind() |
||
89 | } |
||
90 |
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.