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