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 |
||
58 | 1 | final class Client implements IClient |
|
59 | { |
||
60 | /** |
||
61 | * Implement nette smart magic |
||
62 | */ |
||
63 | 1 | use Nette\SmartObject; |
|
64 | |||
65 | /** |
||
66 | * @var Closure |
||
67 | */ |
||
68 | public $onStart = []; |
||
69 | |||
70 | /** |
||
71 | * @var Closure |
||
72 | */ |
||
73 | public $onOpen = []; |
||
74 | |||
75 | /** |
||
76 | * @var Closure |
||
77 | */ |
||
78 | public $onConnect = []; |
||
79 | |||
80 | /** |
||
81 | * @var Closure |
||
82 | */ |
||
83 | public $onDisconnect = []; |
||
84 | |||
85 | /** |
||
86 | * @var Closure |
||
87 | */ |
||
88 | public $onClose = []; |
||
89 | |||
90 | /** |
||
91 | * @var Closure |
||
92 | */ |
||
93 | public $onPing = []; |
||
94 | |||
95 | /** |
||
96 | * @var Closure |
||
97 | */ |
||
98 | public $onPong = []; |
||
99 | |||
100 | /** |
||
101 | * @var Closure |
||
102 | */ |
||
103 | public $onPublish = []; |
||
104 | |||
105 | /** |
||
106 | * @var Closure |
||
107 | */ |
||
108 | public $onSubscribe = []; |
||
109 | |||
110 | /** |
||
111 | * @var Closure |
||
112 | */ |
||
113 | public $onUnsubscribe = []; |
||
114 | |||
115 | /** |
||
116 | * @var Closure |
||
117 | */ |
||
118 | public $onMessage = []; |
||
119 | |||
120 | /** |
||
121 | * @var Closure |
||
122 | */ |
||
123 | public $onWarning = []; |
||
124 | |||
125 | /** |
||
126 | * @var Closure |
||
127 | */ |
||
128 | public $onError = []; |
||
129 | |||
130 | /** |
||
131 | * @var EventLoop\LoopInterface |
||
132 | */ |
||
133 | private $loop; |
||
134 | |||
135 | /** |
||
136 | * @var Configuration\Broker |
||
137 | */ |
||
138 | private $configuration; |
||
139 | |||
140 | /** |
||
141 | * @var Socket\ConnectorInterface |
||
142 | */ |
||
143 | private $connector; |
||
144 | |||
145 | /** |
||
146 | * @var Socket\ConnectionInterface|NULL |
||
147 | */ |
||
148 | private $stream = NULL; |
||
149 | |||
150 | /** |
||
151 | * @var Mqtt\Connection |
||
152 | */ |
||
153 | private $connection; |
||
154 | |||
155 | /** |
||
156 | * @var Mqtt\StreamParser |
||
157 | */ |
||
158 | private $parser; |
||
159 | |||
160 | /** |
||
161 | * @var Mqtt\IdentifierGenerator |
||
162 | */ |
||
163 | private $identifierGenerator; |
||
164 | |||
165 | /** |
||
166 | * @var bool |
||
167 | */ |
||
168 | private $isConnected = FALSE; |
||
169 | |||
170 | /** |
||
171 | * @var bool |
||
172 | */ |
||
173 | private $isConnecting = FALSE; |
||
174 | |||
175 | /** |
||
176 | * @var bool |
||
177 | */ |
||
178 | private $isDisconnecting = FALSE; |
||
179 | |||
180 | /** |
||
181 | * @var string |
||
182 | */ |
||
183 | private $host; |
||
184 | |||
185 | /** |
||
186 | * @var int |
||
187 | */ |
||
188 | private $port; |
||
189 | |||
190 | /** |
||
191 | * @var int |
||
192 | */ |
||
193 | private $timeout = 5; |
||
194 | |||
195 | /** |
||
196 | * @var Flow\Envelope[] |
||
197 | */ |
||
198 | private $receivingFlows = []; |
||
199 | |||
200 | /** |
||
201 | * @var Flow\Envelope[] |
||
202 | */ |
||
203 | private $sendingFlows = []; |
||
204 | |||
205 | /** |
||
206 | * @var Flow\Envelope |
||
207 | */ |
||
208 | private $writtenFlow; |
||
209 | |||
210 | /** |
||
211 | * @var EventLoop\TimerInterface[] |
||
212 | */ |
||
213 | private $timer = []; |
||
214 | |||
215 | /** |
||
216 | * @var Mqtt\Message|NULL |
||
217 | */ |
||
218 | private $lastMessage = NULL; |
||
219 | |||
220 | /** |
||
221 | * @param EventLoop\LoopInterface $eventLoop |
||
222 | * @param Configuration\Broker $configuration |
||
223 | * @param Mqtt\IdentifierGenerator|NULL $identifierGenerator |
||
224 | * @param Mqtt\StreamParser|NULL $parser |
||
225 | */ |
||
226 | public function __construct( |
||
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | public function setLoop(EventLoop\LoopInterface $loop) : void |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | public function getLoop() : EventLoop\LoopInterface |
||
276 | |||
277 | /** |
||
278 | * {@inheritdoc} |
||
279 | * |
||
280 | * @throws Exceptions\InvalidStateException |
||
281 | */ |
||
282 | public function setConfiguration(Configuration\Broker $configuration) : void |
||
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | * |
||
294 | * @throws Exceptions\InvalidStateException |
||
295 | */ |
||
296 | public function getUri() : string |
||
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | public function getPort() : int |
||
308 | |||
309 | /** |
||
310 | * {@inheritdoc} |
||
311 | */ |
||
312 | public function isConnected() : bool |
||
316 | |||
317 | /** |
||
318 | * {@inheritdoc} |
||
319 | * |
||
320 | * @throws Exceptions\InvalidStateException |
||
321 | */ |
||
322 | public function connect() : Promise\ExtendedPromiseInterface |
||
382 | |||
383 | /** |
||
384 | * {@inheritdoc} |
||
385 | */ |
||
386 | public function disconnect() : Promise\ExtendedPromiseInterface |
||
416 | |||
417 | /** |
||
418 | * {@inheritdoc} |
||
419 | */ |
||
420 | View Code Duplication | public function subscribe(Mqtt\Subscription $subscription) : Promise\ExtendedPromiseInterface |
|
428 | |||
429 | /** |
||
430 | * {@inheritdoc} |
||
431 | */ |
||
432 | View Code Duplication | public function unsubscribe(Mqtt\Subscription $subscription) : Promise\ExtendedPromiseInterface |
|
440 | |||
441 | /** |
||
442 | * {@inheritdoc} |
||
443 | */ |
||
444 | public function publish(Mqtt\Message $message) : Promise\ExtendedPromiseInterface |
||
452 | |||
453 | /** |
||
454 | * {@inheritdoc} |
||
455 | */ |
||
456 | public function publishPeriodically( |
||
481 | |||
482 | /** |
||
483 | * Establishes a network connection to a server |
||
484 | * |
||
485 | * @return Promise\ExtendedPromiseInterface |
||
486 | * |
||
487 | * @throws Exceptions\InvalidStateException |
||
488 | */ |
||
489 | private function establishConnection() : Promise\ExtendedPromiseInterface |
||
524 | |||
525 | /** |
||
526 | * Registers a new client with the broker |
||
527 | * |
||
528 | * @param Mqtt\Connection $connection |
||
529 | * |
||
530 | * @return Promise\ExtendedPromiseInterface |
||
531 | */ |
||
532 | private function registerClient(Mqtt\Connection $connection) : Promise\ExtendedPromiseInterface |
||
559 | |||
560 | /** |
||
561 | * Handles incoming data |
||
562 | * |
||
563 | * @param string $data |
||
564 | * |
||
565 | * @return void |
||
566 | */ |
||
567 | private function handleReceive(string $data) : void |
||
585 | |||
586 | /** |
||
587 | * Handles an incoming packet |
||
588 | * |
||
589 | * @param Mqtt\Packet $packet |
||
590 | * |
||
591 | * @return void |
||
592 | */ |
||
593 | private function handlePacket(Mqtt\Packet $packet) : void |
||
643 | |||
644 | /** |
||
645 | * Handles outgoing packets |
||
646 | * |
||
647 | * @return void |
||
648 | */ |
||
649 | private function handleSend() : void |
||
674 | |||
675 | /** |
||
676 | * Handles closing of the stream |
||
677 | * |
||
678 | * @return void |
||
679 | */ |
||
680 | private function handleClose() : void |
||
698 | |||
699 | /** |
||
700 | * Handles errors of the stream |
||
701 | * |
||
702 | * @param Exception $ex |
||
703 | * |
||
704 | * @return void |
||
705 | */ |
||
706 | private function handleError(Exception $ex) : void |
||
710 | |||
711 | /** |
||
712 | * Starts the given flow |
||
713 | * |
||
714 | * @param Mqtt\Flow $flow |
||
715 | * @param bool $isSilent |
||
716 | * |
||
717 | * @return Promise\ExtendedPromiseInterface |
||
718 | */ |
||
719 | private function startFlow(Mqtt\Flow $flow, bool $isSilent = FALSE) : Promise\ExtendedPromiseInterface |
||
752 | |||
753 | /** |
||
754 | * Continues the given flow |
||
755 | * |
||
756 | * @param Flow\Envelope $flow |
||
757 | * @param Mqtt\Packet $packet |
||
758 | * |
||
759 | * @return void |
||
760 | */ |
||
761 | private function continueFlow(Flow\Envelope $flow, Mqtt\Packet $packet) : void |
||
789 | |||
790 | /** |
||
791 | * Finishes the given flow |
||
792 | * |
||
793 | * @param Flow\Envelope $flow |
||
794 | * |
||
795 | * @return void |
||
796 | */ |
||
797 | private function finishFlow(Flow\Envelope $flow) : void |
||
860 | |||
861 | /** |
||
862 | * @return void |
||
863 | */ |
||
864 | private function createConnector() : void |
||
878 | } |
||
879 |
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..