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 |
||
15 | class Client implements Interfaces\ClientInterface |
||
16 | { |
||
17 | /** |
||
18 | * Socket resource |
||
19 | * @var resource|null |
||
20 | */ |
||
21 | private $_socket; |
||
22 | |||
23 | /** |
||
24 | * Code of error |
||
25 | * @var int |
||
26 | */ |
||
27 | private $_socket_err_num; |
||
28 | |||
29 | /** |
||
30 | * Description of socket error |
||
31 | * @var string |
||
32 | */ |
||
33 | private $_socket_err_str; |
||
34 | |||
35 | /** |
||
36 | * Configuration of connection |
||
37 | * @var Config |
||
38 | */ |
||
39 | private $_config; |
||
40 | |||
41 | /** |
||
42 | * Client constructor. |
||
43 | * @param ConfigInterface $config |
||
44 | */ |
||
45 | public function __construct(ConfigInterface $config) |
||
50 | |||
51 | /** |
||
52 | * Get some parameter from config |
||
53 | * |
||
54 | * @param string $parameter |
||
55 | * @return mixed |
||
56 | */ |
||
57 | private function config(string $parameter) |
||
61 | |||
62 | /** |
||
63 | * Convert ordinary string to hex string |
||
64 | * |
||
65 | * @param string $string |
||
66 | * @return string |
||
67 | */ |
||
68 | private function encodeLength(string $string): string |
||
95 | |||
96 | /** |
||
97 | * Send write query to RouterOS (with or without tag) |
||
98 | * |
||
99 | * @param QueryInterface $query |
||
100 | * @return ClientInterface |
||
101 | */ |
||
102 | public function write(QueryInterface $query): ClientInterface |
||
115 | |||
116 | /** |
||
117 | * Read answer from server after query was executed |
||
118 | * |
||
119 | * @param bool $parse |
||
120 | * @return array |
||
121 | */ |
||
122 | public function read(bool $parse = true): array |
||
151 | |||
152 | /** |
||
153 | * Parse response from Router OS |
||
154 | * |
||
155 | * @param array $response Response data |
||
156 | * @return array Array with parsed data |
||
157 | */ |
||
158 | private function parseResponse(array $response): array |
||
186 | |||
187 | /** |
||
188 | * Authorization logic |
||
189 | * |
||
190 | * @return bool |
||
191 | */ |
||
192 | private function login(): bool |
||
217 | |||
218 | /** |
||
219 | * Connect to socket server |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | public function connect(): bool |
||
250 | |||
251 | /** |
||
252 | * Save socket resource to static variable |
||
253 | * |
||
254 | * @param resource|null $socket |
||
255 | * @return bool |
||
256 | */ |
||
257 | private function setSocket($socket): bool |
||
265 | |||
266 | /** |
||
267 | * Return socket resource if is exist |
||
268 | * |
||
269 | * @return bool|resource |
||
270 | */ |
||
271 | public function getSocket() |
||
277 | |||
278 | /** |
||
279 | * Initiate socket session |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | private function openSocket(): bool |
||
322 | |||
323 | /** |
||
324 | * Close socket session |
||
325 | * |
||
326 | * @return bool |
||
327 | */ |
||
328 | private function closeSocket(): bool |
||
333 | } |
||
334 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.