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 |
||
| 21 | class GuiHandler implements TimerDataListenerInterface, UserGroupDataListenerInterface |
||
| 22 | { |
||
| 23 | /** @var Connection */ |
||
| 24 | protected $connection; |
||
| 25 | |||
| 26 | /** @var Logger */ |
||
| 27 | protected $logger; |
||
| 28 | |||
| 29 | /** @var Console */ |
||
| 30 | protected $console; |
||
| 31 | |||
| 32 | /** @var int */ |
||
| 33 | protected $charLimit; |
||
| 34 | |||
| 35 | /** @var ManialinkInerface[][] */ |
||
| 36 | protected $displayQueu = []; |
||
| 37 | |||
| 38 | /** @var ManialinkInerface[][] */ |
||
| 39 | protected $individualQueu = []; |
||
| 40 | |||
| 41 | /** @var ManialinkInerface[][] */ |
||
| 42 | protected $displayeds = []; |
||
| 43 | |||
| 44 | /** @var ManialinkInerface[][] */ |
||
| 45 | protected $hideQueu = []; |
||
| 46 | |||
| 47 | /** @var String[][] */ |
||
| 48 | protected $hideIndividualQueu = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * GuiHandler constructor. |
||
| 52 | * |
||
| 53 | * @param Connection $connection |
||
| 54 | */ |
||
| 55 | 10 | public function __construct(Connection $connection, Logger $logger, Console $console, $charLimit = 262144) |
|
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Add a manialink to the diplay queue. |
||
| 69 | * |
||
| 70 | * @param ManialinkInerface $manialink |
||
| 71 | */ |
||
| 72 | 8 | public function addToDisplay(ManialinkInerface $manialink) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Add a manialink to the destruction queue. |
||
| 85 | * |
||
| 86 | * @param ManialinkInerface $manialink |
||
| 87 | */ |
||
| 88 | 3 | public function addToHide(ManialinkInerface $manialink) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Display & hide all manialinks. |
||
| 105 | */ |
||
| 106 | 9 | protected function displayManialinks() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Execute multicall & handle error. |
||
| 134 | */ |
||
| 135 | 9 | protected function executeMultiCall() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Get list of all manialinks that needs to be displayed |
||
| 147 | * |
||
| 148 | * @return \Generator |
||
| 149 | */ |
||
| 150 | 9 | protected function getManialinksToDisplay() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @inheritdoc |
||
| 187 | */ |
||
| 188 | 9 | public function onPostLoop() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @inheritdoc |
||
| 195 | */ |
||
| 196 | 1 | public function onPreLoop() |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @inheritdoc |
||
| 202 | */ |
||
| 203 | 1 | public function onEverySecond() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @inheritdoc |
||
| 209 | */ |
||
| 210 | 1 | View Code Duplication | public function onExpansionGroupAddUser(Group $group, $loginAdded) |
| 221 | |||
| 222 | /** |
||
| 223 | * @inheritdoc |
||
| 224 | */ |
||
| 225 | 1 | View Code Duplication | public function onExpansionGroupRemoveUser(Group $group, $loginRemoved) |
| 236 | |||
| 237 | /** |
||
| 238 | * @inheritdoc |
||
| 239 | */ |
||
| 240 | 1 | public function onExpansionGroupDestroy(Group $group, $lastLogin) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * List of all manialinks that are currentyl displayed. |
||
| 249 | * |
||
| 250 | * @return \eXpansion\Core\Model\Gui\ManialinkInerface[][] |
||
| 251 | */ |
||
| 252 | 1 | public function getDisplayeds() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @param int $charLimit |
||
| 259 | */ |
||
| 260 | 2 | public function setCharLimit($charLimit) |
|
| 264 | } |
||
| 265 |
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.