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 |
||
26 | class GuiHandler implements |
||
27 | ListenerInterfaceExpTimer, |
||
28 | ListenerInterfaceExpUserGroup, |
||
29 | ListenerInterfaceMpLegacyPlayer, |
||
30 | StatusAwarePluginInterface, |
||
31 | GuiHandlerInterface |
||
32 | { |
||
33 | /** @var Factory */ |
||
34 | protected $factory; |
||
35 | |||
36 | /** @var LoggerInterface */ |
||
37 | protected $logger; |
||
38 | |||
39 | /** @var Console */ |
||
40 | protected $console; |
||
41 | |||
42 | /** @var ActionFactory */ |
||
43 | protected $actionFactory; |
||
44 | |||
45 | /** @var int */ |
||
46 | protected $charLimit; |
||
47 | |||
48 | /** @var ManialinkInterface[][] */ |
||
49 | protected $displayQueu = []; |
||
50 | |||
51 | /** @var ManialinkInterface[][] */ |
||
52 | protected $individualQueu = []; |
||
53 | |||
54 | /** @var ManialinkInterface[][] */ |
||
55 | protected $displayeds = []; |
||
56 | |||
57 | /** @var ManialinkInterface[][] */ |
||
58 | protected $hideQueu = []; |
||
59 | |||
60 | /** @var ManialinkInterface[][] */ |
||
61 | protected $hideIndividualQueu = []; |
||
62 | |||
63 | /** @var String[] */ |
||
64 | protected $disconnectedLogins = []; |
||
65 | |||
66 | /** |
||
67 | * GuiHandler constructor. |
||
68 | * |
||
69 | * @param Factory $factory |
||
70 | * @param LoggerInterface $logger |
||
71 | * @param Console $console |
||
72 | * @param ActionFactory $actionFactory |
||
73 | * @param int $charLimit |
||
74 | */ |
||
75 | 15 | View Code Duplication | public function __construct( |
88 | |||
89 | /** |
||
90 | * @inheritdoc |
||
91 | */ |
||
92 | public function setStatus($status) |
||
98 | |||
99 | |||
100 | /** |
||
101 | * @inheritdoc |
||
102 | **/ |
||
103 | 13 | public function addToDisplay(ManialinkInterface $manialink) |
|
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | 3 | public function addToHide(ManialinkInterface $manialink) |
|
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | public function getManialink(Group $group, ManialinkFactoryInterface $manialinkFactory) |
||
151 | |||
152 | /** |
||
153 | * @inheritdoc |
||
154 | */ |
||
155 | public function getFactoryManialinks(ManialinkFactory $manialinkFactory) |
||
172 | |||
173 | /** |
||
174 | * Display & hide all manialinks. |
||
175 | * @throws \Maniaplanet\DedicatedServer\InvalidArgumentException |
||
176 | */ |
||
177 | 14 | protected function displayManialinks() |
|
215 | |||
216 | /** |
||
217 | * Execute multi call & handle error. |
||
218 | */ |
||
219 | 14 | View Code Duplication | protected function executeMultiCall() |
228 | |||
229 | /** |
||
230 | * Get list of all manialinks that needs to be displayed |
||
231 | * |
||
232 | * @return \Generator |
||
233 | */ |
||
234 | 14 | protected function getManialinksToDisplay() |
|
293 | |||
294 | /** |
||
295 | * @param int $charLimit |
||
296 | */ |
||
297 | 2 | public function setCharLimit($charLimit) |
|
301 | |||
302 | /** |
||
303 | * List of all manialinks that are currently displayed. |
||
304 | * |
||
305 | * @return ManialinkInterface[][] |
||
306 | */ |
||
307 | 1 | public function getDisplayeds() |
|
311 | |||
312 | /** |
||
313 | * @inheritdoc |
||
314 | */ |
||
315 | 14 | public function onPostLoop() |
|
319 | |||
320 | /** |
||
321 | * @inheritdoc |
||
322 | */ |
||
323 | 1 | public function onPreLoop() |
|
326 | |||
327 | /** |
||
328 | * @inheritdoc |
||
329 | */ |
||
330 | 1 | public function onEverySecond() |
|
333 | |||
334 | /** |
||
335 | * @inheritdoc |
||
336 | */ |
||
337 | 4 | View Code Duplication | public function onExpansionGroupAddUser(Group $group, $loginAdded) |
352 | |||
353 | /** |
||
354 | * @inheritdoc |
||
355 | */ |
||
356 | 5 | View Code Duplication | public function onExpansionGroupRemoveUser(Group $group, $loginRemoved) |
371 | |||
372 | /** |
||
373 | * @inheritdoc |
||
374 | */ |
||
375 | 1 | public function onExpansionGroupDestroy(Group $group, $lastLogin) |
|
381 | |||
382 | /** |
||
383 | * @inheritdoc |
||
384 | */ |
||
385 | 1 | public function onPlayerConnect(Player $player) |
|
388 | |||
389 | /** |
||
390 | * @inheritdoc |
||
391 | */ |
||
392 | 1 | public function onPlayerDisconnect(Player $player, $disconnectionReason) |
|
397 | |||
398 | /** |
||
399 | * @inheritdoc |
||
400 | */ |
||
401 | 1 | public function onPlayerInfoChanged(Player $oldPlayer, Player $player) |
|
404 | |||
405 | /** |
||
406 | * @inheritdoc |
||
407 | */ |
||
408 | 1 | public function onPlayerAlliesChanged(Player $oldPlayer, Player $player) |
|
411 | } |
||
412 |
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.