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 Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | 1 | final class Client implements IClient |
|
| 55 | { |
||
| 56 | /** |
||
| 57 | * Implement nette smart magic |
||
| 58 | */ |
||
| 59 | 1 | use Nette\SmartObject; |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @var \Closure |
||
| 63 | */ |
||
| 64 | public $onStart = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var \Closure |
||
| 68 | */ |
||
| 69 | public $onOpen = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var \Closure |
||
| 73 | */ |
||
| 74 | public $onConnect = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var \Closure |
||
| 78 | */ |
||
| 79 | public $onDisconnect = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var \Closure |
||
| 83 | */ |
||
| 84 | public $onClose = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var \Closure |
||
| 88 | */ |
||
| 89 | public $onPing = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var \Closure |
||
| 93 | */ |
||
| 94 | public $onPong = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var \Closure |
||
| 98 | */ |
||
| 99 | public $onPublish = []; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var \Closure |
||
| 103 | */ |
||
| 104 | public $onSubscribe = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var \Closure |
||
| 108 | */ |
||
| 109 | public $onUnsubscribe = []; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var \Closure |
||
| 113 | */ |
||
| 114 | public $onMessage = []; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var \Closure |
||
| 118 | */ |
||
| 119 | public $onWarning = []; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var \Closure |
||
| 123 | */ |
||
| 124 | public $onError = []; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var EventLoop\LoopInterface |
||
| 128 | */ |
||
| 129 | private $loop; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var Configuration |
||
| 133 | */ |
||
| 134 | private $configuration; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var Socket\ConnectorInterface |
||
| 138 | */ |
||
| 139 | private $connector; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var Socket\ConnectionInterface|NULL |
||
| 143 | */ |
||
| 144 | private $stream = NULL; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var Mqtt\Connection |
||
| 148 | */ |
||
| 149 | private $connection; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var Mqtt\StreamParser |
||
| 153 | */ |
||
| 154 | private $parser; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var Mqtt\IdentifierGenerator |
||
| 158 | */ |
||
| 159 | private $identifierGenerator; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @var bool |
||
| 163 | */ |
||
| 164 | private $isConnected = FALSE; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @var bool |
||
| 168 | */ |
||
| 169 | private $isConnecting = FALSE; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @var bool |
||
| 173 | */ |
||
| 174 | private $isDisconnecting = FALSE; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @var string |
||
| 178 | */ |
||
| 179 | private $host; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @var int |
||
| 183 | */ |
||
| 184 | private $port; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @var int |
||
| 188 | */ |
||
| 189 | private $timeout = 5; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @var Flow\Envelope[] |
||
| 193 | */ |
||
| 194 | private $receivingFlows = []; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @var Flow\Envelope[] |
||
| 198 | */ |
||
| 199 | private $sendingFlows = []; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @var Flow\Envelope |
||
| 203 | */ |
||
| 204 | private $writtenFlow; |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @var EventLoop\Timer\TimerInterface[] |
||
| 208 | */ |
||
| 209 | private $timer = []; |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @var Mqtt\Message|NULL |
||
| 213 | */ |
||
| 214 | private $lastMessage = NULL; |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param EventLoop\LoopInterface $eventLoop |
||
| 218 | * @param Configuration $configuration |
||
| 219 | * @param Mqtt\IdentifierGenerator|NULL $identifierGenerator |
||
| 220 | * @param Mqtt\StreamParser|NULL $parser |
||
| 221 | */ |
||
| 222 | public function __construct( |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | public function setLoop(EventLoop\LoopInterface $loop) : void |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function getLoop() : EventLoop\LoopInterface |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | public function setConfiguration(Configuration $configuration) : void |
||
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | public function getUri() : string |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritdoc} |
||
| 295 | */ |
||
| 296 | public function getPort() : int |
||
| 300 | |||
| 301 | /** |
||
| 302 | * {@inheritdoc} |
||
| 303 | */ |
||
| 304 | public function isConnected() : bool |
||
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritdoc} |
||
| 311 | */ |
||
| 312 | public function connect() : Promise\ExtendedPromiseInterface |
||
| 372 | |||
| 373 | /** |
||
| 374 | * {@inheritdoc} |
||
| 375 | */ |
||
| 376 | public function disconnect() : Promise\ExtendedPromiseInterface |
||
| 406 | |||
| 407 | /** |
||
| 408 | * {@inheritdoc} |
||
| 409 | */ |
||
| 410 | View Code Duplication | public function subscribe(Mqtt\Subscription $subscription) : Promise\ExtendedPromiseInterface |
|
| 418 | |||
| 419 | /** |
||
| 420 | * {@inheritdoc} |
||
| 421 | */ |
||
| 422 | View Code Duplication | public function unsubscribe(Mqtt\Subscription $subscription) : Promise\ExtendedPromiseInterface |
|
| 430 | |||
| 431 | /** |
||
| 432 | * {@inheritdoc} |
||
| 433 | */ |
||
| 434 | public function publish(Mqtt\Message $message) : Promise\ExtendedPromiseInterface |
||
| 442 | |||
| 443 | /** |
||
| 444 | * {@inheritdoc} |
||
| 445 | */ |
||
| 446 | public function publishPeriodically( |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Establishes a network connection to a server |
||
| 474 | * |
||
| 475 | * @return Promise\ExtendedPromiseInterface |
||
| 476 | */ |
||
| 477 | private function establishConnection() : Promise\ExtendedPromiseInterface |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Registers a new client with the broker |
||
| 515 | * |
||
| 516 | * @param Mqtt\Connection $connection |
||
| 517 | * |
||
| 518 | * @return Promise\ExtendedPromiseInterface |
||
| 519 | */ |
||
| 520 | private function registerClient(Mqtt\Connection $connection) : Promise\ExtendedPromiseInterface |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Handles incoming data |
||
| 550 | * |
||
| 551 | * @param string $data |
||
| 552 | * |
||
| 553 | * @return void |
||
| 554 | */ |
||
| 555 | private function handleReceive(string $data) : void |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Handles an incoming packet |
||
| 576 | * |
||
| 577 | * @param Mqtt\Packet $packet |
||
| 578 | * |
||
| 579 | * @return void |
||
| 580 | */ |
||
| 581 | private function handlePacket(Mqtt\Packet $packet) : void |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Handles outgoing packets |
||
| 634 | * |
||
| 635 | * @return void |
||
| 636 | */ |
||
| 637 | private function handleSend() : void |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Handles closing of the stream |
||
| 665 | * |
||
| 666 | * @return void |
||
| 667 | */ |
||
| 668 | private function handleClose() : void |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Handles errors of the stream |
||
| 689 | * |
||
| 690 | * @param \Exception $ex |
||
| 691 | * |
||
| 692 | * @return void |
||
| 693 | */ |
||
| 694 | private function handleError(\Exception $ex) : void |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Starts the given flow |
||
| 701 | * |
||
| 702 | * @param Mqtt\Flow $flow |
||
| 703 | * @param bool $isSilent |
||
| 704 | * |
||
| 705 | * @return Promise\ExtendedPromiseInterface |
||
| 706 | */ |
||
| 707 | private function startFlow(Mqtt\Flow $flow, bool $isSilent = FALSE) : Promise\ExtendedPromiseInterface |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Continues the given flow |
||
| 743 | * |
||
| 744 | * @param Flow\Envelope $flow |
||
| 745 | * @param Mqtt\Packet $packet |
||
| 746 | * |
||
| 747 | * @return void |
||
| 748 | */ |
||
| 749 | private function continueFlow(Flow\Envelope $flow, Mqtt\Packet $packet) : void |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Finishes the given flow |
||
| 780 | * |
||
| 781 | * @param Flow\Envelope $flow |
||
| 782 | * |
||
| 783 | * @return void |
||
| 784 | */ |
||
| 785 | private function finishFlow(Flow\Envelope $flow) : void |
||
| 848 | |||
| 849 | /** |
||
| 850 | * @return void |
||
| 851 | */ |
||
| 852 | private function createConnector() : void |
||
| 866 | } |
||
| 867 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: