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 Connection 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 Connection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Connection extends ClientConnection |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * @TODO DESCR |
||
|
|||
20 | */ |
||
21 | const STATE_PACKET = 1; |
||
22 | |||
23 | /** |
||
24 | * @var integer Sequence |
||
25 | */ |
||
26 | protected $seq = 0; |
||
27 | |||
28 | /** |
||
29 | * @var boolean Keepalive? |
||
30 | */ |
||
31 | protected $keepalive = false; |
||
32 | |||
33 | /** |
||
34 | * @var array Response |
||
35 | */ |
||
36 | public $response = []; |
||
37 | |||
38 | /** |
||
39 | * @var boolean Current packet size |
||
40 | */ |
||
41 | protected $pctSize = 0; |
||
42 | |||
43 | /** |
||
44 | * @var integer Default low mark. Minimum number of bytes in buffer. |
||
45 | */ |
||
46 | protected $lowMark = 2; |
||
47 | |||
48 | /** |
||
49 | * @var integer Default high mark. Maximum number of bytes in buffer. |
||
50 | */ |
||
51 | protected $highMark = 512; |
||
52 | |||
53 | private $rcodeMessages = [ |
||
54 | 0 => 'Success', |
||
55 | 1 => 'Format Error', |
||
56 | 2 => 'Server Failure', |
||
57 | 3 => 'Non-Existent Domain', |
||
58 | 4 => 'Not Implemented', |
||
59 | 5 => 'Query Refused', |
||
60 | 6 => 'Name Exists when it should not', |
||
61 | 7 => 'RR Set Exists when it should not', |
||
62 | 8 => 'RR Set that should exist does not', |
||
63 | 9 => 'Not Authorized', |
||
64 | 10 => 'Name not contained in zone', |
||
65 | 16 => 'TSIG Signature Failure', |
||
66 | 17 => 'Key not recognized', |
||
67 | 18 => 'Signature out of time window', |
||
68 | 19 => 'Bad TKEY Mode', |
||
69 | 20 => 'Duplicate key name', |
||
70 | 21 => 'Algorithm not supported', |
||
71 | 22 => 'Bad Truncation', |
||
72 | 23 => 'Bad/missing server cookie' |
||
73 | ]; |
||
74 | |||
75 | /** |
||
76 | * @param int $rcode |
||
77 | * @return string |
||
78 | */ |
||
79 | private function getMessageByRcode($rcode){ |
||
86 | |||
87 | /** |
||
88 | * Called when new UDP packet received. |
||
89 | * @param string $pct |
||
90 | * @return void |
||
91 | */ |
||
92 | public function onUdpPacket($pct) |
||
219 | |||
220 | /** |
||
221 | * Called when new data received |
||
222 | * @return void |
||
223 | */ |
||
224 | public function onRead() |
||
250 | |||
251 | /** |
||
252 | * Gets the host information |
||
253 | * @param string $hostname Hostname |
||
254 | * @param callable $cb Callback |
||
255 | * @callback $cb ( ) |
||
256 | * @return void |
||
257 | */ |
||
258 | public function get($hostname, $cb) |
||
301 | |||
302 | /** |
||
303 | * Called when connection finishes |
||
304 | * @return void |
||
305 | */ |
||
306 | public function onFinish() |
||
311 | } |
||
312 |
This check looks
TODO
comments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.