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 | final class EventStreamViaPersistentSubscriptionFeed |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | private $events; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | private $links = []; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var bool |
||
| 21 | */ |
||
| 22 | private $isHeadOfStream; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param array $events |
||
| 26 | * @param array $links |
||
| 27 | * @param bool $isHeadOfStream |
||
| 28 | */ |
||
| 29 | View Code Duplication | public function __construct(array $events, array $links, bool $isHeadOfStream) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @param array $events |
||
| 43 | * |
||
| 44 | * @return mixed |
||
| 45 | */ |
||
| 46 | View Code Duplication | private function validateEvents(array $events) |
|
| 54 | |||
| 55 | /** |
||
| 56 | * @param $link |
||
| 57 | */ |
||
| 58 | View Code Duplication | private function validateLink($link) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @return bool |
||
| 70 | */ |
||
| 71 | public function hasPreviousLink(): bool |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return EventStreamViaPersistentSubscriptionFeedLink |
||
| 78 | */ |
||
| 79 | public function getPreviousLink(): EventStreamViaPersistentSubscriptionFeedLink |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | public function hasNextLink(): bool |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return EventStreamViaPersistentSubscriptionFeedLink |
||
| 94 | */ |
||
| 95 | public function getNextLink(): EventStreamViaPersistentSubscriptionFeedLink |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | public function hasLastLink(): bool |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return EventStreamViaPersistentSubscriptionFeedLink |
||
| 110 | */ |
||
| 111 | public function getLastLink(): EventStreamViaPersistentSubscriptionFeedLink |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return EventStreamViaPersistentSubscriptionFeedLink |
||
| 118 | */ |
||
| 119 | public function getAckAllLink(): EventStreamViaPersistentSubscriptionFeedLink |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return EventStreamViaPersistentSubscriptionFeedLink |
||
| 126 | */ |
||
| 127 | public function getNackAllLink(): EventStreamViaPersistentSubscriptionFeedLink |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | public function getEvents(): array |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | public function isHeadOfStream(): bool |
||
| 147 | } |
||
| 148 |
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.