Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
17 | class Client implements Interfaces\ClientInterface |
||
18 | { |
||
19 | /** |
||
20 | * Socket resource |
||
21 | * @var resource|null |
||
22 | */ |
||
23 | private $_socket; |
||
24 | |||
25 | /** |
||
26 | * Code of error |
||
27 | * @var int |
||
28 | */ |
||
29 | private $_socket_err_num; |
||
30 | |||
31 | /** |
||
32 | * Description of socket error |
||
33 | * @var string |
||
34 | */ |
||
35 | private $_socket_err_str; |
||
36 | |||
37 | /** |
||
38 | * Configuration of connection |
||
39 | * @var ConfigInterface |
||
40 | */ |
||
41 | private $_config; |
||
42 | |||
43 | /** |
||
44 | * Client constructor. |
||
45 | * |
||
46 | * @param ConfigInterface $config |
||
47 | * @throws ConfigException |
||
48 | * @throws ClientException |
||
49 | */ |
||
50 | public function __construct(ConfigInterface $config) |
||
63 | |||
64 | /** |
||
65 | * Check for important keys |
||
66 | * |
||
67 | * @param array $keys |
||
68 | * @param ConfigInterface $config |
||
69 | * @throws ConfigException |
||
70 | */ |
||
71 | private function keysCheck(array $keys, ConfigInterface $config) |
||
80 | |||
81 | /** |
||
82 | * Get some parameter from config |
||
83 | * |
||
84 | * @param string $parameter |
||
85 | * @return mixed |
||
86 | */ |
||
87 | private function config(string $parameter) |
||
91 | |||
92 | /** |
||
93 | * Convert ordinary string to hex string |
||
94 | * |
||
95 | * @param string $string |
||
96 | * @return string |
||
97 | */ |
||
98 | private function encodeLength(string $string): string |
||
125 | |||
126 | /** |
||
127 | * Read length of line |
||
128 | * |
||
129 | * @param int $byte |
||
130 | * @return int |
||
131 | */ |
||
132 | private function getLength(int $byte): int |
||
165 | |||
166 | /** |
||
167 | * Send write query to RouterOS (with or without tag) |
||
168 | * |
||
169 | * @param QueryInterface $query |
||
170 | * @return ClientInterface |
||
171 | */ |
||
172 | public function write(QueryInterface $query): ClientInterface |
||
185 | |||
186 | // public function read2(bool $parse = true): array |
||
187 | // { |
||
188 | // while (true) { |
||
189 | // |
||
190 | // $res = ''; |
||
191 | // while ($buf = fread($this->_socket, 1)) { |
||
192 | // if (substr($res, -5) === '!done') { |
||
193 | // echo 'done'; |
||
194 | // break 2; |
||
195 | // } |
||
196 | // echo "$buf\n"; |
||
197 | // $res .= $buf; |
||
198 | // } |
||
199 | // $result[] = $res; |
||
200 | // } |
||
201 | // print_r($result); |
||
202 | // die(); |
||
203 | // } |
||
204 | |||
205 | /** |
||
206 | * Read answer from server after query was executed |
||
207 | * |
||
208 | * @param bool $parse |
||
209 | * @return array |
||
210 | */ |
||
211 | public function read(bool $parse = true): array |
||
243 | |||
244 | /** |
||
245 | * Parse response from Router OS |
||
246 | * |
||
247 | * @param array $response Response data |
||
248 | * @return array Array with parsed data |
||
249 | */ |
||
250 | private function parseResponse(array $response): array |
||
272 | |||
273 | /** |
||
274 | * Authorization logic |
||
275 | * |
||
276 | * @return bool |
||
277 | */ |
||
278 | private function login(): bool |
||
303 | |||
304 | /** |
||
305 | * Connect to socket server |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | public function connect(): bool |
||
340 | |||
341 | /** |
||
342 | * Save socket resource to static variable |
||
343 | * |
||
344 | * @param resource|null $socket |
||
345 | * @return bool |
||
346 | */ |
||
347 | private function setSocket($socket): bool |
||
355 | |||
356 | /** |
||
357 | * Return socket resource if is exist |
||
358 | * |
||
359 | * @return bool|resource |
||
360 | */ |
||
361 | public function getSocket() |
||
367 | |||
368 | /** |
||
369 | * Initiate socket session |
||
370 | * |
||
371 | * @return bool |
||
372 | * @throws ClientException |
||
373 | */ |
||
374 | private function openSocket(): bool |
||
409 | |||
410 | /** |
||
411 | * Close socket session |
||
412 | * |
||
413 | * @return bool |
||
414 | */ |
||
415 | private function closeSocket(): bool |
||
419 | } |
||
420 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.