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 |
||
| 14 | class Client implements Interfaces\ClientInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Socket resource |
||
| 18 | * |
||
| 19 | * @var resource|null |
||
| 20 | */ |
||
| 21 | private $_socket; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Code of error |
||
| 25 | * |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | private $_socket_err_num; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Description of socket error |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $_socket_err_str; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Configuration of connection |
||
| 39 | * |
||
| 40 | * @var \RouterOS\Config |
||
| 41 | */ |
||
| 42 | private $_config; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Client constructor. |
||
| 46 | * |
||
| 47 | * @param array|\RouterOS\Config $config |
||
| 48 | * @throws ConfigException |
||
| 49 | * @throws ClientException |
||
| 50 | */ |
||
| 51 | 5 | public function __construct($config) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Check for important keys |
||
| 72 | * |
||
| 73 | * @param array $keys |
||
| 74 | * @param Config $config |
||
| 75 | * @throws ConfigException |
||
| 76 | */ |
||
| 77 | 5 | private function exceptionIfKeyNotExist(array $keys, Config $config) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Get some parameter from config |
||
| 89 | * |
||
| 90 | * @param string $parameter Name of required parameter |
||
| 91 | * @return mixed |
||
| 92 | * @throws ConfigException |
||
| 93 | */ |
||
| 94 | 5 | private function config(string $parameter) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Return socket resource if is exist |
||
| 101 | * |
||
| 102 | * @return \RouterOS\Config |
||
| 103 | * @since 0.6 |
||
| 104 | */ |
||
| 105 | public function getConfig(): Config |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Encode given length in RouterOS format |
||
| 112 | * |
||
| 113 | * @param string $string |
||
| 114 | * @return string Encoded length |
||
| 115 | * @throws ClientException |
||
| 116 | */ |
||
| 117 | 4 | private function encodeLength(string $string): string |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Read length of line |
||
| 157 | * |
||
| 158 | * @param int $byte |
||
| 159 | * @return int |
||
| 160 | */ |
||
| 161 | 4 | private function getLength(int $byte): int |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Alias for ->write()->read() combination of methods |
||
| 196 | * |
||
| 197 | * @param Query $query |
||
| 198 | * @param bool $parse |
||
| 199 | * @return array |
||
| 200 | * @throws ClientException |
||
| 201 | * @since 0.6 |
||
| 202 | */ |
||
| 203 | public function wr(Query $query, bool $parse = true): array |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Send write query to RouterOS (with or without tag) |
||
| 210 | * |
||
| 211 | * @param Query $query |
||
| 212 | * @return Client |
||
| 213 | * @throws ClientException |
||
| 214 | */ |
||
| 215 | 4 | public function write(Query $query): Client |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Read answer from server after query was executed |
||
| 231 | * |
||
| 232 | * @param bool $parse |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | 4 | public function read(bool $parse = true): array |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Parse response from Router OS |
||
| 274 | * |
||
| 275 | * @param array $response Response data |
||
| 276 | * @return array Array with parsed data |
||
| 277 | */ |
||
| 278 | 2 | private function parseResponse(array $response): array |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Parse result from RouterOS by regular expression |
||
| 317 | * |
||
| 318 | * @param string $value |
||
| 319 | * @param array $matches |
||
| 320 | */ |
||
| 321 | 2 | private function pregResponse(string $value, &$matches) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Authorization logic |
||
| 328 | * |
||
| 329 | * @return bool |
||
| 330 | * @throws ClientException |
||
| 331 | * @throws ConfigException |
||
| 332 | */ |
||
| 333 | 4 | private function login(): bool |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Connect to socket server |
||
| 361 | * |
||
| 362 | * @return bool |
||
| 363 | * @throws ClientException |
||
| 364 | * @throws ConfigException |
||
| 365 | */ |
||
| 366 | 5 | private function connect(): bool |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Save socket resource to static variable |
||
| 400 | * |
||
| 401 | * @param resource $socket |
||
| 402 | */ |
||
| 403 | 4 | private function setSocket($socket) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Return socket resource if is exist |
||
| 410 | * |
||
| 411 | * @return resource |
||
| 412 | */ |
||
| 413 | 4 | public function getSocket() |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Initiate socket session |
||
| 420 | * |
||
| 421 | * @throws ClientException |
||
| 422 | * @throws ConfigException |
||
| 423 | */ |
||
| 424 | 5 | private function openSocket() |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Close socket session |
||
| 459 | * |
||
| 460 | * @return bool |
||
| 461 | */ |
||
| 462 | 1 | private function closeSocket(): bool |
|
| 466 | } |
||
| 467 |