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 |
||
| 25 | class GuiHandler implements |
||
| 26 | ListenerInterfaceExpTimer, |
||
| 27 | ListenerInterfaceExpUserGroup, |
||
| 28 | ListenerInterfaceMpLegacyPlayer, |
||
| 29 | GuiHandlerInterface |
||
| 30 | { |
||
| 31 | /** @var Connection */ |
||
| 32 | protected $connection; |
||
| 33 | |||
| 34 | /** @var LoggerInterface */ |
||
| 35 | protected $logger; |
||
| 36 | |||
| 37 | /** @var Console */ |
||
| 38 | protected $console; |
||
| 39 | |||
| 40 | /** @var ActionFactory */ |
||
| 41 | protected $actionFactory; |
||
| 42 | |||
| 43 | /** @var int */ |
||
| 44 | protected $charLimit; |
||
| 45 | |||
| 46 | /** @var ManialinkInterface[][] */ |
||
| 47 | protected $displayQueu = []; |
||
| 48 | |||
| 49 | /** @var ManialinkInterface[][] */ |
||
| 50 | protected $individualQueu = []; |
||
| 51 | |||
| 52 | /** @var ManialinkInterface[][] */ |
||
| 53 | protected $displayeds = []; |
||
| 54 | |||
| 55 | /** @var ManialinkInterface[][] */ |
||
| 56 | protected $hideQueu = []; |
||
| 57 | |||
| 58 | /** @var String[][] */ |
||
| 59 | protected $hideIndividualQueu = []; |
||
| 60 | |||
| 61 | /** @var String[] */ |
||
| 62 | protected $disconnectedLogins = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * GuiHandler constructor. |
||
| 66 | * |
||
| 67 | * @param Connection $connection |
||
| 68 | */ |
||
| 69 | 11 | public function __construct( |
|
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Add a manialink to the display queue. |
||
| 89 | * |
||
| 90 | * @param ManialinkInterface $manialink |
||
| 91 | * @param ManialinkFactory $manialinkFactory |
||
| 92 | * |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | 9 | public function addToDisplay(ManialinkInterface $manialink, ManialinkFactoryInterface $manialinkFactory) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Add a manialink to the destruction queue. |
||
| 110 | * |
||
| 111 | * @param ManialinkInterface $manialink |
||
| 112 | * @param ManialinkFactory $manialinkFactory |
||
| 113 | */ |
||
| 114 | 3 | public function addToHide(ManialinkInterface $manialink, ManialinkFactoryInterface $manialinkFactory) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Get manialink for a group and manialink factory. |
||
| 133 | * |
||
| 134 | * @param Group $group |
||
| 135 | * @param ManialinkFactory $manialinkFactory |
||
| 136 | * |
||
| 137 | * @return null |
||
| 138 | */ |
||
| 139 | public function getManialink(Group $group, ManialinkFactoryInterface $manialinkFactory) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Display & hide all manialinks. |
||
| 154 | */ |
||
| 155 | 10 | protected function displayManialinks() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Execute multi call & handle error. |
||
| 190 | */ |
||
| 191 | 10 | protected function executeMultiCall() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Get list of all manialinks that needs to be displayed |
||
| 203 | * |
||
| 204 | * @return \Generator |
||
| 205 | */ |
||
| 206 | 10 | protected function getManialinksToDisplay() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * @param int $charLimit |
||
| 252 | */ |
||
| 253 | 2 | public function setCharLimit($charLimit) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * List of all manialinks that are currently displayed. |
||
| 260 | * |
||
| 261 | * @return ManialinkInterface[][] |
||
| 262 | */ |
||
| 263 | 1 | public function getDisplayeds() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @inheritdoc |
||
| 270 | */ |
||
| 271 | 10 | public function onPostLoop() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @inheritdoc |
||
| 278 | */ |
||
| 279 | 1 | public function onPreLoop() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * @inheritdoc |
||
| 285 | */ |
||
| 286 | 1 | public function onEverySecond() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @inheritdoc |
||
| 292 | */ |
||
| 293 | 1 | View Code Duplication | public function onExpansionGroupAddUser(Group $group, $loginAdded) |
| 304 | |||
| 305 | /** |
||
| 306 | * @inheritdoc |
||
| 307 | */ |
||
| 308 | 2 | View Code Duplication | public function onExpansionGroupRemoveUser(Group $group, $loginRemoved) |
| 319 | |||
| 320 | /** |
||
| 321 | * @inheritdoc |
||
| 322 | */ |
||
| 323 | 1 | public function onExpansionGroupDestroy(Group $group, $lastLogin) |
|
| 329 | |||
| 330 | 1 | public function onPlayerConnect(Player $player) |
|
| 333 | |||
| 334 | 1 | public function onPlayerDisconnect(Player $player, $disconnectionReason) |
|
| 338 | |||
| 339 | 1 | public function onPlayerInfoChanged(Player $oldPlayer, Player $player) |
|
| 342 | |||
| 343 | 1 | public function onPlayerAlliesChanged(Player $oldPlayer, Player $player) |
|
| 346 | } |
||
| 347 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.