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 |
||
10 | class Connection extends ClientConnection |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * @var string Protocol version |
||
15 | */ |
||
16 | public $protover = '3.0'; |
||
17 | |||
18 | /** |
||
19 | * @var integer Maximum packet size |
||
20 | */ |
||
21 | public $maxPacketSize = 0x1000000; |
||
22 | |||
23 | /** |
||
24 | * @var integer Charset number |
||
25 | */ |
||
26 | public $charsetNumber = 0x08; |
||
27 | |||
28 | /** |
||
29 | * @var string Database name |
||
30 | */ |
||
31 | public $dbname = ''; |
||
32 | |||
33 | /** |
||
34 | * @var string Username |
||
35 | */ |
||
36 | protected $user = 'root'; |
||
37 | |||
38 | /** |
||
39 | * @var string Password |
||
40 | */ |
||
41 | protected $password = ''; |
||
42 | |||
43 | /** |
||
44 | * @var string Default options |
||
45 | */ |
||
46 | public $options = ''; |
||
47 | |||
48 | /** |
||
49 | * @var integer Connection's state. 0 - start, 1 - got initial packet, 2 - auth. packet sent, 3 - auth. error, 4 - handshaked OK |
||
50 | */ |
||
51 | public $state = 0; |
||
52 | |||
53 | /** |
||
54 | * @var string State of pointer of incoming data. 0 - Result Set Header Packet, 1 - Field Packet, 2 - Row Packet |
||
55 | */ |
||
56 | public $instate = 0; |
||
57 | |||
58 | /** |
||
59 | * @var array Resulting rows |
||
60 | */ |
||
61 | public $resultRows = []; |
||
62 | |||
63 | /** |
||
64 | * @var array Resulting fields |
||
65 | */ |
||
66 | public $resultFields = []; |
||
67 | |||
68 | /** |
||
69 | * @var string Equals to INSERT_ID(). |
||
70 | */ |
||
71 | public $insertId; |
||
72 | |||
73 | /** |
||
74 | * @var integer Inserted rows number |
||
75 | */ |
||
76 | public $insertNum; |
||
77 | |||
78 | /** |
||
79 | * @var integer Number of affected rows |
||
80 | */ |
||
81 | public $affectedRows; |
||
82 | |||
83 | /** |
||
84 | * @var array Runtime parameters from server |
||
85 | */ |
||
86 | public $parameters = []; |
||
87 | |||
88 | /** |
||
89 | * @var string Backend key |
||
90 | */ |
||
91 | public $backendKey; |
||
92 | |||
93 | /** |
||
94 | * @var int |
||
95 | */ |
||
96 | public $errno = 0; |
||
97 | |||
98 | /** |
||
99 | * @var string |
||
100 | */ |
||
101 | public $errmsg; |
||
102 | |||
103 | /** |
||
104 | * @var |
||
105 | */ |
||
106 | public $status; |
||
107 | |||
108 | /** |
||
109 | * State: authentication packet sent |
||
110 | */ |
||
111 | const STATE_AUTH_PACKET_SENT = 2; |
||
112 | |||
113 | /** |
||
114 | * State: authencation error |
||
115 | */ |
||
116 | const STATE_AUTH_ERROR = 3; |
||
117 | |||
118 | /** |
||
119 | * State: authentication passed |
||
120 | */ |
||
121 | const STATE_AUTH_OK = 4; |
||
122 | |||
123 | /** |
||
124 | * Called when the stream is handshaked (at low-level), and peer is ready to recv. data |
||
125 | * @return void |
||
126 | */ |
||
127 | public function onReady() |
||
153 | |||
154 | /** |
||
155 | * Executes the given callback when/if the connection is handshaked. |
||
156 | * @param callable $cb Callback |
||
157 | * @callback $cb ( ) |
||
158 | * @return void |
||
159 | */ |
||
160 | View Code Duplication | public function onConnected($cb) |
|
173 | |||
174 | /** |
||
175 | * Converts binary string to integer |
||
176 | * @param string $str Binary string |
||
177 | * @param boolean $l Optional. Little endian. Default value - true. |
||
178 | * @return integer Resulting integer |
||
179 | */ |
||
180 | View Code Duplication | public function bytes2int($str, $l = true) |
|
195 | |||
196 | /** |
||
197 | * Converts integer to binary string |
||
198 | * @param integer $len Length |
||
199 | * @param integer $int Integer |
||
200 | * @param boolean $l Optional. Little endian. Default value - true. |
||
201 | * @return string Resulting binary string |
||
202 | */ |
||
203 | View Code Duplication | public function int2bytes($len, $int = 0, $l = true) |
|
224 | |||
225 | /** |
||
226 | * Send a packet |
||
227 | * @param string $type Data |
||
228 | * @param string $packet Packet |
||
229 | * @return boolean Success |
||
230 | */ |
||
231 | public function sendPacket($type, $packet) |
||
244 | |||
245 | /** |
||
246 | * Builds length-encoded binary string |
||
247 | * @param string $s String |
||
248 | * @return string Resulting binary string |
||
249 | */ |
||
250 | public function buildLenEncodedBinary($s) |
||
272 | |||
273 | /** |
||
274 | * Parses length-encoded binary |
||
275 | * @param string &$s Reference to source string |
||
276 | * @param integer &$p |
||
277 | * @return integer Result |
||
278 | */ |
||
279 | public function parseEncodedBinary(&$s, &$p) |
||
315 | |||
316 | /** |
||
317 | * Parse length-encoded string |
||
318 | * @param string &$s Reference to source string |
||
319 | * @param integer &$p Reference to pointer |
||
320 | * @return integer Result |
||
321 | */ |
||
322 | public function parseEncodedString(&$s, &$p) |
||
335 | |||
336 | /** |
||
337 | * Send SQL-query |
||
338 | * @param string $q Query |
||
339 | * @param callable $callback Optional. Callback called when response received. |
||
340 | * @callback $callback ( ) |
||
341 | * @return boolean Success |
||
342 | */ |
||
343 | public function query($q, $callback = null) |
||
347 | |||
348 | /** |
||
349 | * Send echo-request |
||
350 | * @param callable $callback Optional. Callback called when response received |
||
351 | * @callback $callback ( ) |
||
352 | * @return boolean Success |
||
353 | */ |
||
354 | public function ping($callback = null) |
||
359 | |||
360 | /** |
||
361 | * Sends sync-request |
||
362 | * @param callable $cb Optional. Callback called when response received. |
||
363 | * @callback $cb ( ) |
||
364 | * @return boolean Success |
||
365 | */ |
||
366 | public function sync($cb = null) |
||
370 | |||
371 | /** |
||
372 | * Send terminate-request to shutdown the connection |
||
373 | * @param callable $cb Optional. Callback called when response received. |
||
374 | * @callback $cb ( ) |
||
375 | * @return boolean Success |
||
376 | */ |
||
377 | public function terminate($cb = null) |
||
381 | |||
382 | /** |
||
383 | * Sends arbitrary command |
||
384 | * @param integer $cmd Command's code. See constants above. |
||
385 | * @param string $q Data |
||
386 | * @param callable $cb Optional. Callback called when response received. |
||
387 | * @callback $cb ( ) |
||
388 | * @return boolean Success |
||
389 | */ |
||
390 | View Code Duplication | public function command($cmd, $q = '', $cb = null) |
|
403 | |||
404 | /** |
||
405 | * Set default database name |
||
406 | * @param string $name Database name |
||
407 | * @return boolean Success |
||
408 | */ |
||
409 | public function selectDB($name) |
||
419 | |||
420 | /** |
||
421 | * |
||
422 | */ |
||
423 | public function onRead() |
||
633 | |||
634 | /** |
||
635 | * Decode strings from the NUL-terminated representation |
||
636 | * @param string $data Binary data |
||
637 | * @param integer $limit Optional. Limit of count. Default is 1. |
||
638 | * @param reference &$p Optional. Pointer. |
||
639 | * @return array Decoded strings |
||
640 | */ |
||
641 | public function decodeNULstrings($data, $limit = 1, &$p = 0) |
||
659 | |||
660 | /** |
||
661 | * Called when the whole result received |
||
662 | * @return void |
||
663 | */ |
||
664 | public function onResultDone() |
||
676 | |||
677 | /** |
||
678 | * Called when error occured |
||
679 | * @return void |
||
680 | */ |
||
681 | public function onError() |
||
698 | } |
||
699 |
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.