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 | private $connector; |
||
45 | |||
46 | /** |
||
47 | * Some strings with custom output |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | private $customOutput; |
||
52 | |||
53 | /** |
||
54 | * Client constructor. |
||
55 | * |
||
56 | * @param array|\RouterOS\Interfaces\ConfigInterface $config Array with configuration or Config object |
||
57 | * @param bool $autoConnect If false it will skip auto-connect stage if not need to instantiate connection |
||
58 | * |
||
59 | * @throws \RouterOS\Exceptions\ClientException |
||
60 | * @throws \RouterOS\Exceptions\ConfigException |
||
61 | * @throws \RouterOS\Exceptions\QueryException |
||
62 | */ |
||
63 | 27 | public function __construct($config, bool $autoConnect = true) |
|
64 | { |
||
65 | // If array then need create object |
||
66 | 27 | if (is_array($config)) { |
|
67 | 26 | $config = new Config($config); |
|
68 | } |
||
69 | |||
70 | // Check for important keys |
||
71 | 27 | if (true !== $key = ArrayHelper::checkIfKeysNotExist(['host', 'user', 'pass'], $config->getParameters())) { |
|
72 | 1 | throw new ConfigException("One or few parameters '$key' of Config is not set or empty"); |
|
73 | } |
||
74 | |||
75 | // Save config if everything is okay |
||
76 | 27 | $this->config = $config; |
|
77 | |||
78 | // Skip next step if not need to instantiate connection |
||
79 | 27 | if (false === $autoConnect) { |
|
80 | 1 | return; |
|
81 | } |
||
82 | |||
83 | // Throw error if cannot to connect |
||
84 | 26 | if (false === $this->connect()) { |
|
85 | 1 | throw new ClientException('Unable to connect to ' . $config->get('host') . ':' . $config->get('port')); |
|
86 | } |
||
87 | 26 | } |
|
88 | |||
89 | /** |
||
90 | * Get some parameter from config |
||
91 | * |
||
92 | * @param string $parameter Name of required parameter |
||
93 | * |
||
94 | * @return mixed |
||
95 | * @throws \RouterOS\Exceptions\ConfigException |
||
96 | */ |
||
97 | 26 | private function config(string $parameter) |
|
101 | |||
102 | /** |
||
103 | * Send write query to RouterOS (modern version of write) |
||
104 | * |
||
105 | * @param array|string|\RouterOS\Interfaces\QueryInterface $endpoint Path of API query or Query object |
||
106 | * @param array|null $where List of where filters |
||
107 | * @param string|null $operations Some operations which need make on response |
||
108 | * @param string|null $tag Mark query with tag |
||
109 | * |
||
110 | * @return \RouterOS\Interfaces\ClientInterface |
||
111 | * @throws \RouterOS\Exceptions\QueryException |
||
112 | * @throws \RouterOS\Exceptions\ClientException |
||
113 | * @throws \RouterOS\Exceptions\ConfigException |
||
114 | * @since 1.0.0 |
||
115 | */ |
||
116 | 26 | public function query($endpoint, array $where = null, string $operations = null, string $tag = null): ClientInterface |
|
150 | |||
151 | /** |
||
152 | * Query helper |
||
153 | * |
||
154 | * @param array $item |
||
155 | * @param \RouterOS\Interfaces\QueryInterface $query |
||
156 | * |
||
157 | * @return \RouterOS\Query |
||
158 | * @throws \RouterOS\Exceptions\QueryException |
||
159 | * @throws \RouterOS\Exceptions\ClientException |
||
160 | */ |
||
161 | 6 | private function preQuery(array $item, QueryInterface $query): QueryInterface |
|
184 | |||
185 | /** |
||
186 | * Send write query object to RouterOS |
||
187 | * |
||
188 | * @param \RouterOS\Interfaces\QueryInterface $query |
||
189 | * |
||
190 | * @return \RouterOS\Interfaces\ClientInterface |
||
191 | * @throws \RouterOS\Exceptions\QueryException |
||
192 | * @throws \RouterOS\Exceptions\ConfigException |
||
193 | * @since 1.0.0 |
||
194 | */ |
||
195 | 26 | private function writeRAW(QueryInterface $query): ClientInterface |
|
225 | |||
226 | /** |
||
227 | * Read RAW response from RouterOS, it can be /export command results also, not only array from API |
||
228 | * |
||
229 | * @return array|string |
||
230 | * @since 1.0.0 |
||
231 | */ |
||
232 | 26 | public function readRAW() |
|
274 | |||
275 | /** |
||
276 | * Read answer from server after query was executed |
||
277 | * |
||
278 | * A Mikrotik reply is formed of blocks |
||
279 | * Each block starts with a word, one of ('!re', '!trap', '!done', '!fatal') |
||
280 | * Each block end with an zero byte (empty line) |
||
281 | * Reply ends with a complete !done or !fatal block (ended with 'empty line') |
||
282 | * A !fatal block precedes TCP connexion close |
||
283 | * |
||
284 | * @param bool $parse If need parse output to array |
||
285 | * |
||
286 | * @return mixed |
||
287 | */ |
||
288 | 26 | public function read(bool $parse = true) |
|
302 | |||
303 | /** |
||
304 | * Read using Iterators to improve performance on large dataset |
||
305 | * |
||
306 | * @return \RouterOS\ResponseIterator |
||
307 | * @since 1.0.0 |
||
308 | */ |
||
309 | 3 | public function readAsIterator(): ResponseIterator |
|
313 | |||
314 | /** |
||
315 | * This method was created by memory save reasons, it convert response |
||
316 | * from RouterOS to readable array in safe way. |
||
317 | * |
||
318 | * @param array $raw Array RAW response from server |
||
319 | * |
||
320 | * @return mixed |
||
321 | * |
||
322 | * Based on RouterOSResponseArray solution by @arily |
||
323 | * |
||
324 | * @see https://github.com/arily/RouterOSResponseArray |
||
325 | * @since 1.0.0 |
||
326 | */ |
||
327 | 4 | private function rosario(array $raw): array |
|
328 | { |
||
329 | // This RAW should't be an error |
||
330 | 4 | $positions = array_keys($raw, '!re'); |
|
331 | 4 | $count = count($raw); |
|
332 | 4 | $result = []; |
|
333 | |||
334 | 4 | if (isset($positions[1])) { |
|
335 | |||
336 | 1 | foreach ($positions as $key => $position) { |
|
337 | // Get length of future block |
||
338 | 1 | $length = isset($positions[$key + 1]) |
|
339 | 1 | ? $positions[$key + 1] - $position + 1 |
|
340 | 1 | : $count - $position; |
|
341 | |||
342 | // Convert array to simple items |
||
343 | 1 | $item = []; |
|
344 | 1 | for ($i = 1; $i < $length; $i++) { |
|
345 | 1 | $item[] = array_shift($raw); |
|
346 | } |
||
347 | |||
348 | // Save as result |
||
349 | 1 | $result[] = $this->parseResponse($item)[0]; |
|
350 | } |
||
351 | |||
352 | } else { |
||
353 | 4 | $result = $this->parseResponse($raw); |
|
354 | } |
||
355 | |||
356 | 4 | return $result; |
|
357 | } |
||
358 | |||
359 | /** |
||
360 | * Parse response from Router OS |
||
361 | * |
||
362 | * @param array $response Response data |
||
363 | * |
||
364 | * @return array Array with parsed data |
||
365 | */ |
||
366 | 5 | public function parseResponse(array $response): array |
|
396 | |||
397 | /** |
||
398 | * Response helper |
||
399 | * |
||
400 | * @param string $value Value which should be parsed |
||
401 | * @param array $result Array with parsed response |
||
402 | * @param array|null $matches Matched words |
||
403 | * @param string|int $iterator Type of iterations or number of item |
||
404 | */ |
||
405 | 4 | private function preParseResponse(string $value, array &$result, ?array &$matches, $iterator = 'after'): void |
|
412 | |||
413 | /** |
||
414 | * Parse result from RouterOS by regular expression |
||
415 | * |
||
416 | * @param string $value |
||
417 | * @param array|null $matches |
||
418 | */ |
||
419 | 9 | protected function pregResponse(string $value, ?array &$matches): void |
|
423 | |||
424 | /** |
||
425 | * Authorization logic |
||
426 | * |
||
427 | * @param bool $legacyRetry Retry login if we detect legacy version of RouterOS |
||
428 | * |
||
429 | * @return bool |
||
430 | * @throws \RouterOS\Exceptions\ClientException |
||
431 | * @throws \RouterOS\Exceptions\ConfigException |
||
432 | * @throws \RouterOS\Exceptions\QueryException |
||
433 | */ |
||
434 | 26 | private function login(bool $legacyRetry = false): bool |
|
479 | |||
480 | /** |
||
481 | * Detect by login request if firmware is legacy |
||
482 | * |
||
483 | * @param array $response |
||
484 | * |
||
485 | * @return bool |
||
486 | * @throws \RouterOS\Exceptions\ConfigException |
||
487 | */ |
||
488 | 26 | private function isLegacy(array $response): bool |
|
492 | |||
493 | /** |
||
494 | * Connect to socket server |
||
495 | * |
||
496 | * @return bool |
||
497 | * @throws \RouterOS\Exceptions\ClientException |
||
498 | * @throws \RouterOS\Exceptions\ConfigException |
||
499 | * @throws \RouterOS\Exceptions\QueryException |
||
500 | */ |
||
501 | 26 | public function connect(): bool |
|
532 | |||
533 | /** |
||
534 | * Check if custom output is not empty |
||
535 | * |
||
536 | * @return bool |
||
537 | */ |
||
538 | 26 | private function isCustomOutput(): bool |
|
542 | |||
543 | /** |
||
544 | * Execute export command on remote host, it also will be used |
||
545 | * if "/export" command passed to query. |
||
546 | * |
||
547 | * @param string|null $arguments String with arguments which should be passed to export command |
||
548 | * |
||
549 | * @return string |
||
550 | * @throws \RouterOS\Exceptions\ConfigException |
||
551 | * @since 1.3.0 |
||
552 | */ |
||
553 | public function export(string $arguments = null): string |
||
571 | } |
||
572 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.