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 |
||
| 13 | class VoteService |
||
| 14 | { |
||
| 15 | /** @var Console */ |
||
| 16 | protected $console; |
||
| 17 | |||
| 18 | /** @var Connection */ |
||
| 19 | protected $connection; |
||
| 20 | |||
| 21 | /** @var ChatNotification */ |
||
| 22 | protected $chatNotification; |
||
| 23 | |||
| 24 | /** @var Dispatcher */ |
||
| 25 | protected $dispatcher; |
||
| 26 | |||
| 27 | /** @var AbstractVotePlugin[] */ |
||
| 28 | protected $votePlugins = []; |
||
| 29 | |||
| 30 | /** @var array mapping between native MP votes and equivalent expansion votes. */ |
||
| 31 | protected $voteMapping = []; |
||
| 32 | |||
| 33 | /** @var AbstractVotePlugin */ |
||
| 34 | protected $currentVote = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * VoteManager constructor. |
||
| 38 | * @param Console $console |
||
| 39 | * @param Connection $connection |
||
| 40 | * @param ChatNotification $chatNotification |
||
| 41 | * @param Dispatcher $dispatcher |
||
| 42 | * @param AbstractVotePlugin[] $voteFactories |
||
| 43 | */ |
||
| 44 | 10 | public function __construct( |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Reset ongoing vote. |
||
| 67 | */ |
||
| 68 | 4 | public function reset() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Cast vote |
||
| 78 | * |
||
| 79 | * @param string $login |
||
| 80 | * @param string $type |
||
| 81 | */ |
||
| 82 | 2 | public function castVote($login, $type) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Update the status of the vote. |
||
| 98 | */ |
||
| 99 | 4 | public function update() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Cancel ongoing vote. |
||
| 130 | */ |
||
| 131 | 1 | public function cancel() |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Pass ongoing vote. |
||
| 143 | */ |
||
| 144 | public function pass() |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * @return AbstractVotePlugin |
||
| 157 | */ |
||
| 158 | 10 | public function getCurrentVote() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Start a vote. |
||
| 165 | * |
||
| 166 | * @param Player $player |
||
| 167 | * @param string $typeCode |
||
| 168 | * @param array $params |
||
| 169 | */ |
||
| 170 | 10 | public function startVote(Player $player, $typeCode, $params) |
|
| 195 | } |
||
| 196 |
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.