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 ManialinkInterface[][] */ |
||
| 59 | protected $hideIndividualQueu = []; |
||
| 60 | |||
| 61 | /** @var String[] */ |
||
| 62 | protected $disconnectedLogins = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * GuiHandler constructor. |
||
| 66 | * |
||
| 67 | * @param Connection $connection |
||
| 68 | */ |
||
| 69 | 15 | public function __construct( |
|
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * @inheritdoc |
||
| 89 | **/ |
||
| 90 | 13 | public function addToDisplay(ManialinkInterface $manialink) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @inheritdoc |
||
| 105 | */ |
||
| 106 | 3 | public function addToHide(ManialinkInterface $manialink) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @inheritdoc |
||
| 125 | */ |
||
| 126 | public function getManialink(Group $group, ManialinkFactoryInterface $manialinkFactory) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Display & hide all manialinks. |
||
| 141 | */ |
||
| 142 | 14 | protected function displayManialinks() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Execute multi call & handle error. |
||
| 177 | */ |
||
| 178 | 14 | protected function executeMultiCall() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Get list of all manialinks that needs to be displayed |
||
| 190 | * |
||
| 191 | * @return \Generator |
||
| 192 | */ |
||
| 193 | 14 | protected function getManialinksToDisplay() |
|
| 252 | |||
| 253 | /** |
||
| 254 | * @param int $charLimit |
||
| 255 | */ |
||
| 256 | 2 | public function setCharLimit($charLimit) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * List of all manialinks that are currently displayed. |
||
| 263 | * |
||
| 264 | * @return ManialinkInterface[][] |
||
| 265 | */ |
||
| 266 | 1 | public function getDisplayeds() |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @inheritdoc |
||
| 273 | */ |
||
| 274 | 14 | public function onPostLoop() |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @inheritdoc |
||
| 281 | */ |
||
| 282 | 1 | public function onPreLoop() |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @inheritdoc |
||
| 288 | */ |
||
| 289 | 1 | public function onEverySecond() |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @inheritdoc |
||
| 295 | */ |
||
| 296 | 4 | View Code Duplication | public function onExpansionGroupAddUser(Group $group, $loginAdded) |
| 311 | |||
| 312 | /** |
||
| 313 | * @inheritdoc |
||
| 314 | */ |
||
| 315 | 5 | View Code Duplication | public function onExpansionGroupRemoveUser(Group $group, $loginRemoved) |
| 330 | |||
| 331 | /** |
||
| 332 | * @inheritdoc |
||
| 333 | */ |
||
| 334 | 1 | public function onExpansionGroupDestroy(Group $group, $lastLogin) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @inheritdoc |
||
| 343 | */ |
||
| 344 | 1 | public function onPlayerConnect(Player $player) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @inheritdoc |
||
| 350 | */ |
||
| 351 | 1 | public function onPlayerDisconnect(Player $player, $disconnectionReason) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @inheritdoc |
||
| 359 | */ |
||
| 360 | 1 | public function onPlayerInfoChanged(Player $oldPlayer, Player $player) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * @inheritdoc |
||
| 366 | */ |
||
| 367 | 1 | public function onPlayerAlliesChanged(Player $oldPlayer, Player $player) |
|
| 370 | } |
||
| 371 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: