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 |
||
| 15 | class Client implements Interfaces\ClientInterface |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Socket resource |
||
| 19 | * |
||
| 20 | * @var resource|null |
||
| 21 | */ |
||
| 22 | private $_socket; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Code of error |
||
| 26 | * |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | private $_socket_err_num; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Description of socket error |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $_socket_err_str; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Configuration of connection |
||
| 40 | * |
||
| 41 | * @var \RouterOS\Config |
||
| 42 | */ |
||
| 43 | private $_config; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Client constructor. |
||
| 47 | * |
||
| 48 | * @param array|\RouterOS\Config $config |
||
| 49 | * @throws \RouterOS\Exceptions\ClientException |
||
| 50 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 51 | * @throws \RouterOS\Exceptions\QueryException |
||
| 52 | */ |
||
| 53 | 5 | public function __construct($config) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Get some parameter from config |
||
| 76 | * |
||
| 77 | * @param string $parameter Name of required parameter |
||
| 78 | * @return mixed |
||
| 79 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 80 | */ |
||
| 81 | 5 | private function config(string $parameter) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Return socket resource if is exist |
||
| 88 | * |
||
| 89 | * @return \RouterOS\Config |
||
| 90 | * @since 0.6 |
||
| 91 | */ |
||
| 92 | public function getConfig(): Config |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Set configuration of client |
||
| 99 | * |
||
| 100 | * @param \RouterOS\Config $config |
||
| 101 | * @since 0.7 |
||
| 102 | */ |
||
| 103 | 5 | public function setConfig(Config $config) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Encode given length in RouterOS format |
||
| 110 | * |
||
| 111 | * @param string $string |
||
| 112 | * @return string Encoded length |
||
| 113 | * @throws \RouterOS\Exceptions\ClientException |
||
| 114 | */ |
||
| 115 | 4 | private function encodeLength(string $string): string |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Read length of line |
||
| 155 | * |
||
| 156 | * @param int $byte |
||
| 157 | * @return int |
||
| 158 | */ |
||
| 159 | 4 | private function getLength(int $byte): int |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Send write query to RouterOS (with or without tag) |
||
| 194 | * |
||
| 195 | * @param \RouterOS\Query $query |
||
| 196 | * @return \RouterOS\Client |
||
| 197 | * @throws \RouterOS\Exceptions\ClientException |
||
| 198 | * @throws \RouterOS\Exceptions\QueryException |
||
| 199 | */ |
||
| 200 | 4 | public function write(Query $query): Client |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Read answer from server after query was executed |
||
| 216 | * |
||
| 217 | * @param bool $parse |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | 4 | public function read(bool $parse = true): array |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Alias for ->write() method |
||
| 259 | * |
||
| 260 | * @param \RouterOS\Query $query |
||
| 261 | * @return \RouterOS\Client |
||
| 262 | * @throws \RouterOS\Exceptions\ClientException |
||
| 263 | * @throws \RouterOS\Exceptions\QueryException |
||
| 264 | */ |
||
| 265 | public function w(Query $query): Client |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Alias for ->read() method |
||
| 272 | * |
||
| 273 | * @param bool $parse |
||
| 274 | * @return array |
||
| 275 | * @since 0.7 |
||
| 276 | */ |
||
| 277 | public function r(bool $parse = true): array |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Alias for ->write()->read() combination of methods |
||
| 284 | * |
||
| 285 | * @param \RouterOS\Query $query |
||
| 286 | * @param bool $parse |
||
| 287 | * @return array |
||
| 288 | * @throws \RouterOS\Exceptions\ClientException |
||
| 289 | * @throws \RouterOS\Exceptions\QueryException |
||
| 290 | * @since 0.6 |
||
| 291 | */ |
||
| 292 | public function wr(Query $query, bool $parse = true): array |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Parse response from Router OS |
||
| 299 | * |
||
| 300 | * @param array $response Response data |
||
| 301 | * @return array Array with parsed data |
||
| 302 | */ |
||
| 303 | 1 | private function parseResponse(array $response): array |
|
| 304 | { |
||
| 305 | 1 | $result = []; |
|
| 306 | 1 | $i = -1; |
|
| 307 | 1 | $lines = \count($response); |
|
| 308 | 1 | foreach ($response as $key => $value) { |
|
| 309 | 1 | switch ($value) { |
|
| 310 | 1 | case '!re': |
|
| 311 | $i++; |
||
| 312 | break; |
||
| 313 | 1 | case '!fatal': |
|
| 314 | $result = $response; |
||
| 315 | break 2; |
||
| 316 | 1 | case '!trap': |
|
| 317 | 1 | case '!done': |
|
| 318 | // Check for =ret=, .tag and any other following messages |
||
| 319 | 1 | for ($j = $key + 1; $j <= $lines; $j++) { |
|
| 320 | // If we have lines after current one |
||
| 321 | 1 | if (isset($response[$j])) { |
|
| 322 | 1 | $this->pregResponse($response[$j], $matches); |
|
| 323 | 1 | if (!empty($matches)) { |
|
| 324 | 1 | $result['after'][$matches[1][0]] = $matches[2][0]; |
|
| 325 | } |
||
| 326 | } |
||
| 327 | } |
||
| 328 | 1 | break 2; |
|
| 329 | default: |
||
| 330 | $this->pregResponse($value, $matches); |
||
| 331 | if (!empty($matches)) { |
||
| 332 | $result[$i][$matches[1][0]] = $matches[2][0]; |
||
| 333 | } |
||
| 334 | break; |
||
| 335 | } |
||
| 336 | } |
||
| 337 | 1 | return $result; |
|
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Parse result from RouterOS by regular expression |
||
| 342 | * |
||
| 343 | * @param string $value |
||
| 344 | * @param array $matches |
||
| 345 | */ |
||
| 346 | 1 | private function pregResponse(string $value, &$matches) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Authorization logic |
||
| 353 | * |
||
| 354 | * @return bool |
||
| 355 | * @throws \RouterOS\Exceptions\ClientException |
||
| 356 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 357 | * @throws \RouterOS\Exceptions\QueryException |
||
| 358 | */ |
||
| 359 | 4 | private function login(): bool |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Connect to socket server |
||
| 387 | * |
||
| 388 | * @return bool |
||
| 389 | * @throws \RouterOS\Exceptions\ClientException |
||
| 390 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 391 | * @throws \RouterOS\Exceptions\QueryException |
||
| 392 | */ |
||
| 393 | 5 | private function connect(): bool |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Save socket resource to static variable |
||
| 427 | * |
||
| 428 | * @param resource $socket |
||
| 429 | */ |
||
| 430 | 4 | private function setSocket($socket) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Return socket resource if is exist |
||
| 437 | * |
||
| 438 | * @return resource |
||
| 439 | */ |
||
| 440 | 4 | public function getSocket() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Initiate socket session |
||
| 447 | * |
||
| 448 | * @throws \RouterOS\Exceptions\ClientException |
||
| 449 | * @throws \RouterOS\Exceptions\ConfigException |
||
| 450 | */ |
||
| 451 | 5 | private function openSocket() |
|
| 452 | { |
||
| 453 | // Default: Context for ssl |
||
| 454 | 5 | $context = stream_context_create([ |
|
| 455 | 'ssl' => [ |
||
| 456 | 5 | 'ciphers' => 'ADH:ALL', |
|
| 457 | 'verify_peer' => false, |
||
| 458 | 'verify_peer_name' => false |
||
| 459 | ] |
||
| 460 | ]); |
||
| 461 | |||
| 462 | // Default: Proto tcp:// but for ssl we need ssl:// |
||
| 463 | 5 | $proto = $this->config('ssl') ? 'ssl://' : ''; |
|
| 464 | |||
| 465 | // Initiate socket client |
||
| 466 | 5 | $socket = @stream_socket_client( |
|
| 467 | 5 | $proto . $this->config('host') . ':' . $this->config('port'), |
|
| 468 | 5 | $this->_socket_err_num, |
|
| 469 | 5 | $this->_socket_err_str, |
|
| 470 | 5 | $this->config('timeout'), |
|
| 471 | 5 | STREAM_CLIENT_CONNECT, |
|
| 472 | $context |
||
| 473 | ); |
||
| 474 | |||
| 475 | // Throw error is socket is not initiated |
||
| 476 | 5 | if (!$socket) { |
|
| 477 | 1 | throw new ClientException('Unable to establish socket session, ' . $this->_socket_err_str); |
|
| 478 | } |
||
| 479 | |||
| 480 | // Save socket to static variable |
||
| 481 | 4 | return $this->setSocket($socket); |
|
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Close socket session |
||
| 486 | * |
||
| 487 | * @return bool |
||
| 488 | */ |
||
| 489 | private function closeSocket(): bool |
||
| 493 | } |
||
| 494 |