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 |
||
| 16 | class VoteService implements ListenerInterfaceMpLegacyVote, ListenerInterfaceExpTimer, ListenerInterfaceMpScriptMap |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var Connection |
||
| 20 | */ |
||
| 21 | public $connection; |
||
| 22 | |||
| 23 | /** @var Console */ |
||
| 24 | public $console; |
||
| 25 | |||
| 26 | public $removeVote = false; |
||
| 27 | |||
| 28 | /** @var null|Vote */ |
||
| 29 | private $currentVote = null; |
||
| 30 | |||
| 31 | /** @var array */ |
||
| 32 | private $votesStarted = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Dispatcher |
||
| 36 | */ |
||
| 37 | private $dispatcher; |
||
| 38 | /** |
||
| 39 | * @var ChatNotification |
||
| 40 | */ |
||
| 41 | private $chatNotification; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * VoteManager constructor. |
||
| 45 | * @param Console $console |
||
| 46 | * @param Connection $connection |
||
| 47 | * @param ChatNotification $chatNotification |
||
| 48 | * @param Dispatcher $dispatcher |
||
| 49 | */ |
||
| 50 | public function __construct( |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * When a new vote is addressed |
||
| 65 | * |
||
| 66 | * @param Player $player |
||
| 67 | * @param string $cmdName |
||
| 68 | * @param string $cmdValue |
||
| 69 | * |
||
| 70 | * @return void |
||
| 71 | */ |
||
| 72 | public function onVoteNew(Player $player, $cmdName, $cmdValue) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * When vote gets cancelled |
||
| 90 | * |
||
| 91 | * @param Player $player |
||
| 92 | * @param string $cmdName |
||
| 93 | * @param string $cmdValue |
||
| 94 | * |
||
| 95 | * @return void |
||
| 96 | */ |
||
| 97 | public function onVoteCancelled(Player $player, $cmdName, $cmdValue) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * When vote Passes |
||
| 106 | * @param Player $player |
||
| 107 | * @param string $cmdName |
||
| 108 | * @param string $cmdValue |
||
| 109 | * |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | public function onVotePassed(Player $player, $cmdName, $cmdValue) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * When vote Fails |
||
| 121 | * @param Player $player |
||
| 122 | * @param string $cmdName |
||
| 123 | * @param string $cmdValue |
||
| 124 | * |
||
| 125 | * @return void |
||
| 126 | */ |
||
| 127 | public function onVoteFailed(Player $player, $cmdName, $cmdValue) |
||
| 133 | |||
| 134 | public function onPreLoop() |
||
| 138 | |||
| 139 | public function onPostLoop() |
||
| 143 | |||
| 144 | public function updateVote($login, $type) |
||
| 157 | |||
| 158 | public function onEverySecond() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return Vote |
||
| 184 | */ |
||
| 185 | public function getCurrentVote() |
||
| 189 | |||
| 190 | public function startVote(Player $player, $type) |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Callback sent when the "StartMap" section start. |
||
| 211 | * |
||
| 212 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 213 | * @param int $time Server time when the callback was sent |
||
| 214 | * @param boolean $restarted true if the map was restarted, false otherwise |
||
| 215 | * @param Map $map Map started with. |
||
| 216 | * |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | public function onStartMapStart($count, $time, $restarted, Map $map) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Callback sent when the "StartMap" section end. |
||
| 226 | * |
||
| 227 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 228 | * @param int $time Server time when the callback was sent |
||
| 229 | * @param boolean $restarted true if the map was restarted, false otherwise |
||
| 230 | * @param Map $map Map started with. |
||
| 231 | * |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | public function onStartMapEnd($count, $time, $restarted, Map $map) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Callback sent when the "EndMap" section start. |
||
| 241 | * |
||
| 242 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 243 | * @param int $time Server time when the callback was sent |
||
| 244 | * @param boolean $restarted true if the map was restarted, false otherwise |
||
| 245 | * @param Map $map Map started with. |
||
| 246 | * |
||
| 247 | * @return void |
||
| 248 | */ |
||
| 249 | public function onEndMapStart($count, $time, $restarted, Map $map) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Callback sent when the "EndMap" section end. |
||
| 256 | * |
||
| 257 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 258 | * @param int $time Server time when the callback was sent |
||
| 259 | * @param boolean $restarted true if the map was restarted, false otherwise |
||
| 260 | * @param Map $map Map started with. |
||
| 261 | * |
||
| 262 | * @return void |
||
| 263 | */ |
||
| 264 | public function onEndMapEnd($count, $time, $restarted, Map $map) |
||
| 268 | } |
||
| 269 |
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.