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( |
||
226 | EventLoop\LoopInterface $eventLoop, |
||
227 | Configuration\Broker $configuration, |
||
228 | Mqtt\IdentifierGenerator $identifierGenerator = NULL, |
||
229 | Mqtt\StreamParser $parser = NULL |
||
230 | ) { |
||
231 | 1 | $this->loop = $eventLoop; |
|
232 | 1 | $this->configuration = $configuration; |
|
233 | |||
234 | 1 | $this->parser = $parser; |
|
235 | |||
236 | 1 | if ($this->parser === NULL) { |
|
237 | 1 | $this->parser = new Mqtt\StreamParser; |
|
238 | } |
||
239 | |||
240 | 1 | $this->parser->onError(function (Exception $ex) { |
|
241 | $this->onError($ex, $this); |
||
242 | 1 | }); |
|
243 | |||
244 | 1 | $this->identifierGenerator = $identifierGenerator; |
|
245 | |||
246 | 1 | if ($this->identifierGenerator === NULL) { |
|
247 | 1 | $this->identifierGenerator = new Mqtt\DefaultIdentifierGenerator; |
|
|
|||
248 | } |
||
249 | 1 | } |
|
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | */ |
||
254 | public function setLoop(EventLoop\LoopInterface $loop) : void |
||
255 | { |
||
256 | if (!$this->isConnected && !$this->isConnecting) { |
||
257 | $this->loop = $loop; |
||
258 | |||
259 | } else { |
||
260 | throw new Exceptions\LogicException('Connection is already established. React event loop could not be changed.'); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | public function getLoop() : EventLoop\LoopInterface |
||
268 | { |
||
269 | return $this->loop; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * {@inheritdoc} |
||
274 | * |
||
275 | * @throws Exceptions\InvalidStateException |
||
276 | */ |
||
277 | public function setConfiguration(Configuration\Broker $configuration) : void |
||
278 | { |
||
279 | if ($this->isConnected() || $this->isConnecting) { |
||
280 | throw new Exceptions\InvalidStateException('Client is connecting or connected to the broker, therefore configuration could not be changed.'); |
||
281 | } |
||
282 | |||
283 | $this->configuration = $configuration; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * {@inheritdoc} |
||
288 | * |
||
289 | * @throws Exceptions\InvalidStateException |
||
290 | */ |
||
291 | public function getUri() : string |
||
292 | { |
||
293 | return $this->configuration->getUri(); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * {@inheritdoc} |
||
298 | */ |
||
299 | public function getPort() : int |
||
300 | { |
||
301 | return $this->configuration->getPort(); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * {@inheritdoc} |
||
306 | */ |
||
307 | public function isConnected() : bool |
||
308 | { |
||
309 | return $this->isConnected; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * {@inheritdoc} |
||
314 | * |
||
315 | * @throws Exceptions\InvalidStateException |
||
316 | */ |
||
317 | public function connect() : Promise\ExtendedPromiseInterface |
||
318 | { |
||
319 | if ($this->isConnected || $this->isConnecting) { |
||
320 | return new Promise\RejectedPromise(new Exceptions\LogicException('The client is already connected.')); |
||
321 | } |
||
322 | |||
323 | $this->createConnector(); |
||
324 | |||
325 | $this->onStart(); |
||
326 | |||
327 | $this->isConnecting = TRUE; |
||
328 | $this->isConnected = FALSE; |
||
329 | |||
330 | $connection = $this->configuration->getConnection(); |
||
331 | |||
332 | if ($connection->getClientID() === '') { |
||
333 | $connection->setClientID($this->identifierGenerator->generateClientID()); |
||
334 | } |
||
335 | |||
336 | $deferred = new Promise\Deferred; |
||
337 | |||
338 | $this->establishConnection() |
||
339 | ->then(function (Socket\ConnectionInterface $stream) use ($connection, $deferred) { |
||
340 | $this->stream = $stream; |
||
341 | |||
342 | $this->onOpen($connection, $this); |
||
343 | |||
344 | $this->registerClient($connection) |
||
345 | ->then(function (Mqtt\Connection $connection) use ($deferred) { |
||
346 | $this->isConnecting = FALSE; |
||
347 | $this->isConnected = TRUE; |
||
348 | |||
349 | $this->connection = $connection; |
||
350 | |||
351 | $this->onConnect($connection, $this); |
||
352 | |||
353 | $deferred->resolve($this->connection); |
||
354 | }) |
||
355 | ->otherwise(function (Exception $ex) use ($stream, $deferred, $connection) { |
||
356 | $this->isConnecting = FALSE; |
||
357 | |||
358 | $this->onError($ex, $this); |
||
359 | |||
360 | $deferred->reject($ex); |
||
361 | |||
362 | if ($this->stream !== NULL) { |
||
363 | $this->stream->close(); |
||
364 | } |
||
365 | |||
366 | $this->onClose($connection, $this); |
||
367 | }); |
||
368 | }) |
||
369 | ->otherwise(function (Exception $ex) use ($deferred) { |
||
370 | $this->isConnecting = FALSE; |
||
371 | |||
372 | $this->onError($ex, $this); |
||
373 | |||
374 | $deferred->reject($ex); |
||
375 | }); |
||
376 | |||
377 | return $deferred->promise(); |
||
378 | } |
||
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 |
||
862 | { |
||
863 | $this->connector = new Socket\Connector($this->loop); |
||
864 | |||
865 | if ($this->configuration->isSSLEnabled()) { |
||
870 | } |
||
871 |
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..