Complex classes like ZmqModel 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 ZmqModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | abstract class ZmqModel extends BaseEventEmitter implements ModelInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | const CONNECTOR = 2; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | const BINDER = 1; |
||
28 | |||
29 | /** |
||
30 | * @var int |
||
31 | */ |
||
32 | const SOCKET_UNDEFINED = 1; |
||
33 | |||
34 | /** |
||
35 | * @var int |
||
36 | */ |
||
37 | const COMMAND_HEARTBEAT = 1; |
||
38 | |||
39 | /** |
||
40 | * @var int |
||
41 | */ |
||
42 | const COMMAND_MESSAGE = 2; |
||
43 | |||
44 | /** |
||
45 | * @var int |
||
46 | */ |
||
47 | const MODE_STANDARD = Channel::MODE_STANDARD; |
||
48 | |||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | const MODE_BUFFER_ONLINE = Channel::MODE_BUFFER_ONLINE; |
||
53 | |||
54 | /** |
||
55 | * @var int |
||
56 | */ |
||
57 | const MODE_BUFFER_OFFLINE = Channel::MODE_BUFFER_OFFLINE; |
||
58 | |||
59 | /** |
||
60 | * @var int |
||
61 | */ |
||
62 | const MODE_BUFFER = Channel::MODE_BUFFER; |
||
63 | |||
64 | /** |
||
65 | * @var LoopInterface |
||
66 | */ |
||
67 | protected $loop; |
||
68 | |||
69 | /** |
||
70 | * @var ZmqContext |
||
71 | */ |
||
72 | protected $context; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $id; |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $endpoint; |
||
83 | |||
84 | /** |
||
85 | * @var int |
||
86 | */ |
||
87 | protected $type; |
||
88 | |||
89 | /** |
||
90 | * @var string[] |
||
91 | */ |
||
92 | protected $hosts; |
||
93 | |||
94 | /** |
||
95 | * @var string[] |
||
96 | */ |
||
97 | protected $flags; |
||
98 | |||
99 | /** |
||
100 | * @var mixed[] |
||
101 | */ |
||
102 | protected $options; |
||
103 | |||
104 | /** |
||
105 | * @var bool |
||
106 | */ |
||
107 | protected $isConnected; |
||
108 | |||
109 | /** |
||
110 | * @var string |
||
111 | */ |
||
112 | protected $pendingOperation; |
||
113 | |||
114 | /** |
||
115 | * @var callable |
||
116 | */ |
||
117 | protected $connectCallback; |
||
118 | |||
119 | /** |
||
120 | * @var callable |
||
121 | */ |
||
122 | protected $disconnectCallback; |
||
123 | |||
124 | /** |
||
125 | * @var ZmqSocket |
||
126 | */ |
||
127 | public $socket; |
||
128 | |||
129 | /** |
||
130 | * @var Buffer |
||
131 | */ |
||
132 | protected $buffer; |
||
133 | |||
134 | /** |
||
135 | * @var ConnectionPool |
||
136 | */ |
||
137 | protected $connectionPool; |
||
138 | |||
139 | /** |
||
140 | * @var TimerInterface |
||
141 | */ |
||
142 | private $hTimer; |
||
143 | |||
144 | /** |
||
145 | * @var TimerInterface |
||
146 | */ |
||
147 | private $rTimer; |
||
148 | |||
149 | /** |
||
150 | * @param LoopInterface $loop |
||
151 | * @param string[] $params |
||
152 | */ |
||
153 | public function __construct(LoopInterface $loop, $params) |
||
194 | |||
195 | /** |
||
196 | * |
||
197 | */ |
||
198 | public function __destruct() |
||
222 | |||
223 | /** |
||
224 | * @override |
||
225 | * @inheritDoc |
||
226 | */ |
||
227 | public function start($blockEvent = false) |
||
259 | |||
260 | /** |
||
261 | * @override |
||
262 | * @inheritDoc |
||
263 | */ |
||
264 | public function stop($blockEvent = false) |
||
286 | |||
287 | /** |
||
288 | * @override |
||
289 | * @inheritDoc |
||
290 | */ |
||
291 | public function unicast($id, $message, $flags = self::MODE_STANDARD) |
||
299 | |||
300 | /** |
||
301 | * @override |
||
302 | * @inheritDoc |
||
303 | */ |
||
304 | public function broadcast($message) |
||
321 | |||
322 | /** |
||
323 | * @override |
||
324 | * @inheritDoc |
||
325 | */ |
||
326 | public function isStarted() |
||
330 | |||
331 | /** |
||
332 | * @override |
||
333 | * @inheritDoc |
||
334 | */ |
||
335 | public function isStopped() |
||
339 | |||
340 | /** |
||
341 | * @override |
||
342 | * @inheritDoc |
||
343 | */ |
||
344 | public function isConnected($id) |
||
348 | |||
349 | /** |
||
350 | * @override |
||
351 | * @inheritDoc |
||
352 | */ |
||
353 | public function getConnected() |
||
357 | |||
358 | /** |
||
359 | * Set connection statically to be marked as online until specific timestamp. |
||
360 | * |
||
361 | * @param string $id |
||
362 | * @param float $until |
||
363 | */ |
||
364 | public function markConnectionOnline($id, $until) |
||
368 | |||
369 | /** |
||
370 | * Set connection statically to be marked always as online. |
||
371 | * |
||
372 | * @param string $id |
||
373 | */ |
||
374 | public function markConnectionPersistent($id) |
||
378 | |||
379 | /** |
||
380 | * @return int |
||
381 | */ |
||
382 | abstract protected function getSocketType(); |
||
383 | |||
384 | /** |
||
385 | * @param string[] $multipartMessage |
||
386 | * @return string[] |
||
387 | */ |
||
388 | abstract protected function parseBinderMessage($multipartMessage); |
||
389 | |||
390 | /** |
||
391 | * @param string[] $multipartMessage |
||
392 | * @return string[] |
||
393 | */ |
||
394 | abstract protected function parseConnectorMessage($multipartMessage); |
||
395 | |||
396 | /** |
||
397 | * @param string $id |
||
398 | * @param string $type |
||
399 | * @return string[] |
||
400 | */ |
||
401 | abstract protected function prepareBinderMessage($id, $type); |
||
402 | |||
403 | /** |
||
404 | * @param string $id |
||
405 | * @param string $type |
||
406 | * @return string[] |
||
407 | */ |
||
408 | abstract protected function prepareConnectorMessage($id, $type); |
||
409 | |||
410 | /** |
||
411 | * @return ZmqSocket |
||
412 | */ |
||
413 | protected function getSocket() |
||
424 | |||
425 | /** |
||
426 | * @return Buffer |
||
427 | */ |
||
428 | protected function getBuffer() |
||
432 | |||
433 | /** |
||
434 | * @return ConnectionPool |
||
435 | */ |
||
436 | protected function getConnectionPool() |
||
440 | |||
441 | /** |
||
442 | * @param string $event |
||
443 | * @param callable $callback |
||
444 | */ |
||
445 | protected function setEventListener($event, callable $callback) |
||
449 | |||
450 | /** |
||
451 | * @param string $event |
||
452 | * @param callable $callback |
||
453 | */ |
||
454 | protected function removeEventListener($event, callable $callback) |
||
458 | |||
459 | /** |
||
460 | * @param string[] $argv |
||
461 | */ |
||
462 | public function onMessages($argv) |
||
493 | |||
494 | /** |
||
495 | * @param int $type |
||
496 | * @return int string |
||
497 | */ |
||
498 | private function getSocketConnectorType($type) |
||
510 | |||
511 | /** |
||
512 | * @param int $type |
||
513 | * @return int string |
||
514 | */ |
||
515 | private function getSocketDisconnectorType($type) |
||
527 | |||
528 | /** |
||
529 | * @param Connection $conn |
||
530 | * @param string[] $message |
||
531 | */ |
||
532 | private function onRecvMessage(Connection $conn, $message) |
||
537 | |||
538 | /** |
||
539 | * @param Connection $conn |
||
540 | */ |
||
541 | private function onRecvHeartbeat(Connection $conn) |
||
545 | |||
546 | /** |
||
547 | * @param Connection $conn |
||
548 | * @param $message |
||
549 | * @return mixed |
||
550 | */ |
||
551 | private function recvMessage(Connection $conn, $message) |
||
555 | |||
556 | /** |
||
557 | * @param Connection $conn |
||
558 | */ |
||
559 | private function recvHeartbeat(Connection $conn) |
||
576 | |||
577 | /** |
||
578 | * |
||
579 | */ |
||
580 | private function fail() |
||
584 | |||
585 | /** |
||
586 | * @param string $id |
||
587 | * @return bool |
||
588 | */ |
||
589 | private function heartbeat($id) |
||
598 | |||
599 | /** |
||
600 | * |
||
601 | */ |
||
602 | private function startHeartbeat() |
||
621 | |||
622 | /** |
||
623 | * |
||
624 | */ |
||
625 | private function clearConnectionPool() |
||
634 | |||
635 | /** |
||
636 | * |
||
637 | */ |
||
638 | private function stopHeartbeat() |
||
646 | |||
647 | /** |
||
648 | * @param string $id |
||
649 | * @param string $type |
||
650 | * @param mixed $message |
||
651 | * @return null|string[] |
||
652 | */ |
||
653 | private function getFrame($id, $type, $message) |
||
684 | |||
685 | /** |
||
686 | * @param string $id |
||
687 | * @param string $type |
||
688 | * @param mixed $message |
||
689 | * @param int $flags |
||
690 | * @return bool |
||
691 | */ |
||
692 | private function sendMessage($id, $type, $message = null, $flags = self::MODE_STANDARD) |
||
725 | |||
726 | /** |
||
727 | * Start time register. |
||
728 | * |
||
729 | * Time register purpose is to cyclically increase timestamp representing last active tick of event loop. This method |
||
730 | * allows model to not mark external sockets wrongly as offline because of its own heavy load. |
||
731 | */ |
||
732 | private function startTimeRegister() |
||
745 | |||
746 | /** |
||
747 | * Stop time register. |
||
748 | * |
||
749 | * @see ZmqModel::startTimeRegister |
||
750 | */ |
||
751 | private function stopTimeRegister() |
||
760 | } |
||
761 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.