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 | use SocketTrait; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Socket resource |
||
| 22 | * |
||
| 23 | * @var resource|null |
||
| 24 | */ |
||
| 25 | private $_socket; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Code of error |
||
| 29 | * |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | private $_socket_err_num; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Description of socket error |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $_socket_err_str; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Configuration of connection |
||
| 43 | * |
||
| 44 | * @var \RouterOS\Config |
||
| 45 | */ |
||
| 46 | private $_config; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Client constructor. |
||
| 50 | * |
||
| 51 | * @param array|\RouterOS\Config $config |
||
| 52 | * @throws \RouterOS\Exceptions\ClientException |
||
| 53 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 54 | * @throws \RouterOS\Exceptions\QueryException |
||
| 55 | */ |
||
| 56 | 7 | public function __construct($config) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Get some parameter from config |
||
| 79 | * |
||
| 80 | * @param string $parameter Name of required parameter |
||
| 81 | * @return mixed |
||
| 82 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 83 | */ |
||
| 84 | 7 | private function config(string $parameter) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Return socket resource if is exist |
||
| 91 | * |
||
| 92 | * @return \RouterOS\Config |
||
| 93 | * @since 0.6 |
||
| 94 | */ |
||
| 95 | public function getConfig(): Config |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Set configuration of client |
||
| 102 | * |
||
| 103 | * @param \RouterOS\Config $config |
||
| 104 | * @since 0.7 |
||
| 105 | */ |
||
| 106 | 7 | public function setConfig(Config $config) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Encode given length in RouterOS format |
||
| 113 | * |
||
| 114 | * @param string $string |
||
| 115 | * @return string Encoded length |
||
| 116 | * @throws \RouterOS\Exceptions\ClientException |
||
| 117 | */ |
||
| 118 | 6 | private function encodeLength(string $string): string |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Read length of line |
||
| 158 | * |
||
| 159 | * @param int $byte |
||
| 160 | * @return int |
||
| 161 | */ |
||
| 162 | 6 | private function getLength(int $byte): int |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Send write query to RouterOS (with or without tag) |
||
| 193 | * |
||
| 194 | * @param string|array|\RouterOS\Query $query |
||
| 195 | * @return \RouterOS\Client |
||
| 196 | * @throws \RouterOS\Exceptions\ClientException |
||
| 197 | * @throws \RouterOS\Exceptions\QueryException |
||
| 198 | */ |
||
| 199 | 6 | public function write($query): Client |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Read answer from server after query was executed |
||
| 226 | * |
||
| 227 | * @param bool $parse |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | 6 | public function read(bool $parse = true): array |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Alias for ->write() method |
||
| 266 | * |
||
| 267 | * @param string|array|\RouterOS\Query $query |
||
| 268 | * @return \RouterOS\Client |
||
| 269 | * @throws \RouterOS\Exceptions\ClientException |
||
| 270 | * @throws \RouterOS\Exceptions\QueryException |
||
| 271 | */ |
||
| 272 | public function w($query): Client |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Alias for ->read() method |
||
| 279 | * |
||
| 280 | * @param bool $parse |
||
| 281 | * @return array |
||
| 282 | * @since 0.7 |
||
| 283 | */ |
||
| 284 | public function r(bool $parse = true): array |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Alias for ->write()->read() combination of methods |
||
| 291 | * |
||
| 292 | * @param string|array|\RouterOS\Query $query |
||
| 293 | * @param bool $parse |
||
| 294 | * @return array |
||
| 295 | * @throws \RouterOS\Exceptions\ClientException |
||
| 296 | * @throws \RouterOS\Exceptions\QueryException |
||
| 297 | * @since 0.6 |
||
| 298 | */ |
||
| 299 | public function wr($query, bool $parse = true): array |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Parse response from Router OS |
||
| 306 | * |
||
| 307 | * @param array $response Response data |
||
| 308 | * @return array Array with parsed data |
||
| 309 | */ |
||
| 310 | 2 | private function parseResponse(array $response): array |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Parse result from RouterOS by regular expression |
||
| 349 | * |
||
| 350 | * @param string $value |
||
| 351 | * @param array $matches |
||
| 352 | */ |
||
| 353 | 2 | private function pregResponse(string $value, &$matches) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Authorization logic |
||
| 360 | * |
||
| 361 | * @return bool |
||
| 362 | * @throws \RouterOS\Exceptions\ClientException |
||
| 363 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 364 | * @throws \RouterOS\Exceptions\QueryException |
||
| 365 | */ |
||
| 366 | 6 | private function login(): bool |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Connect to socket server |
||
| 394 | * |
||
| 395 | * @return bool |
||
| 396 | * @throws \RouterOS\Exceptions\ClientException |
||
| 397 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 398 | * @throws \RouterOS\Exceptions\QueryException |
||
| 399 | */ |
||
| 400 | 7 | private function connect(): bool |
|
| 431 | |||
| 432 | } |
||
| 433 |