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 |
||
| 10 | final class EventStreamViaPersistentSubscriptionFeedIterator implements \Iterator |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var StreamId |
||
| 14 | */ |
||
| 15 | private $streamId; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var HttpClient |
||
| 19 | */ |
||
| 20 | private $client; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var EventStreamViaPersistentSubscriptionFeed |
||
| 24 | */ |
||
| 25 | private $currentFeed; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $headUri; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | private $initialized = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | private $currentKey = 0; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $groupName; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | private $batchSize; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param StreamId $streamId |
||
| 54 | * @param string $groupName |
||
| 55 | * @param HttpClient $client |
||
| 56 | * @param int $batchSize |
||
| 57 | */ |
||
| 58 | public function __construct(StreamId $streamId, string $groupName, HttpClient $client, int $batchSize = 1) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $uri |
||
| 69 | * |
||
| 70 | * @return EventStreamViaPersistentSubscriptionFeed |
||
| 71 | */ |
||
| 72 | private function readEventStreamFeed(string $uri): EventStreamViaPersistentSubscriptionFeed |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return EventStreamViaPersistentSubscriptionFeed |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function current(): EventStreamViaPersistentSubscriptionFeed |
|
| 96 | |||
| 97 | public function next() |
||
| 102 | |||
| 103 | View Code Duplication | private function readForward() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @return int |
||
| 115 | */ |
||
| 116 | public function key(): int |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | public function valid(): bool |
||
| 128 | |||
| 129 | public function rewind() |
||
| 135 | } |
||
| 136 |
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.