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:
Complex classes like GuiHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GuiHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class GuiHandler implements TimerDataListenerInterface, UserGroupDataListenerInterface |
||
| 21 | { |
||
| 22 | /** @var Connection */ |
||
| 23 | protected $connection; |
||
| 24 | |||
| 25 | /** @var Logger */ |
||
| 26 | protected $logger; |
||
| 27 | |||
| 28 | /** @var Console */ |
||
| 29 | protected $console; |
||
| 30 | |||
| 31 | /** @var int */ |
||
| 32 | protected $charLimit; |
||
| 33 | |||
| 34 | /** @var ManialinkInterface[][] */ |
||
| 35 | protected $displayQueu = []; |
||
| 36 | |||
| 37 | /** @var ManialinkInterface[][] */ |
||
| 38 | protected $individualQueu = []; |
||
| 39 | |||
| 40 | /** @var ManialinkInterface[][] */ |
||
| 41 | protected $displayeds = []; |
||
| 42 | |||
| 43 | /** @var ManialinkInterface[][] */ |
||
| 44 | protected $hideQueu = []; |
||
| 45 | |||
| 46 | /** @var String[][] */ |
||
| 47 | protected $hideIndividualQueu = []; |
||
| 48 | |||
| 49 | private $groupsBuffer = []; |
||
| 50 | private $windowsBuffer = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * GuiHandler constructor. |
||
| 54 | 10 | * |
|
| 55 | * @param Connection $connection |
||
| 56 | 10 | */ |
|
| 57 | public function __construct(Connection $connection, Logger $logger, Console $console, $charLimit = 262144) |
||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * Add a manialink to the display queue. |
||
| 71 | 8 | * |
|
| 72 | * @param ManialinkInterface $manialink |
||
| 73 | 8 | * |
|
| 74 | * @return void |
||
| 75 | 8 | */ |
|
| 76 | 1 | public function addToDisplay(ManialinkInterface $manialink) |
|
| 86 | |||
| 87 | 3 | /** |
|
| 88 | * Add a manialink to the destruction queue. |
||
| 89 | 3 | * |
|
| 90 | * @param ManialinkInterface $manialink |
||
| 91 | 3 | */ |
|
| 92 | 1 | public function addToHide(ManialinkInterface $manialink) |
|
| 106 | |||
| 107 | 9 | /** |
|
| 108 | 9 | * Display & hide all manialinks. |
|
| 109 | 9 | */ |
|
| 110 | 9 | protected function displayManialinks() |
|
| 141 | |||
| 142 | 9 | /** |
|
| 143 | * Execute multi call & handle error. |
||
| 144 | */ |
||
| 145 | protected function executeMultiCall() |
||
| 154 | |||
| 155 | 8 | /** |
|
| 156 | 8 | * Get list of all manialinks that needs to be displayed |
|
| 157 | 8 | * |
|
| 158 | * @return \Generator |
||
| 159 | */ |
||
| 160 | protected function getManialinksToDisplay() |
||
| 195 | |||
| 196 | 1 | /** |
|
| 197 | * @inheritdoc |
||
| 198 | 1 | */ |
|
| 199 | public function onPostLoop() |
||
| 203 | 1 | ||
| 204 | /** |
||
| 205 | 1 | * @inheritdoc |
|
| 206 | */ |
||
| 207 | public function onPreLoop() |
||
| 210 | 1 | ||
| 211 | /** |
||
| 212 | 1 | * @inheritdoc |
|
| 213 | */ |
||
| 214 | public function onEverySecond() |
||
| 239 | |||
| 240 | 1 | /** |
|
| 241 | * @inheritdoc |
||
| 242 | 1 | */ |
|
| 243 | 1 | public function onExpansionGroupAddUser(Group $group, $loginAdded) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @inheritdoc |
||
| 259 | */ |
||
| 260 | 2 | public function onExpansionGroupRemoveUser(Group $group, $loginRemoved) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @inheritdoc |
||
| 274 | */ |
||
| 275 | public function onExpansionGroupDestroy(Group $group, $lastLogin) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * List of all manialinks that are currently displayed. |
||
| 284 | * |
||
| 285 | * @return ManialinkInterface[][] |
||
| 286 | */ |
||
| 287 | public function getDisplayeds() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param int $charLimit |
||
| 294 | */ |
||
| 295 | public function setCharLimit($charLimit) |
||
| 299 | } |
||
| 300 |
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.