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 \RouterOS\Exceptions\ClientException |
||
| 49 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 50 | * @throws \RouterOS\Exceptions\QueryException |
||
| 51 | */ |
||
| 52 | 5 | public function __construct($config) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Check for important keys |
||
| 73 | * |
||
| 74 | * @param array $keys |
||
| 75 | * @param \RouterOS\Config $config |
||
| 76 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 77 | */ |
||
| 78 | 5 | private function exceptionIfKeyNotExist(array $keys, Config $config) |
|
| 79 | { |
||
| 80 | 5 | $parameters = $config->getParameters(); |
|
| 81 | 5 | foreach ($keys as $key) { |
|
| 82 | 5 | if (!array_key_exists($key, $parameters) && isset($parameters[$key])) { |
|
| 83 | 5 | throw new ConfigException("Parameter '$key' of Config is not set or empty"); |
|
| 84 | } |
||
| 85 | } |
||
| 86 | 5 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Get some parameter from config |
||
| 90 | * |
||
| 91 | * @param string $parameter Name of required parameter |
||
| 92 | * @return mixed |
||
| 93 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 94 | */ |
||
| 95 | 5 | private function config(string $parameter) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Return socket resource if is exist |
||
| 102 | * |
||
| 103 | * @return \RouterOS\Config |
||
| 104 | * @since 0.6 |
||
| 105 | */ |
||
| 106 | public function getConfig(): Config |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set configuration of client |
||
| 113 | * |
||
| 114 | * @param \RouterOS\Config $config |
||
| 115 | * @since 0.7 |
||
| 116 | */ |
||
| 117 | 5 | public function setConfig(Config $config) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Encode given length in RouterOS format |
||
| 124 | * |
||
| 125 | * @param string $string |
||
| 126 | * @return string Encoded length |
||
| 127 | * @throws \RouterOS\Exceptions\ClientException |
||
| 128 | */ |
||
| 129 | 4 | private function encodeLength(string $string): string |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Read length of line |
||
| 169 | * |
||
| 170 | * @param int $byte |
||
| 171 | * @return int |
||
| 172 | */ |
||
| 173 | 4 | private function getLength(int $byte): int |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Send write query to RouterOS (with or without tag) |
||
| 208 | * |
||
| 209 | * @param \RouterOS\Query $query |
||
| 210 | * @return \RouterOS\Client |
||
| 211 | * @throws \RouterOS\Exceptions\ClientException |
||
| 212 | * @throws \RouterOS\Exceptions\QueryException |
||
| 213 | */ |
||
| 214 | 4 | public function write(Query $query): Client |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Read answer from server after query was executed |
||
| 230 | * |
||
| 231 | * @param bool $parse |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | 4 | public function read(bool $parse = true): array |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Alias for ->write() method |
||
| 273 | * |
||
| 274 | * @param \RouterOS\Query $query |
||
| 275 | * @return \RouterOS\Client |
||
| 276 | * @throws \RouterOS\Exceptions\ClientException |
||
| 277 | * @throws \RouterOS\Exceptions\QueryException |
||
| 278 | */ |
||
| 279 | public function w(Query $query): Client |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Alias for ->read() method |
||
| 286 | * |
||
| 287 | * @param bool $parse |
||
| 288 | * @return array |
||
| 289 | * @since 0.7 |
||
| 290 | */ |
||
| 291 | public function r(bool $parse = true): array |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Alias for ->write()->read() combination of methods |
||
| 298 | * |
||
| 299 | * @param \RouterOS\Query $query |
||
| 300 | * @param bool $parse |
||
| 301 | * @return array |
||
| 302 | * @throws \RouterOS\Exceptions\ClientException |
||
| 303 | * @throws \RouterOS\Exceptions\QueryException |
||
| 304 | * @since 0.6 |
||
| 305 | */ |
||
| 306 | public function wr(Query $query, bool $parse = true): array |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Parse response from Router OS |
||
| 313 | * |
||
| 314 | * @param array $response Response data |
||
| 315 | * @return array Array with parsed data |
||
| 316 | */ |
||
| 317 | 1 | private function parseResponse(array $response): array |
|
| 318 | { |
||
| 319 | 1 | $result = []; |
|
| 320 | 1 | $i = -1; |
|
| 321 | 1 | $lines = \count($response); |
|
| 322 | 1 | foreach ($response as $key => $value) { |
|
| 323 | switch ($value) { |
||
| 324 | 1 | case '!re': |
|
| 325 | $i++; |
||
| 326 | break; |
||
| 327 | 1 | case '!fatal': |
|
| 328 | $result = $response; |
||
| 329 | break 2; |
||
| 330 | 1 | case '!trap': |
|
| 331 | 1 | case '!done': |
|
| 332 | // Check for =ret=, .tag and any other following messages |
||
| 333 | 1 | for ($j = $key + 1; $j <= $lines; $j++) { |
|
| 334 | // If we have lines after current one |
||
| 335 | 1 | if (isset($response[$j])) { |
|
| 336 | 1 | $this->pregResponse($response[$j], $matches); |
|
| 337 | 1 | if (!empty($matches)) { |
|
| 338 | 1 | $result['after'][$matches[1][0]] = $matches[2][0]; |
|
| 339 | } |
||
| 340 | } |
||
| 341 | } |
||
| 342 | 1 | break 2; |
|
| 343 | default: |
||
| 344 | $this->pregResponse($value, $matches); |
||
| 345 | if (!empty($matches)) { |
||
| 346 | $result[$i][$matches[1][0]] = $matches[2][0]; |
||
| 347 | } |
||
| 348 | break; |
||
| 349 | } |
||
| 350 | } |
||
| 351 | 1 | return $result; |
|
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Parse result from RouterOS by regular expression |
||
| 356 | * |
||
| 357 | * @param string $value |
||
| 358 | * @param array $matches |
||
| 359 | */ |
||
| 360 | 1 | private function pregResponse(string $value, &$matches) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Authorization logic |
||
| 367 | * |
||
| 368 | * @return bool |
||
| 369 | * @throws \RouterOS\Exceptions\ClientException |
||
| 370 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 371 | * @throws \RouterOS\Exceptions\QueryException |
||
| 372 | */ |
||
| 373 | 4 | private function login(): bool |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Connect to socket server |
||
| 401 | * |
||
| 402 | * @return bool |
||
| 403 | * @throws \RouterOS\Exceptions\ClientException |
||
| 404 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 405 | * @throws \RouterOS\Exceptions\QueryException |
||
| 406 | */ |
||
| 407 | 5 | private function connect(): bool |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Save socket resource to static variable |
||
| 441 | * |
||
| 442 | * @param resource $socket |
||
| 443 | */ |
||
| 444 | 4 | private function setSocket($socket) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Return socket resource if is exist |
||
| 451 | * |
||
| 452 | * @return resource |
||
| 453 | */ |
||
| 454 | 4 | public function getSocket() |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Initiate socket session |
||
| 461 | * |
||
| 462 | * @throws \RouterOS\Exceptions\ClientException |
||
| 463 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 464 | */ |
||
| 465 | 5 | private function openSocket() |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Close socket session |
||
| 500 | * |
||
| 501 | * @return bool |
||
| 502 | */ |
||
| 503 | private function closeSocket(): bool |
||
| 507 | } |
||
| 508 |