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 |
||
29 | class Client implements Interfaces\ClientInterface |
||
30 | { |
||
31 | use SocketTrait, ShortsTrait; |
||
32 | |||
33 | /** |
||
34 | * Configuration of connection |
||
35 | * |
||
36 | * @var \RouterOS\Config |
||
37 | */ |
||
38 | private $_config; |
||
39 | |||
40 | /** |
||
41 | * API communication object |
||
42 | * |
||
43 | * @var \RouterOS\APIConnector |
||
44 | */ |
||
45 | |||
46 | private $_connector; |
||
47 | |||
48 | /** |
||
49 | * Client constructor. |
||
50 | * |
||
51 | * @param array|\RouterOS\Interfaces\ConfigInterface $config |
||
52 | * |
||
53 | * @throws \RouterOS\Exceptions\ClientException |
||
54 | * @throws \RouterOS\Exceptions\ConfigException |
||
55 | * @throws \RouterOS\Exceptions\QueryException |
||
56 | */ |
||
57 | 17 | public function __construct($config) |
|
58 | { |
||
59 | // If array then need create object |
||
60 | 17 | if (is_array($config)) { |
|
61 | 14 | $config = new Config($config); |
|
62 | } |
||
63 | |||
64 | // Check for important keys |
||
65 | 17 | if (true !== $key = ArrayHelper::checkIfKeysNotExist(['host', 'user', 'pass'], $config->getParameters())) { |
|
66 | 1 | throw new ConfigException("One or few parameters '$key' of Config is not set or empty"); |
|
67 | } |
||
68 | |||
69 | // Save config if everything is okay |
||
70 | 16 | $this->_config = $config; |
|
71 | |||
72 | // Throw error if cannot to connect |
||
73 | 16 | if (false === $this->connect()) { |
|
74 | throw new ClientException('Unable to connect to ' . $config->get('host') . ':' . $config->get('port')); |
||
75 | } |
||
76 | 14 | } |
|
77 | |||
78 | /** |
||
79 | * Get some parameter from config |
||
80 | * |
||
81 | * @param string $parameter Name of required parameter |
||
82 | * |
||
83 | * @return mixed |
||
84 | * @throws \RouterOS\Exceptions\ConfigException |
||
85 | */ |
||
86 | 16 | private function config(string $parameter) |
|
90 | |||
91 | /** |
||
92 | * Send write query to RouterOS |
||
93 | * |
||
94 | * @param string|array|\RouterOS\Query $query |
||
95 | * |
||
96 | * @return \RouterOS\Client |
||
97 | * @throws \RouterOS\Exceptions\QueryException |
||
98 | * @deprecated |
||
99 | */ |
||
100 | 5 | public function write($query): Client |
|
116 | |||
117 | /** |
||
118 | * Send write query to RouterOS (modern version of write) |
||
119 | * |
||
120 | * @param string|\RouterOS\Query $endpoint Path of API query or Query object |
||
121 | * @param array|null $where List of where filters |
||
122 | * @param string|null $operations Some operations which need make on response |
||
123 | * @param string|null $tag Mark query with tag |
||
124 | * |
||
125 | * @return \RouterOS\Client |
||
126 | * @throws \RouterOS\Exceptions\QueryException |
||
127 | * @throws \RouterOS\Exceptions\ClientException |
||
128 | * @since 1.0.0 |
||
129 | */ |
||
130 | 15 | public function query($endpoint, array $where = null, string $operations = null, string $tag = null): Client |
|
164 | |||
165 | /** |
||
166 | * Query helper |
||
167 | * |
||
168 | * @param array $item |
||
169 | * @param \RouterOS\Interfaces\QueryInterface $query |
||
170 | * |
||
171 | * @return \RouterOS\Query |
||
172 | * @throws \RouterOS\Exceptions\ClientException |
||
173 | * @throws \RouterOS\Exceptions\QueryException |
||
174 | */ |
||
175 | 3 | private function preQuery(array $item, Query $query): Query |
|
198 | |||
199 | /** |
||
200 | * Send write query object to RouterOS |
||
201 | * |
||
202 | * @param \RouterOS\Query $query |
||
203 | * |
||
204 | * @return \RouterOS\Client |
||
205 | * @throws \RouterOS\Exceptions\QueryException |
||
206 | * @since 1.0.0 |
||
207 | */ |
||
208 | 15 | private function writeRAW(Query $query): Client |
|
220 | |||
221 | /** |
||
222 | * Read RAW response from RouterOS |
||
223 | * |
||
224 | * @return array |
||
225 | * @since 1.0.0 |
||
226 | */ |
||
227 | 15 | private function readRAW(): array |
|
263 | |||
264 | /** |
||
265 | * Read answer from server after query was executed |
||
266 | * |
||
267 | * A Mikrotik reply is formed of blocks |
||
268 | * Each block starts with a word, one of ('!re', '!trap', '!done', '!fatal') |
||
269 | * Each block end with an zero byte (empty line) |
||
270 | * Reply ends with a complete !done or !fatal block (ended with 'empty line') |
||
271 | * A !fatal block precedes TCP connexion close |
||
272 | * |
||
273 | * @param bool $parse |
||
274 | * |
||
275 | * @return mixed |
||
276 | */ |
||
277 | 15 | public function read(bool $parse = true) |
|
285 | |||
286 | /** |
||
287 | * Read using Iterators to improve performance on large dataset |
||
288 | * |
||
289 | * @return \RouterOS\ResponseIterator |
||
290 | * @since 1.0.0 |
||
291 | */ |
||
292 | 4 | public function readAsIterator(): ResponseIterator |
|
296 | |||
297 | /** |
||
298 | * This method was created by memory save reasons, it convert response |
||
299 | * from RouterOS to readable array in safe way. |
||
300 | * |
||
301 | * @param array $raw Array RAW response from server |
||
302 | * |
||
303 | * @return mixed |
||
304 | * |
||
305 | * Based on RouterOSResponseArray solution by @arily |
||
306 | * |
||
307 | * @link https://github.com/arily/RouterOSResponseArray |
||
308 | * @since 1.0.0 |
||
309 | */ |
||
310 | 4 | private function rosario(array $raw): array |
|
341 | |||
342 | /** |
||
343 | * Parse response from Router OS |
||
344 | * |
||
345 | * @param array $response Response data |
||
346 | * |
||
347 | * @return array Array with parsed data |
||
348 | */ |
||
349 | 5 | public function parseResponse(array $response): array |
|
379 | |||
380 | /** |
||
381 | * Response helper |
||
382 | * |
||
383 | * @param string $value Value which should be parsed |
||
384 | * @param array $result Array with parsed response |
||
385 | * @param null|array $matches Matched words |
||
386 | * @param string|int $iterator Type of iterations or number of item |
||
387 | */ |
||
388 | 4 | private function preParseResponse(string $value, array &$result, ?array &$matches, $iterator = 'after'): void |
|
395 | |||
396 | /** |
||
397 | * Parse result from RouterOS by regular expression |
||
398 | * |
||
399 | * @param string $value |
||
400 | * @param null|array $matches |
||
401 | */ |
||
402 | 4 | private function pregResponse(string $value, ?array &$matches): void |
|
406 | |||
407 | /** |
||
408 | * Authorization logic |
||
409 | * |
||
410 | * @param bool $legacyRetry Retry login if we detect legacy version of RouterOS |
||
411 | * |
||
412 | * @return bool |
||
413 | * @throws \RouterOS\Exceptions\ClientException |
||
414 | * @throws \RouterOS\Exceptions\ConfigException |
||
415 | * @throws \RouterOS\Exceptions\QueryException |
||
416 | */ |
||
417 | 15 | private function login(bool $legacyRetry = false): bool |
|
462 | |||
463 | /** |
||
464 | * Detect by login request if firmware is legacy |
||
465 | * |
||
466 | * @param array $response |
||
467 | * |
||
468 | * @return bool |
||
469 | * @throws \RouterOS\Exceptions\ConfigException |
||
470 | */ |
||
471 | 14 | private function isLegacy(array &$response): bool |
|
475 | |||
476 | /** |
||
477 | * Connect to socket server |
||
478 | * |
||
479 | * @return bool |
||
480 | * @throws \RouterOS\Exceptions\ClientException |
||
481 | * @throws \RouterOS\Exceptions\ConfigException |
||
482 | * @throws \RouterOS\Exceptions\QueryException |
||
483 | */ |
||
484 | 16 | private function connect(): bool |
|
515 | } |
||
516 |