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 |
||
32 | 1 | class Client implements IClient |
|
33 | { |
||
34 | /** |
||
35 | * Implement nette smart magic |
||
36 | */ |
||
37 | 1 | use Nette\SmartObject; |
|
38 | |||
39 | const VERSION = '0.1.0'; |
||
40 | |||
41 | const TYPE_ID_WELCOME = 0; |
||
42 | const TYPE_ID_PREFIX = 1; |
||
43 | const TYPE_ID_CALL = 2; |
||
44 | const TYPE_ID_CALL_RESULT = 3; |
||
45 | const TYPE_ID_CALL_ERROR = 4; |
||
46 | const TYPE_ID_SUBSCRIBE = 5; |
||
47 | const TYPE_ID_UNSUBSCRIBE = 6; |
||
48 | const TYPE_ID_PUBLISH = 7; |
||
49 | const TYPE_ID_EVENT = 8; |
||
50 | |||
51 | /** |
||
52 | * @var \Closure |
||
53 | */ |
||
54 | public $onConnect = []; |
||
55 | |||
56 | /** |
||
57 | * @var \Closure |
||
58 | */ |
||
59 | public $onOpen = []; |
||
60 | |||
61 | /** |
||
62 | * @var \Closure |
||
63 | */ |
||
64 | public $onEvent = []; |
||
65 | |||
66 | /** |
||
67 | * @var EventLoop\LoopInterface |
||
68 | */ |
||
69 | private $loop; |
||
70 | |||
71 | /** |
||
72 | * @var Configuration |
||
73 | */ |
||
74 | private $configuration; |
||
75 | |||
76 | /** |
||
77 | * @var Stream\DuplexResourceStream |
||
78 | */ |
||
79 | private $stream; |
||
80 | |||
81 | /** |
||
82 | * @var bool |
||
83 | */ |
||
84 | private $connected = FALSE; |
||
85 | |||
86 | /** |
||
87 | * @var callable[] |
||
88 | */ |
||
89 | private $sucessCallbacks = []; |
||
90 | |||
91 | /** |
||
92 | * @var callable[] |
||
93 | */ |
||
94 | private $errorCallbacks = []; |
||
95 | |||
96 | /** |
||
97 | * @param EventLoop\LoopInterface $loop |
||
98 | * @param Configuration $configuration |
||
99 | */ |
||
100 | function __construct(EventLoop\LoopInterface $loop, Configuration $configuration) |
||
105 | |||
106 | /** |
||
107 | * Disconnect on destruct |
||
108 | */ |
||
109 | function __destruct() |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function setLoop(EventLoop\LoopInterface $loop) : void |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | public function getLoop() : EventLoop\LoopInterface |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function connect() : void |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | public function disconnect() : void |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | public function isConnected() : bool |
||
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | public function publish(string $topicUri, string $event, array $exclude = [], array $eligible = []) : void |
||
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | public function subscribe(string $topicUri) : void |
||
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | public function unsubscribe(string $topicUri) : void |
||
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | public function call(string $processUri, array $args, callable $successCallback = NULL, callable $errorCallback = NULL) : void |
||
234 | |||
235 | /** |
||
236 | * @param mixed $data |
||
237 | * @param array $header |
||
238 | * |
||
239 | * @return void |
||
240 | */ |
||
241 | private function receiveData($data, array $header) : void |
||
300 | |||
301 | /** |
||
302 | * @param array $data |
||
303 | * @param string $type |
||
304 | * @param bool $masked |
||
305 | * |
||
306 | * @return void |
||
307 | * |
||
308 | * @throws Utils\JsonException |
||
309 | */ |
||
310 | private function sendData(array $data, string $type = 'text', bool $masked = TRUE) : void |
||
320 | |||
321 | /** |
||
322 | * Parse received data |
||
323 | * |
||
324 | * @param array $response |
||
325 | * |
||
326 | * @return void |
||
327 | * |
||
328 | * @throws Utils\JsonException |
||
329 | */ |
||
330 | private function parseData(array $response) : void |
||
358 | |||
359 | /** |
||
360 | * Create header for web socket client |
||
361 | * |
||
362 | * @return string |
||
363 | */ |
||
364 | private function createHeader() : string |
||
385 | |||
386 | /** |
||
387 | * Parse raw incoming data |
||
388 | * |
||
389 | * @param string $header |
||
390 | * |
||
391 | * @return mixed[] |
||
392 | */ |
||
393 | private function parseChunk(string $header) : array |
||
426 | |||
427 | /** |
||
428 | * Generate token |
||
429 | * |
||
430 | * @param int $length |
||
431 | * |
||
432 | * @return string |
||
433 | */ |
||
434 | private function generateAlphaNumToken(int $length) : string |
||
449 | |||
450 | /** |
||
451 | * @param string $payload |
||
452 | * @param string $type |
||
453 | * @param bool $masked |
||
454 | * |
||
455 | * @return bool|string |
||
456 | */ |
||
457 | private function hybi10Encode(string $payload, string $type = 'text', bool $masked = TRUE) |
||
537 | |||
538 | /** |
||
539 | * @param $data |
||
540 | * |
||
541 | * @return string|NULL |
||
542 | */ |
||
543 | private function hybi10Decode($data) : ?string |
||
587 | } |
||
588 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.