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 |
|
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() |
|
483 | |||
484 | /** |
||
485 | * Close socket session |
||
486 | * |
||
487 | * @return bool |
||
488 | */ |
||
489 | private function closeSocket(): bool |
||
493 | } |
||
494 |