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 |
||
| 53 | 1 | final class Client implements IClient |
|
| 54 | { |
||
| 55 | /** |
||
| 56 | * Implement nette smart magic |
||
| 57 | */ |
||
| 58 | 1 | use Nette\SmartObject; |
|
| 59 | |||
| 60 | /** |
||
| 61 | * @var \Closure |
||
| 62 | */ |
||
| 63 | public $onOpen = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var \Closure |
||
| 67 | */ |
||
| 68 | public $onConnect = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var \Closure |
||
| 72 | */ |
||
| 73 | public $onDisconnect = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var \Closure |
||
| 77 | */ |
||
| 78 | public $onClose = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var \Closure |
||
| 82 | */ |
||
| 83 | public $onPing = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var \Closure |
||
| 87 | */ |
||
| 88 | public $onPong = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var \Closure |
||
| 92 | */ |
||
| 93 | public $onPublish = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var \Closure |
||
| 97 | */ |
||
| 98 | public $onSubscribe = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var \Closure |
||
| 102 | */ |
||
| 103 | public $onUnsubscribe = []; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var \Closure |
||
| 107 | */ |
||
| 108 | public $onMessage = []; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var \Closure |
||
| 112 | */ |
||
| 113 | public $onWarning = []; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var \Closure |
||
| 117 | */ |
||
| 118 | public $onError = []; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var EventLoop\LoopInterface |
||
| 122 | */ |
||
| 123 | private $loop; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var Configuration |
||
| 127 | */ |
||
| 128 | private $configuration; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var Socket\ConnectorInterface |
||
| 132 | */ |
||
| 133 | private $connector; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var Socket\ConnectionInterface|NULL |
||
| 137 | */ |
||
| 138 | private $stream = NULL; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var Mqtt\Connection |
||
| 142 | */ |
||
| 143 | private $connection; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var Mqtt\StreamParser |
||
| 147 | */ |
||
| 148 | private $parser; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var Mqtt\IdentifierGenerator |
||
| 152 | */ |
||
| 153 | private $identifierGenerator; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var bool |
||
| 157 | */ |
||
| 158 | private $isConnected = FALSE; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var bool |
||
| 162 | */ |
||
| 163 | private $isConnecting = FALSE; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @var bool |
||
| 167 | */ |
||
| 168 | private $isDisconnecting = FALSE; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var string |
||
| 172 | */ |
||
| 173 | private $host; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @var int |
||
| 177 | */ |
||
| 178 | private $port; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @var int |
||
| 182 | */ |
||
| 183 | private $timeout = 5; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @var Flow\Envelope[] |
||
| 187 | */ |
||
| 188 | private $receivingFlows = []; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @var Flow\Envelope[] |
||
| 192 | */ |
||
| 193 | private $sendingFlows = []; |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @var Flow\Envelope |
||
| 197 | */ |
||
| 198 | private $writtenFlow; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @var EventLoop\Timer\TimerInterface[] |
||
| 202 | */ |
||
| 203 | private $timer = []; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param EventLoop\LoopInterface $eventLoop |
||
| 207 | * @param Configuration $configuration |
||
| 208 | * @param Mqtt\IdentifierGenerator|NULL $identifierGenerator |
||
| 209 | * @param Mqtt\StreamParser|NULL $parser |
||
| 210 | */ |
||
| 211 | public function __construct( |
||
| 212 | EventLoop\LoopInterface $eventLoop, |
||
| 213 | Configuration $configuration, |
||
| 214 | Mqtt\IdentifierGenerator $identifierGenerator = NULL, |
||
| 215 | Mqtt\StreamParser $parser = NULL |
||
| 216 | ) { |
||
| 217 | 1 | $this->loop = $eventLoop; |
|
| 218 | 1 | $this->configuration = $configuration; |
|
| 219 | |||
| 220 | 1 | $this->createConnector(); |
|
| 221 | |||
| 222 | 1 | $this->parser = $parser; |
|
| 223 | |||
| 224 | 1 | if ($this->parser === NULL) { |
|
| 225 | 1 | $this->parser = new Mqtt\StreamParser; |
|
| 226 | } |
||
| 227 | |||
| 228 | 1 | $this->parser->onError(function (\Exception $ex) { |
|
| 229 | $this->onError($ex, $this); |
||
| 230 | 1 | }); |
|
| 231 | |||
| 232 | 1 | $this->identifierGenerator = $identifierGenerator; |
|
| 233 | |||
| 234 | 1 | if ($this->identifierGenerator === NULL) { |
|
| 235 | 1 | $this->identifierGenerator = new Mqtt\DefaultIdentifierGenerator; |
|
|
|
|||
| 236 | } |
||
| 237 | 1 | } |
|
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | public function setLoop(EventLoop\LoopInterface $loop) : void |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | */ |
||
| 257 | public function getLoop() : EventLoop\LoopInterface |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | */ |
||
| 265 | public function setConfiguration(Configuration $configuration) : void |
||
| 273 | |||
| 274 | /** |
||
| 275 | * {@inheritdoc} |
||
| 276 | */ |
||
| 277 | public function getUri() : string |
||
| 281 | |||
| 282 | /** |
||
| 283 | * {@inheritdoc} |
||
| 284 | */ |
||
| 285 | public function getPort() : int |
||
| 289 | |||
| 290 | /** |
||
| 291 | * {@inheritdoc} |
||
| 292 | */ |
||
| 293 | public function isConnected() : bool |
||
| 297 | |||
| 298 | /** |
||
| 299 | * {@inheritdoc} |
||
| 300 | */ |
||
| 301 | public function connect() : Promise\ExtendedPromiseInterface |
||
| 359 | |||
| 360 | /** |
||
| 361 | * {@inheritdoc} |
||
| 362 | */ |
||
| 363 | public function disconnect() : Promise\ExtendedPromiseInterface |
||
| 393 | |||
| 394 | /** |
||
| 395 | * {@inheritdoc} |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function subscribe(Mqtt\Subscription $subscription) : Promise\ExtendedPromiseInterface |
|
| 405 | |||
| 406 | /** |
||
| 407 | * {@inheritdoc} |
||
| 408 | */ |
||
| 409 | View Code Duplication | public function unsubscribe(Mqtt\Subscription $subscription) : Promise\ExtendedPromiseInterface |
|
| 417 | |||
| 418 | /** |
||
| 419 | * {@inheritdoc} |
||
| 420 | */ |
||
| 421 | public function publish(Mqtt\Message $message) : Promise\ExtendedPromiseInterface |
||
| 429 | |||
| 430 | /** |
||
| 431 | * {@inheritdoc} |
||
| 432 | */ |
||
| 433 | public function publishPeriodically( |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Establishes a network connection to a server |
||
| 461 | * |
||
| 462 | * @return Promise\ExtendedPromiseInterface |
||
| 463 | */ |
||
| 464 | private function establishConnection() : Promise\ExtendedPromiseInterface |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Registers a new client with the broker |
||
| 502 | * |
||
| 503 | * @param Mqtt\Connection $connection |
||
| 504 | * |
||
| 505 | * @return Promise\ExtendedPromiseInterface |
||
| 506 | */ |
||
| 507 | private function registerClient(Mqtt\Connection $connection) : Promise\ExtendedPromiseInterface |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Handles incoming data |
||
| 537 | * |
||
| 538 | * @param string $data |
||
| 539 | * |
||
| 540 | * @return void |
||
| 541 | */ |
||
| 542 | private function handleReceive(string $data) : void |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Handles an incoming packet |
||
| 563 | * |
||
| 564 | * @param Mqtt\Packet $packet |
||
| 565 | * |
||
| 566 | * @return void |
||
| 567 | */ |
||
| 568 | private function handlePacket(Mqtt\Packet $packet) : void |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Handles outgoing packets |
||
| 621 | * |
||
| 622 | * @return void |
||
| 623 | */ |
||
| 624 | private function handleSend() : void |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Handles closing of the stream |
||
| 652 | * |
||
| 653 | * @return void |
||
| 654 | */ |
||
| 655 | private function handleClose() : void |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Handles errors of the stream |
||
| 676 | * |
||
| 677 | * @param \Exception $ex |
||
| 678 | * |
||
| 679 | * @return void |
||
| 680 | */ |
||
| 681 | private function handleError(\Exception $ex) : void |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Starts the given flow |
||
| 688 | * |
||
| 689 | * @param Mqtt\Flow $flow |
||
| 690 | * @param bool $isSilent |
||
| 691 | * |
||
| 692 | * @return Promise\ExtendedPromiseInterface |
||
| 693 | */ |
||
| 694 | private function startFlow(Mqtt\Flow $flow, bool $isSilent = FALSE) : Promise\ExtendedPromiseInterface |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Continues the given flow |
||
| 730 | * |
||
| 731 | * @param Flow\Envelope $flow |
||
| 732 | * @param Mqtt\Packet $packet |
||
| 733 | * |
||
| 734 | * @return void |
||
| 735 | */ |
||
| 736 | private function continueFlow(Flow\Envelope $flow, Mqtt\Packet $packet) : void |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Finishes the given flow |
||
| 767 | * |
||
| 768 | * @param Flow\Envelope $flow |
||
| 769 | * |
||
| 770 | * @return void |
||
| 771 | */ |
||
| 772 | private function finishFlow(Flow\Envelope $flow) : void |
||
| 820 | |||
| 821 | /** |
||
| 822 | * @return void |
||
| 823 | */ |
||
| 824 | private function createConnector() : void |
||
| 838 | } |
||
| 839 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..