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 | /** |
||
19 | * Socket resource |
||
20 | * @var resource|null |
||
21 | */ |
||
22 | private $_socket; |
||
23 | |||
24 | /** |
||
25 | * Code of error |
||
26 | * @var int |
||
27 | */ |
||
28 | private $_socket_err_num; |
||
29 | |||
30 | /** |
||
31 | * Description of socket error |
||
32 | * @var string |
||
33 | */ |
||
34 | private $_socket_err_str; |
||
35 | |||
36 | /** |
||
37 | * Configuration of connection |
||
38 | * @var ConfigInterface |
||
39 | */ |
||
40 | private $_config; |
||
41 | |||
42 | /** |
||
43 | * Client constructor. |
||
44 | * |
||
45 | * @param ConfigInterface $config |
||
46 | * @throws ConfigException |
||
47 | * @throws ClientException |
||
48 | */ |
||
49 | 5 | public function __construct(ConfigInterface $config) |
|
50 | { |
||
51 | // Check for important keys |
||
52 | 5 | $this->exceptionIfKeyNotExist(['host', 'user', 'pass'], $config); |
|
53 | |||
54 | // Save config if everything is okay |
||
55 | 5 | $this->_config = $config; |
|
56 | |||
57 | // Throw error if cannot to connect |
||
58 | 5 | if (false === $this->connect()) { |
|
59 | throw new ClientException('Unable to connect to ' . $config->get('host') . ':' . $config->get('port')); |
||
60 | } |
||
61 | 4 | } |
|
62 | |||
63 | /** |
||
64 | * Check for important keys |
||
65 | * |
||
66 | * @param array $keys |
||
67 | * @param ConfigInterface $config |
||
68 | * @throws ConfigException |
||
69 | */ |
||
70 | 5 | private function exceptionIfKeyNotExist(array $keys, ConfigInterface $config) |
|
71 | { |
||
72 | 5 | $parameters = $config->getParameters(); |
|
73 | 5 | foreach ($keys as $key) { |
|
74 | 5 | if (!array_key_exists($key, $parameters) && isset($parameters[$key])) { |
|
75 | 5 | throw new ConfigException("Parameter '$key' of Config is not set or empty"); |
|
76 | } |
||
77 | } |
||
78 | 5 | } |
|
79 | |||
80 | /** |
||
81 | * Get some parameter from config |
||
82 | * |
||
83 | * @param string $parameter |
||
84 | * @return mixed |
||
85 | */ |
||
86 | 5 | private function config(string $parameter) |
|
90 | |||
91 | /** |
||
92 | * Encode given length in RouterOS format |
||
93 | * |
||
94 | * @param string $string |
||
95 | * @return string Encoded length |
||
96 | * @throws ClientException |
||
97 | */ |
||
98 | 4 | private function encodeLength(string $string): string |
|
135 | |||
136 | /** |
||
137 | * Read length of line |
||
138 | * |
||
139 | * @param int $byte |
||
140 | * @return int |
||
141 | */ |
||
142 | 4 | private function getLength(int $byte): int |
|
174 | |||
175 | /** |
||
176 | * Send write query to RouterOS (with or without tag) |
||
177 | * |
||
178 | * @param QueryInterface $query |
||
179 | * @return ClientInterface |
||
180 | * @throws ClientException |
||
181 | */ |
||
182 | 4 | public function write(QueryInterface $query): ClientInterface |
|
195 | |||
196 | /** |
||
197 | * Read answer from server after query was executed |
||
198 | * |
||
199 | * @param bool $parse |
||
200 | * @return array |
||
201 | */ |
||
202 | 4 | public function read(bool $parse = true): array |
|
238 | |||
239 | /** |
||
240 | * Parse response from Router OS |
||
241 | * |
||
242 | * @param array $response Response data |
||
243 | * @return array Array with parsed data |
||
244 | */ |
||
245 | 1 | private function parseResponse(array $response): array |
|
281 | |||
282 | /** |
||
283 | * Parse result from RouterOS by regular expression |
||
284 | * |
||
285 | * @param string $value |
||
286 | * @param array $matches |
||
287 | */ |
||
288 | 1 | private function pregResponse(string $value, &$matches) |
|
292 | |||
293 | /** |
||
294 | * Authorization logic |
||
295 | * |
||
296 | * @return bool |
||
297 | * @throws ClientException |
||
298 | */ |
||
299 | 4 | private function login(): bool |
|
325 | |||
326 | /** |
||
327 | * Connect to socket server |
||
328 | * |
||
329 | * @return bool |
||
330 | * @throws ClientException |
||
331 | */ |
||
332 | 5 | private function connect(): bool |
|
363 | |||
364 | /** |
||
365 | * Save socket resource to static variable |
||
366 | * |
||
367 | * @param resource $socket |
||
368 | */ |
||
369 | 4 | private function setSocket($socket) |
|
373 | |||
374 | /** |
||
375 | * Return socket resource if is exist |
||
376 | * |
||
377 | * @return resource |
||
378 | */ |
||
379 | 4 | public function getSocket() |
|
383 | |||
384 | /** |
||
385 | * Initiate socket session |
||
386 | * |
||
387 | * @throws ClientException |
||
388 | */ |
||
389 | 5 | private function openSocket() |
|
421 | |||
422 | /** |
||
423 | * Close socket session |
||
424 | * |
||
425 | * @return bool |
||
426 | */ |
||
427 | private function closeSocket(): bool |
||
431 | } |
||
432 |