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 |
||
| 17 | abstract class AbstractVotePlugin |
||
| 18 | { |
||
| 19 | /** @var DispatcherInterface */ |
||
| 20 | protected $dispatcher; |
||
| 21 | |||
| 22 | /** @var PlayerStorage */ |
||
| 23 | protected $playerStorage; |
||
| 24 | |||
| 25 | /** @var int */ |
||
| 26 | protected $duration; |
||
| 27 | |||
| 28 | /** @var float */ |
||
| 29 | protected $ratio; |
||
| 30 | |||
| 31 | /** @var Vote|null */ |
||
| 32 | protected $currentVote = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * AbstractVotePlugin constructor. |
||
| 36 | * |
||
| 37 | * @param DispatcherInterface $dispatcher |
||
| 38 | * @param PlayerStorage $playerStorage |
||
| 39 | * @param int $duration |
||
| 40 | * @param float $ratio |
||
| 41 | */ |
||
| 42 | 10 | public function __construct( |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Start a new vote. |
||
| 56 | * |
||
| 57 | * @param Player $player |
||
| 58 | * |
||
| 59 | * @return Vote|null |
||
| 60 | */ |
||
| 61 | 7 | public function start(Player $player, $params) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Reset current vote session. |
||
| 69 | */ |
||
| 70 | 1 | public function reset() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * User votes yes |
||
| 77 | * |
||
| 78 | * @param string $login |
||
| 79 | */ |
||
| 80 | 5 | View Code Duplication | public function castYes($login) |
| 89 | |||
| 90 | /** |
||
| 91 | * User votes no |
||
| 92 | * |
||
| 93 | * @param string $login |
||
| 94 | */ |
||
| 95 | 3 | View Code Duplication | public function castNo($login) |
| 104 | |||
| 105 | /** |
||
| 106 | * Cancel ongoing vote. |
||
| 107 | */ |
||
| 108 | 1 | public function cancel() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Update the status of the vote, and execute actions if vote passed. |
||
| 119 | * |
||
| 120 | * @param int $time |
||
| 121 | */ |
||
| 122 | 8 | public function update($time = null) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @return Vote|null |
||
| 154 | */ |
||
| 155 | 6 | public function getCurrentVote() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Pass a vote |
||
| 162 | */ |
||
| 163 | public function pass() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Called when a vote passed. |
||
| 171 | */ |
||
| 172 | 3 | protected function votePassed() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Called when a vote failed. |
||
| 180 | */ |
||
| 181 | 2 | protected function voteFailed() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @return int |
||
| 189 | */ |
||
| 190 | public function getDuration(): int |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return float |
||
| 197 | */ |
||
| 198 | public function getRatio(): float |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return int |
||
| 205 | */ |
||
| 206 | public function getElapsedTime() : int |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get question text to display for this vote. |
||
| 213 | * |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | abstract public function getQuestion(): string; |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get type code of this vote. |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | abstract public function getCode(): string; |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get native votes this votes replaces. |
||
| 227 | * |
||
| 228 | * @return string[] |
||
| 229 | */ |
||
| 230 | abstract public function getReplacementTypes(): array; |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Called when vote is passed. |
||
| 234 | * |
||
| 235 | * @return void |
||
| 236 | */ |
||
| 237 | abstract protected function executeVotePassed(); |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Called when vote is failed. |
||
| 241 | */ |
||
| 242 | abstract protected function executeVoteFailed(); |
||
| 243 | } |
||
| 244 |
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.