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 |
||
| 57 | 1 | final class Client implements IClient |
|
| 58 | { |
||
| 59 | /** |
||
| 60 | * Implement nette smart magic |
||
| 61 | */ |
||
| 62 | 1 | use Nette\SmartObject; |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @var Closure |
||
| 66 | */ |
||
| 67 | public $onStart = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var Closure |
||
| 71 | */ |
||
| 72 | public $onOpen = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var Closure |
||
| 76 | */ |
||
| 77 | public $onConnect = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var Closure |
||
| 81 | */ |
||
| 82 | public $onDisconnect = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var Closure |
||
| 86 | */ |
||
| 87 | public $onClose = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var Closure |
||
| 91 | */ |
||
| 92 | public $onPing = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var Closure |
||
| 96 | */ |
||
| 97 | public $onPong = []; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var Closure |
||
| 101 | */ |
||
| 102 | public $onPublish = []; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var Closure |
||
| 106 | */ |
||
| 107 | public $onSubscribe = []; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var Closure |
||
| 111 | */ |
||
| 112 | public $onUnsubscribe = []; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var Closure |
||
| 116 | */ |
||
| 117 | public $onMessage = []; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var Closure |
||
| 121 | */ |
||
| 122 | public $onWarning = []; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var Closure |
||
| 126 | */ |
||
| 127 | public $onError = []; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var EventLoop\LoopInterface |
||
| 131 | */ |
||
| 132 | private $loop; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var Configuration\Broker |
||
| 136 | */ |
||
| 137 | private $configuration; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var Socket\ConnectorInterface |
||
| 141 | */ |
||
| 142 | private $connector; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @var Socket\ConnectionInterface|NULL |
||
| 146 | */ |
||
| 147 | private $stream = NULL; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var Mqtt\Connection |
||
| 151 | */ |
||
| 152 | private $connection; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @var Mqtt\StreamParser |
||
| 156 | */ |
||
| 157 | private $parser; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var Mqtt\IdentifierGenerator |
||
| 161 | */ |
||
| 162 | private $identifierGenerator; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var bool |
||
| 166 | */ |
||
| 167 | private $isConnected = FALSE; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var bool |
||
| 171 | */ |
||
| 172 | private $isConnecting = FALSE; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var bool |
||
| 176 | */ |
||
| 177 | private $isDisconnecting = FALSE; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @var string |
||
| 181 | */ |
||
| 182 | private $host; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @var int |
||
| 186 | */ |
||
| 187 | private $port; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @var int |
||
| 191 | */ |
||
| 192 | private $timeout = 5; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @var Flow\Envelope[] |
||
| 196 | */ |
||
| 197 | private $receivingFlows = []; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @var Flow\Envelope[] |
||
| 201 | */ |
||
| 202 | private $sendingFlows = []; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @var Flow\Envelope |
||
| 206 | */ |
||
| 207 | private $writtenFlow; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @var EventLoop\TimerInterface[] |
||
| 211 | */ |
||
| 212 | private $timer = []; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @var Mqtt\Message|NULL |
||
| 216 | */ |
||
| 217 | private $lastMessage = NULL; |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param EventLoop\LoopInterface $eventLoop |
||
| 221 | * @param Configuration\Broker $configuration |
||
| 222 | * @param Mqtt\IdentifierGenerator|NULL $identifierGenerator |
||
| 223 | * @param Mqtt\StreamParser|NULL $parser |
||
| 224 | */ |
||
| 225 | public function __construct( |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | public function setLoop(EventLoop\LoopInterface $loop) : void |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | public function getLoop() : EventLoop\LoopInterface |
||
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritdoc} |
||
| 278 | * |
||
| 279 | * @throws Exceptions\InvalidStateException |
||
| 280 | */ |
||
| 281 | public function setConfiguration(Configuration\Broker $configuration) : void |
||
| 289 | |||
| 290 | /** |
||
| 291 | * {@inheritdoc} |
||
| 292 | * |
||
| 293 | * @throws Exceptions\InvalidStateException |
||
| 294 | */ |
||
| 295 | public function getUri() : string |
||
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | public function getPort() : int |
||
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | public function isConnected() : bool |
||
| 315 | |||
| 316 | /** |
||
| 317 | * {@inheritdoc} |
||
| 318 | */ |
||
| 319 | public function connect() : Promise\ExtendedPromiseInterface |
||
| 379 | |||
| 380 | /** |
||
| 381 | * {@inheritdoc} |
||
| 382 | */ |
||
| 383 | public function disconnect() : Promise\ExtendedPromiseInterface |
||
| 413 | |||
| 414 | /** |
||
| 415 | * {@inheritdoc} |
||
| 416 | */ |
||
| 417 | View Code Duplication | public function subscribe(Mqtt\Subscription $subscription) : Promise\ExtendedPromiseInterface |
|
| 425 | |||
| 426 | /** |
||
| 427 | * {@inheritdoc} |
||
| 428 | */ |
||
| 429 | View Code Duplication | public function unsubscribe(Mqtt\Subscription $subscription) : Promise\ExtendedPromiseInterface |
|
| 437 | |||
| 438 | /** |
||
| 439 | * {@inheritdoc} |
||
| 440 | */ |
||
| 441 | public function publish(Mqtt\Message $message) : Promise\ExtendedPromiseInterface |
||
| 449 | |||
| 450 | /** |
||
| 451 | * {@inheritdoc} |
||
| 452 | */ |
||
| 453 | public function publishPeriodically( |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Establishes a network connection to a server |
||
| 481 | * |
||
| 482 | * @return Promise\ExtendedPromiseInterface |
||
| 483 | * |
||
| 484 | * @throws Exceptions\InvalidStateException |
||
| 485 | */ |
||
| 486 | private function establishConnection() : Promise\ExtendedPromiseInterface |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Registers a new client with the broker |
||
| 524 | * |
||
| 525 | * @param Mqtt\Connection $connection |
||
| 526 | * |
||
| 527 | * @return Promise\ExtendedPromiseInterface |
||
| 528 | */ |
||
| 529 | private function registerClient(Mqtt\Connection $connection) : Promise\ExtendedPromiseInterface |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Handles incoming data |
||
| 559 | * |
||
| 560 | * @param string $data |
||
| 561 | * |
||
| 562 | * @return void |
||
| 563 | */ |
||
| 564 | private function handleReceive(string $data) : void |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Handles an incoming packet |
||
| 585 | * |
||
| 586 | * @param Mqtt\Packet $packet |
||
| 587 | * |
||
| 588 | * @return void |
||
| 589 | */ |
||
| 590 | private function handlePacket(Mqtt\Packet $packet) : void |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Handles outgoing packets |
||
| 643 | * |
||
| 644 | * @return void |
||
| 645 | */ |
||
| 646 | private function handleSend() : void |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Handles closing of the stream |
||
| 674 | * |
||
| 675 | * @return void |
||
| 676 | */ |
||
| 677 | private function handleClose() : void |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Handles errors of the stream |
||
| 698 | * |
||
| 699 | * @param \Exception $ex |
||
| 700 | * |
||
| 701 | * @return void |
||
| 702 | */ |
||
| 703 | private function handleError(\Exception $ex) : void |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Starts the given flow |
||
| 710 | * |
||
| 711 | * @param Mqtt\Flow $flow |
||
| 712 | * @param bool $isSilent |
||
| 713 | * |
||
| 714 | * @return Promise\ExtendedPromiseInterface |
||
| 715 | */ |
||
| 716 | private function startFlow(Mqtt\Flow $flow, bool $isSilent = FALSE) : Promise\ExtendedPromiseInterface |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Continues the given flow |
||
| 752 | * |
||
| 753 | * @param Flow\Envelope $flow |
||
| 754 | * @param Mqtt\Packet $packet |
||
| 755 | * |
||
| 756 | * @return void |
||
| 757 | */ |
||
| 758 | private function continueFlow(Flow\Envelope $flow, Mqtt\Packet $packet) : void |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Finishes the given flow |
||
| 789 | * |
||
| 790 | * @param Flow\Envelope $flow |
||
| 791 | * |
||
| 792 | * @return void |
||
| 793 | */ |
||
| 794 | private function finishFlow(Flow\Envelope $flow) : void |
||
| 857 | |||
| 858 | /** |
||
| 859 | * @return void |
||
| 860 | */ |
||
| 861 | private function createConnector() : void |
||
| 875 | } |
||
| 876 |
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: