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_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 array |
||
| 88 | */ |
||
| 89 | private $callbacks = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param EventLoop\LoopInterface $loop |
||
| 93 | * @param Configuration $configuration |
||
| 94 | */ |
||
| 95 | function __construct(EventLoop\LoopInterface $loop, Configuration $configuration) |
||
|
|
|||
| 96 | { |
||
| 97 | 1 | $this->loop = $loop; |
|
| 98 | 1 | $this->configuration = $configuration; |
|
| 99 | 1 | } |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Disconnect on destruct |
||
| 103 | */ |
||
| 104 | function __destruct() |
||
| 105 | { |
||
| 106 | 1 | $this->disconnect(); |
|
| 107 | 1 | } |
|
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function setLoop(EventLoop\LoopInterface $loop) : void |
||
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc} |
||
| 119 | */ |
||
| 120 | public function getLoop() : EventLoop\LoopInterface |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | public function connect() : void |
||
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | */ |
||
| 157 | public function disconnect() : void |
||
| 158 | { |
||
| 159 | 1 | $this->connected = FALSE; |
|
| 160 | |||
| 161 | 1 | if ($this->stream instanceof Stream\DuplexStreamInterface) { |
|
| 162 | $this->stream->close(); |
||
| 163 | } |
||
| 164 | 1 | } |
|
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | public function isConnected() : bool |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | public function publish(string $topicUri, string $event, array $exclude = [], array $eligible = []) : void |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | public function subscribe(string $topicUri) : void |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritdoc} |
||
| 201 | */ |
||
| 202 | public function unsubscribe(string $topicUri) : void |
||
| 209 | |||
| 210 | /** |
||
| 211 | * {@inheritdoc} |
||
| 212 | */ |
||
| 213 | public function call(string $processUri, array $args, callable $callback = NULL) : void |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param mixed $data |
||
| 231 | * @param array $header |
||
| 232 | * |
||
| 233 | * @return void |
||
| 234 | */ |
||
| 235 | private function receiveData($data, array $header) : void |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param array $data |
||
| 271 | * @param string $type |
||
| 272 | * @param bool $masked |
||
| 273 | * |
||
| 274 | * @return void |
||
| 275 | * |
||
| 276 | * @throws Utils\JsonException |
||
| 277 | */ |
||
| 278 | private function sendData(array $data, string $type = 'text', bool $masked = TRUE) : void |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Parse received data |
||
| 291 | * |
||
| 292 | * @param array $response |
||
| 293 | * |
||
| 294 | * @return void |
||
| 295 | * |
||
| 296 | * @throws Utils\JsonException |
||
| 297 | */ |
||
| 298 | private function parseData(array $response) : void |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Create header for web socket client |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | private function createHeader() : string |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Parse raw incoming data |
||
| 354 | * |
||
| 355 | * @param string $header |
||
| 356 | * |
||
| 357 | * @return mixed[] |
||
| 358 | */ |
||
| 359 | private function parseChunk(string $header) : array |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Generate token |
||
| 395 | * |
||
| 396 | * @param int $length |
||
| 397 | * |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | private function generateAlphaNumToken(int $length) : string |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param string $payload |
||
| 418 | * @param string $type |
||
| 419 | * @param bool $masked |
||
| 420 | * |
||
| 421 | * @return bool|string |
||
| 422 | */ |
||
| 423 | private function hybi10Encode(string $payload, string $type = 'text', bool $masked = TRUE) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param $data |
||
| 506 | * |
||
| 507 | * @return string|NULL |
||
| 508 | */ |
||
| 509 | private function hybi10Decode($data) : ?string |
||
| 553 | } |
||
| 554 |
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.