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 |
||
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 | 4 | public function __construct(ConfigInterface $config) |
|
50 | { |
||
51 | // Check for important keys |
||
52 | 4 | $this->keysCheck(['host', 'user', 'pass'], $config); |
|
53 | |||
54 | // Save config if everything is okay |
||
55 | 4 | $this->_config = $config; |
|
56 | |||
57 | // Throw error if cannot to connect |
||
58 | 4 | if (false === $this->connect()) { |
|
59 | 3 | throw new ClientException('Unable to connect to ' . $config->get('host') . ':' . $config->get('port')); |
|
60 | } |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Check for important keys |
||
65 | * |
||
66 | * @param array $keys |
||
67 | * @param ConfigInterface $config |
||
68 | * @throws ConfigException |
||
69 | */ |
||
70 | 4 | private function keysCheck(array $keys, ConfigInterface $config) |
|
79 | |||
80 | /** |
||
81 | * Get some parameter from config |
||
82 | * |
||
83 | * @param string $parameter |
||
84 | * @return mixed |
||
85 | */ |
||
86 | 4 | private function config(string $parameter) |
|
90 | |||
91 | /** |
||
92 | * Convert ordinary string to hex string |
||
93 | * |
||
94 | * @param string $string |
||
95 | * @return string |
||
96 | */ |
||
97 | 3 | private function encodeLength(string $string): string |
|
124 | |||
125 | /** |
||
126 | * Read length of line |
||
127 | * |
||
128 | * @param int $byte |
||
129 | * @return int |
||
130 | */ |
||
131 | 3 | private function getLength(int $byte): int |
|
163 | |||
164 | /** |
||
165 | * Send write query to RouterOS (with or without tag) |
||
166 | * |
||
167 | * @param QueryInterface $query |
||
168 | * @return ClientInterface |
||
169 | */ |
||
170 | 3 | public function write(QueryInterface $query): ClientInterface |
|
183 | |||
184 | /** |
||
185 | * Read answer from server after query was executed |
||
186 | * |
||
187 | * @param bool $parse |
||
188 | * @return array |
||
189 | */ |
||
190 | 3 | public function read(bool $parse = true): array |
|
228 | |||
229 | /** |
||
230 | * Parse response from Router OS |
||
231 | * |
||
232 | * @param array $response Response data |
||
233 | * @return array Array with parsed data |
||
234 | */ |
||
235 | 1 | private function parseResponse(array $response): array |
|
269 | |||
270 | 1 | private function pregResponse(string $value, &$matches) |
|
274 | |||
275 | /** |
||
276 | * Authorization logic |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | 3 | private function login(): bool |
|
305 | |||
306 | /** |
||
307 | * Connect to socket server |
||
308 | * |
||
309 | * @return bool |
||
310 | * @throws ClientException |
||
311 | */ |
||
312 | 4 | private function connect(): bool |
|
343 | |||
344 | /** |
||
345 | * Save socket resource to static variable |
||
346 | * |
||
347 | * @param resource $socket |
||
348 | */ |
||
349 | 3 | private function setSocket($socket) |
|
353 | |||
354 | /** |
||
355 | * Return socket resource if is exist |
||
356 | * |
||
357 | * @return resource |
||
358 | */ |
||
359 | 3 | public function getSocket() |
|
363 | |||
364 | /** |
||
365 | * Initiate socket session |
||
366 | * |
||
367 | * @throws ClientException |
||
368 | */ |
||
369 | 4 | private function openSocket() |
|
401 | |||
402 | /** |
||
403 | * Close socket session |
||
404 | * |
||
405 | * @return bool |
||
406 | */ |
||
407 | 3 | private function closeSocket(): bool |
|
411 | } |
||
412 |
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.