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 | * @param EventLoop\LoopInterface $eventLoop |
||
213 | * @param Configuration $configuration |
||
214 | * @param Mqtt\IdentifierGenerator|NULL $identifierGenerator |
||
215 | * @param Mqtt\StreamParser|NULL $parser |
||
216 | */ |
||
217 | public function __construct( |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | public function setLoop(EventLoop\LoopInterface $loop) : void |
||
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | public function getLoop() : EventLoop\LoopInterface |
||
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | public function setConfiguration(Configuration $configuration) : void |
||
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function getUri() : string |
||
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | public function getPort() : int |
||
295 | |||
296 | /** |
||
297 | * {@inheritdoc} |
||
298 | */ |
||
299 | public function isConnected() : bool |
||
303 | |||
304 | /** |
||
305 | * {@inheritdoc} |
||
306 | */ |
||
307 | public function connect() : Promise\ExtendedPromiseInterface |
||
367 | |||
368 | /** |
||
369 | * {@inheritdoc} |
||
370 | */ |
||
371 | public function disconnect() : Promise\ExtendedPromiseInterface |
||
401 | |||
402 | /** |
||
403 | * {@inheritdoc} |
||
404 | */ |
||
405 | View Code Duplication | public function subscribe(Mqtt\Subscription $subscription) : Promise\ExtendedPromiseInterface |
|
413 | |||
414 | /** |
||
415 | * {@inheritdoc} |
||
416 | */ |
||
417 | View Code Duplication | public function unsubscribe(Mqtt\Subscription $subscription) : Promise\ExtendedPromiseInterface |
|
425 | |||
426 | /** |
||
427 | * {@inheritdoc} |
||
428 | */ |
||
429 | public function publish(Mqtt\Message $message) : Promise\ExtendedPromiseInterface |
||
437 | |||
438 | /** |
||
439 | * {@inheritdoc} |
||
440 | */ |
||
441 | public function publishPeriodically( |
||
466 | |||
467 | /** |
||
468 | * Establishes a network connection to a server |
||
469 | * |
||
470 | * @return Promise\ExtendedPromiseInterface |
||
471 | */ |
||
472 | private function establishConnection() : Promise\ExtendedPromiseInterface |
||
507 | |||
508 | /** |
||
509 | * Registers a new client with the broker |
||
510 | * |
||
511 | * @param Mqtt\Connection $connection |
||
512 | * |
||
513 | * @return Promise\ExtendedPromiseInterface |
||
514 | */ |
||
515 | private function registerClient(Mqtt\Connection $connection) : Promise\ExtendedPromiseInterface |
||
542 | |||
543 | /** |
||
544 | * Handles incoming data |
||
545 | * |
||
546 | * @param string $data |
||
547 | * |
||
548 | * @return void |
||
549 | */ |
||
550 | private function handleReceive(string $data) : void |
||
568 | |||
569 | /** |
||
570 | * Handles an incoming packet |
||
571 | * |
||
572 | * @param Mqtt\Packet $packet |
||
573 | * |
||
574 | * @return void |
||
575 | */ |
||
576 | private function handlePacket(Mqtt\Packet $packet) : void |
||
626 | |||
627 | /** |
||
628 | * Handles outgoing packets |
||
629 | * |
||
630 | * @return void |
||
631 | */ |
||
632 | private function handleSend() : void |
||
657 | |||
658 | /** |
||
659 | * Handles closing of the stream |
||
660 | * |
||
661 | * @return void |
||
662 | */ |
||
663 | private function handleClose() : void |
||
681 | |||
682 | /** |
||
683 | * Handles errors of the stream |
||
684 | * |
||
685 | * @param \Exception $ex |
||
686 | * |
||
687 | * @return void |
||
688 | */ |
||
689 | private function handleError(\Exception $ex) : void |
||
693 | |||
694 | /** |
||
695 | * Starts the given flow |
||
696 | * |
||
697 | * @param Mqtt\Flow $flow |
||
698 | * @param bool $isSilent |
||
699 | * |
||
700 | * @return Promise\ExtendedPromiseInterface |
||
701 | */ |
||
702 | private function startFlow(Mqtt\Flow $flow, bool $isSilent = FALSE) : Promise\ExtendedPromiseInterface |
||
735 | |||
736 | /** |
||
737 | * Continues the given flow |
||
738 | * |
||
739 | * @param Flow\Envelope $flow |
||
740 | * @param Mqtt\Packet $packet |
||
741 | * |
||
742 | * @return void |
||
743 | */ |
||
744 | private function continueFlow(Flow\Envelope $flow, Mqtt\Packet $packet) : void |
||
772 | |||
773 | /** |
||
774 | * Finishes the given flow |
||
775 | * |
||
776 | * @param Flow\Envelope $flow |
||
777 | * |
||
778 | * @return void |
||
779 | */ |
||
780 | private function finishFlow(Flow\Envelope $flow) : void |
||
828 | |||
829 | /** |
||
830 | * @return void |
||
831 | */ |
||
832 | private function createConnector() : void |
||
846 | } |
||
847 |
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..