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 | * @var resource|null |
||
| 21 | */ |
||
| 22 | private $_socket; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Code of error |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | private $_socket_err_num; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Description of socket error |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $_socket_err_str; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Configuration of connection |
||
| 38 | * @var ConfigInterface |
||
| 39 | */ |
||
| 40 | private $_config; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Client constructor. |
||
| 44 | * |
||
| 45 | * @param ConfigInterface $config |
||
| 46 | * @throws ConfigException |
||
| 47 | * @throws ClientException |
||
| 48 | */ |
||
| 49 | 5 | public function __construct(ConfigInterface $config) |
|
| 50 | { |
||
| 51 | // Check for important keys |
||
| 52 | 5 | $this->exceptionIfKeyNotExist(['host', 'user', 'pass'], $config); |
|
| 53 | |||
| 54 | // Save config if everything is okay |
||
| 55 | 5 | $this->_config = $config; |
|
| 56 | |||
| 57 | // Throw error if cannot to connect |
||
| 58 | 5 | if (false === $this->connect()) { |
|
| 59 | 1 | throw new ClientException('Unable to connect to ' . $config->get('host') . ':' . $config->get('port')); |
|
| 60 | } |
||
| 61 | 3 | } |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Check for important keys |
||
| 65 | * |
||
| 66 | * @param array $keys |
||
| 67 | * @param ConfigInterface $config |
||
| 68 | * @throws ConfigException |
||
| 69 | */ |
||
| 70 | 5 | private function exceptionIfKeyNotExist(array $keys, ConfigInterface $config) |
|
| 71 | { |
||
| 72 | 5 | $parameters = $config->getParameters(); |
|
| 73 | 5 | foreach ($keys as $key) { |
|
| 74 | 5 | if (!array_key_exists($key, $parameters) && isset($parameters[$key])) { |
|
| 75 | 5 | throw new ConfigException("Parameter '$key' of Config is not set or empty"); |
|
| 76 | } |
||
| 77 | } |
||
| 78 | 5 | } |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Get some parameter from config |
||
| 82 | * |
||
| 83 | * @param string $parameter |
||
| 84 | * @return mixed |
||
| 85 | */ |
||
| 86 | 5 | private function config(string $parameter) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Encode given length in RouterOS format |
||
| 93 | * |
||
| 94 | * @param string $string |
||
| 95 | * @return string Encoded length |
||
| 96 | * @throws ClientException |
||
| 97 | */ |
||
| 98 | 4 | private function encodeLength(string $string): string |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Read length of line |
||
| 138 | * |
||
| 139 | * @param int $byte |
||
| 140 | * @return int |
||
| 141 | */ |
||
| 142 | 4 | private function getLength(int $byte): int |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Send write query to RouterOS (with or without tag) |
||
| 177 | * |
||
| 178 | * @param QueryInterface $query |
||
| 179 | * @return ClientInterface |
||
| 180 | * @throws ClientException |
||
| 181 | */ |
||
| 182 | 4 | public function write(QueryInterface $query): ClientInterface |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Read answer from server after query was executed |
||
| 198 | * |
||
| 199 | * @param bool $parse |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | 4 | public function read(bool $parse = true): array |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Parse response from Router OS |
||
| 241 | * |
||
| 242 | * @param array $response Response data |
||
| 243 | * @return array Array with parsed data |
||
| 244 | */ |
||
| 245 | 2 | private function parseResponse(array $response): array |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Parse result from RouterOS by regular expression |
||
| 282 | * |
||
| 283 | * @param string $value |
||
| 284 | * @param array $matches |
||
| 285 | */ |
||
| 286 | 2 | private function pregResponse(string $value, &$matches) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Authorization logic |
||
| 293 | * |
||
| 294 | * @return bool |
||
| 295 | * @throws ClientException |
||
| 296 | */ |
||
| 297 | 4 | private function login(): bool |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Connect to socket server |
||
| 326 | * |
||
| 327 | * @return bool |
||
| 328 | * @throws ClientException |
||
| 329 | */ |
||
| 330 | 5 | private function connect(): bool |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Save socket resource to static variable |
||
| 364 | * |
||
| 365 | * @param resource $socket |
||
| 366 | */ |
||
| 367 | 4 | private function setSocket($socket) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Return socket resource if is exist |
||
| 374 | * |
||
| 375 | * @return resource |
||
| 376 | */ |
||
| 377 | 4 | public function getSocket() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Initiate socket session |
||
| 384 | * |
||
| 385 | * @throws ClientException |
||
| 386 | */ |
||
| 387 | 5 | private function openSocket() |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Close socket session |
||
| 422 | * |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | 1 | private function closeSocket(): bool |
|
| 429 | } |
||
| 430 |