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 | 14 | public function __construct($config) |
|
57 | { |
||
58 | // If array then need create object |
||
59 | 14 | if (\is_array($config)) { |
|
60 | 3 | $config = new Config($config); |
|
61 | } |
||
62 | |||
63 | // Check for important keys |
||
64 | 14 | if (true !== $key = ArrayHelper::checkIfKeysNotExist(['host', 'user', 'pass'], $config->getParameters())) { |
|
65 | 1 | throw new ConfigException("One or few parameters '$key' of Config is not set or empty"); |
|
66 | } |
||
67 | |||
68 | // Save config if everything is okay |
||
69 | 13 | $this->setConfig($config); |
|
70 | |||
71 | // Throw error if cannot to connect |
||
72 | 13 | if (false === $this->connect()) { |
|
73 | 1 | throw new ClientException('Unable to connect to ' . $config->get('host') . ':' . $config->get('port')); |
|
74 | } |
||
75 | 11 | } |
|
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 | 13 | 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 | 1 | public function getConfig(): Config |
|
96 | { |
||
97 | 1 | return $this->_config; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Set configuration of client |
||
102 | * |
||
103 | * @param \RouterOS\Config $config |
||
104 | * @since 0.7 |
||
105 | */ |
||
106 | 13 | 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 | 12 | private function encodeLength(string $string): string |
|
155 | |||
156 | /** |
||
157 | * Read length of line |
||
158 | * |
||
159 | * @param int $byte |
||
160 | * @return int |
||
161 | */ |
||
162 | 12 | 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 | 12 | public function write($query): Client |
|
223 | |||
224 | /** |
||
225 | * Read answer from server after query was executed |
||
226 | * |
||
227 | * A Mikrotik reply is formed of blocks |
||
228 | * Each block starts with a word, one of ('!re', '!trap', '!done', '!fatal') |
||
229 | * Each block end with an zero byte (empty line) |
||
230 | * Reply ends with a complete !done or !fatal block (ended with 'empty line') |
||
231 | * A !fatal block precedes TCP connexion close |
||
232 | * |
||
233 | * @param bool $parse |
||
234 | * @return array |
||
235 | */ |
||
236 | 12 | public function read(bool $parse = true): array |
|
275 | |||
276 | /** |
||
277 | * Alias for ->write() method |
||
278 | * |
||
279 | * @param string|array|\RouterOS\Query $query |
||
280 | * @return \RouterOS\Client |
||
281 | * @throws \RouterOS\Exceptions\ClientException |
||
282 | * @throws \RouterOS\Exceptions\QueryException |
||
283 | */ |
||
284 | 1 | public function w($query): Client |
|
288 | |||
289 | /** |
||
290 | * Alias for ->read() method |
||
291 | * |
||
292 | * @param bool $parse |
||
293 | * @return array |
||
294 | * @since 0.7 |
||
295 | */ |
||
296 | 1 | public function r(bool $parse = true): array |
|
300 | |||
301 | /** |
||
302 | * Alias for ->write()->read() combination of methods |
||
303 | * |
||
304 | * @param string|array|\RouterOS\Query $query |
||
305 | * @param bool $parse |
||
306 | * @return array |
||
307 | * @throws \RouterOS\Exceptions\ClientException |
||
308 | * @throws \RouterOS\Exceptions\QueryException |
||
309 | * @since 0.6 |
||
310 | */ |
||
311 | 4 | public function wr($query, bool $parse = true): array |
|
315 | |||
316 | /** |
||
317 | * Parse response from Router OS |
||
318 | * |
||
319 | * @param array $response Response data |
||
320 | * @return array Array with parsed data |
||
321 | */ |
||
322 | 4 | private function parseResponse(array $response): array |
|
358 | |||
359 | /** |
||
360 | * Parse result from RouterOS by regular expression |
||
361 | * |
||
362 | * @param string $value |
||
363 | * @param array $matches |
||
364 | */ |
||
365 | 3 | private function pregResponse(string $value, &$matches) |
|
369 | |||
370 | /** |
||
371 | * Authorization logic |
||
372 | * |
||
373 | * @param bool $legacyRetry Retry login if we detect legacy version of RouterOS |
||
374 | * @return bool |
||
375 | * @throws \RouterOS\Exceptions\ClientException |
||
376 | * @throws \RouterOS\Exceptions\ConfigException |
||
377 | * @throws \RouterOS\Exceptions\QueryException |
||
378 | */ |
||
379 | 12 | private function login(bool $legacyRetry = false): bool |
|
418 | |||
419 | /** |
||
420 | * Detect by login request if firmware is legacy |
||
421 | * |
||
422 | * @param array $response |
||
423 | * @return bool |
||
424 | * @throws ConfigException |
||
425 | */ |
||
426 | 11 | private function isLegacy(array &$response): bool |
|
430 | |||
431 | /** |
||
432 | * Connect to socket server |
||
433 | * |
||
434 | * @return bool |
||
435 | * @throws \RouterOS\Exceptions\ClientException |
||
436 | * @throws \RouterOS\Exceptions\ConfigException |
||
437 | * @throws \RouterOS\Exceptions\QueryException |
||
438 | */ |
||
439 | 13 | private function connect(): bool |
|
470 | |||
471 | } |
||
472 |