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 |
||
| 16 | class Client implements Interfaces\ClientInterface |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Socket resource |
||
| 20 | * |
||
| 21 | * @var resource|null |
||
| 22 | */ |
||
| 23 | private $_socket; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Code of error |
||
| 27 | * |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | private $_socket_err_num; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Description of socket error |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $_socket_err_str; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Configuration of connection |
||
| 41 | * |
||
| 42 | * @var \RouterOS\Config |
||
| 43 | */ |
||
| 44 | private $_config; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Client constructor. |
||
| 48 | * |
||
| 49 | * @param array|\RouterOS\Config $config |
||
| 50 | * @throws \RouterOS\Exceptions\ClientException |
||
| 51 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 52 | * @throws \RouterOS\Exceptions\QueryException |
||
| 53 | */ |
||
| 54 | 7 | public function __construct($config) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Get some parameter from config |
||
| 77 | * |
||
| 78 | * @param string $parameter Name of required parameter |
||
| 79 | * @return mixed |
||
| 80 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 81 | */ |
||
| 82 | 7 | private function config(string $parameter) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Return socket resource if is exist |
||
| 89 | * |
||
| 90 | * @return \RouterOS\Config |
||
| 91 | * @since 0.6 |
||
| 92 | */ |
||
| 93 | public function getConfig(): Config |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Set configuration of client |
||
| 100 | * |
||
| 101 | * @param \RouterOS\Config $config |
||
| 102 | * @since 0.7 |
||
| 103 | */ |
||
| 104 | 7 | public function setConfig(Config $config) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Encode given length in RouterOS format |
||
| 111 | * |
||
| 112 | * @param string $string |
||
| 113 | * @return string Encoded length |
||
| 114 | * @throws \RouterOS\Exceptions\ClientException |
||
| 115 | */ |
||
| 116 | 6 | private function encodeLength(string $string): string |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Read length of line |
||
| 156 | * |
||
| 157 | * @param int $byte |
||
| 158 | * @return int |
||
| 159 | */ |
||
| 160 | 6 | private function getLength(int $byte): int |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Send write query to RouterOS (with or without tag) |
||
| 191 | * |
||
| 192 | * @param string|array|\RouterOS\Query $query |
||
| 193 | * @return \RouterOS\Client |
||
| 194 | * @throws \RouterOS\Exceptions\ClientException |
||
| 195 | * @throws \RouterOS\Exceptions\QueryException |
||
| 196 | */ |
||
| 197 | 6 | public function write($query): Client |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Read answer from server after query was executed |
||
| 224 | * |
||
| 225 | * @param bool $parse |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | 6 | public function read(bool $parse = true): array |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Alias for ->write() method |
||
| 267 | * |
||
| 268 | * @param string|array|\RouterOS\Query $query |
||
| 269 | * @return \RouterOS\Client |
||
| 270 | * @throws \RouterOS\Exceptions\ClientException |
||
| 271 | * @throws \RouterOS\Exceptions\QueryException |
||
| 272 | */ |
||
| 273 | public function w($query): Client |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Alias for ->read() method |
||
| 280 | * |
||
| 281 | * @param bool $parse |
||
| 282 | * @return array |
||
| 283 | * @since 0.7 |
||
| 284 | */ |
||
| 285 | public function r(bool $parse = true): array |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Alias for ->write()->read() combination of methods |
||
| 292 | * |
||
| 293 | * @param string|array|\RouterOS\Query $query |
||
| 294 | * @param bool $parse |
||
| 295 | * @return array |
||
| 296 | * @throws \RouterOS\Exceptions\ClientException |
||
| 297 | * @throws \RouterOS\Exceptions\QueryException |
||
| 298 | * @since 0.6 |
||
| 299 | */ |
||
| 300 | public function wr($query, bool $parse = true): array |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Parse response from Router OS |
||
| 307 | * |
||
| 308 | * @param array $response Response data |
||
| 309 | * @return array Array with parsed data |
||
| 310 | */ |
||
| 311 | 2 | private function parseResponse(array $response): array |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Parse result from RouterOS by regular expression |
||
| 350 | * |
||
| 351 | * @param string $value |
||
| 352 | * @param array $matches |
||
| 353 | */ |
||
| 354 | 2 | private function pregResponse(string $value, &$matches) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Authorization logic |
||
| 361 | * |
||
| 362 | * @return bool |
||
| 363 | * @throws \RouterOS\Exceptions\ClientException |
||
| 364 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 365 | * @throws \RouterOS\Exceptions\QueryException |
||
| 366 | */ |
||
| 367 | 6 | private function login(): bool |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Connect to socket server |
||
| 395 | * |
||
| 396 | * @return bool |
||
| 397 | * @throws \RouterOS\Exceptions\ClientException |
||
| 398 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 399 | * @throws \RouterOS\Exceptions\QueryException |
||
| 400 | */ |
||
| 401 | 7 | private function connect(): bool |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Save socket resource to static variable |
||
| 435 | * |
||
| 436 | * @param resource $socket |
||
| 437 | */ |
||
| 438 | 6 | private function setSocket($socket) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Return socket resource if is exist |
||
| 445 | * |
||
| 446 | * @return resource |
||
| 447 | */ |
||
| 448 | 6 | public function getSocket() |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Initiate socket session |
||
| 455 | * |
||
| 456 | * @throws \RouterOS\Exceptions\ClientException |
||
| 457 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 458 | */ |
||
| 459 | 7 | private function openSocket() |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Close socket session |
||
| 494 | * |
||
| 495 | * @return bool |
||
| 496 | */ |
||
| 497 | 1 | private function closeSocket(): bool |
|
| 501 | } |
||
| 502 |