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 |
||
9 | class Connection extends ClientConnection { |
||
10 | |||
11 | /** |
||
12 | * @var integer Sequence. Pointer of packet sequence |
||
13 | */ |
||
14 | public $seq = 0; |
||
15 | |||
16 | /** |
||
17 | * @var integer Client flags |
||
18 | */ |
||
19 | public $clientFlags = 239237; |
||
20 | |||
21 | /** |
||
22 | * @var integer |
||
23 | */ |
||
24 | public $threadId; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | public $scramble; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | public $serverver; |
||
35 | |||
36 | /** |
||
37 | * @var integer |
||
38 | */ |
||
39 | public $serverCaps; |
||
40 | |||
41 | /** |
||
42 | * @var integer |
||
43 | */ |
||
44 | public $serverLang; |
||
45 | |||
46 | /** |
||
47 | * @var integer Server flags: http://dev.mysql.com/doc/internals/en/status-flags.html |
||
48 | */ |
||
49 | public $serverStatus; |
||
50 | |||
51 | /** |
||
52 | * @var integer Number of warnings generated by the command |
||
53 | */ |
||
54 | public $warnCount; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | public $message; |
||
60 | |||
61 | /** |
||
62 | * @var integer Charset number (see MySQL charset list) |
||
63 | */ |
||
64 | public $charsetNumber = 0x21; |
||
65 | |||
66 | /** |
||
67 | * @var string User name |
||
68 | */ |
||
69 | protected $user = 'root'; |
||
70 | |||
71 | /** |
||
72 | * @var string Password |
||
73 | */ |
||
74 | protected $password = ''; |
||
75 | |||
76 | /** |
||
77 | * @var string Database name |
||
78 | */ |
||
79 | public $dbname = ''; |
||
80 | |||
81 | /** |
||
82 | * @TODO DESCR |
||
|
|||
83 | */ |
||
84 | const STATE_STANDBY = 0; |
||
85 | |||
86 | /** |
||
87 | * @TODO DESCR |
||
88 | */ |
||
89 | const STATE_BODY = 1; |
||
90 | |||
91 | /** |
||
92 | * @var string Phase |
||
93 | */ |
||
94 | protected $phase = 0; |
||
95 | |||
96 | /** |
||
97 | * @TODO DESCR |
||
98 | */ |
||
99 | const PHASE_GOT_INIT = 1; |
||
100 | |||
101 | /** |
||
102 | * @TODO DESCR |
||
103 | */ |
||
104 | const PHASE_AUTH_SENT = 2; |
||
105 | |||
106 | /** |
||
107 | * @TODO DESCR |
||
108 | */ |
||
109 | const PHASE_AUTH_ERR = 3; |
||
110 | |||
111 | /** |
||
112 | * @TODO DESCR |
||
113 | */ |
||
114 | const PHASE_HANDSHAKED = 4; |
||
115 | |||
116 | /** |
||
117 | * @var integer State of pointer of incoming data. 0 - Result Set Header Packet, 1 - Field Packet, 2 - Row Packet |
||
118 | */ |
||
119 | protected $rsState = 0; |
||
120 | |||
121 | /** |
||
122 | * @TODO DESCR |
||
123 | */ |
||
124 | const RS_STATE_HEADER = 0; |
||
125 | |||
126 | /** |
||
127 | * @TODO DESCR |
||
128 | */ |
||
129 | const RS_STATE_FIELD = 1; |
||
130 | |||
131 | /** |
||
132 | * @TODO DESCR |
||
133 | */ |
||
134 | const RS_STATE_ROW = 2; |
||
135 | |||
136 | /** |
||
137 | * @var integer Packet size |
||
138 | */ |
||
139 | protected $pctSize = 0; |
||
140 | |||
141 | /** |
||
142 | * @var array Result rows |
||
143 | */ |
||
144 | public $resultRows = []; |
||
145 | |||
146 | /** |
||
147 | * @var array Result fields |
||
148 | */ |
||
149 | public $resultFields = []; |
||
150 | |||
151 | /** |
||
152 | * @var object Property holds a reference to user's object |
||
153 | */ |
||
154 | public $context; |
||
155 | |||
156 | /** |
||
157 | * @var integer INSERT_ID() |
||
158 | */ |
||
159 | public $insertId; |
||
160 | |||
161 | /** |
||
162 | * @var integer Affected rows |
||
163 | */ |
||
164 | public $affectedRows; |
||
165 | |||
166 | /** |
||
167 | * @var integer Protocol version |
||
168 | */ |
||
169 | public $protover = 0; |
||
170 | |||
171 | /** |
||
172 | * @var integer Timeout |
||
173 | */ |
||
174 | public $timeout = 120; |
||
175 | |||
176 | /** |
||
177 | * @var integer Error number |
||
178 | */ |
||
179 | public $errno = 0; |
||
180 | |||
181 | /** |
||
182 | * @var string Error message |
||
183 | */ |
||
184 | public $errmsg = ''; |
||
185 | |||
186 | /** |
||
187 | * @var integer Low mark |
||
188 | */ |
||
189 | protected $lowMark = 4; |
||
190 | |||
191 | /** |
||
192 | * Executes the given callback when/if the connection is handshaked |
||
193 | * @param callable $cb Callback |
||
194 | * @callback $cb ( Connection $conn, boolean $success ) |
||
195 | * @return void |
||
196 | */ |
||
197 | View Code Duplication | public function onConnected($cb) { |
|
211 | |||
212 | /** |
||
213 | * Called when the connection is handshaked (at low-level), and peer is ready to recv. data |
||
214 | * @return void |
||
215 | */ |
||
216 | public function onReady() { |
||
221 | |||
222 | /** |
||
223 | * Sends a packet |
||
224 | * @param string $packet Data |
||
225 | * @return boolean Success |
||
226 | */ |
||
227 | public function sendPacket($packet) { |
||
231 | |||
232 | /** |
||
233 | * Builds length-encoded binary string |
||
234 | * @param string $s String |
||
235 | * @return string Resulting binary string |
||
236 | */ |
||
237 | public function buildLenEncodedBinary($s) { |
||
258 | |||
259 | /** |
||
260 | * Parses length-encoded binary integer |
||
261 | * @return integer Result |
||
262 | */ |
||
263 | public function parseEncodedBinary() { |
||
282 | |||
283 | /** |
||
284 | * Parse length-encoded string |
||
285 | * @return integer Result |
||
286 | */ |
||
287 | public function parseEncodedString() { |
||
294 | |||
295 | /** |
||
296 | * Generates auth. token |
||
297 | * @param string $scramble Scramble string |
||
298 | * @param string $password Password |
||
299 | * @return string Result |
||
300 | */ |
||
301 | public function getAuthToken($scramble, $password) { |
||
304 | |||
305 | /** |
||
306 | * Sends auth. packet |
||
307 | * @return void |
||
308 | */ |
||
309 | public function auth() { |
||
342 | |||
343 | /** |
||
344 | * Sends SQL-query |
||
345 | * @param string $q Query |
||
346 | * @param callable $callback Optional. Callback called when response received |
||
347 | * @callback $callback ( Connection $conn, boolean $success ) |
||
348 | * @return boolean Success |
||
349 | */ |
||
350 | public function query($q, $callback = NULL) { |
||
353 | |||
354 | /** |
||
355 | * Sends echo-request |
||
356 | * @param callable $callback Optional. Callback called when response received |
||
357 | * @callback $callback ( Connection $conn, boolean $success ) |
||
358 | * @return boolean Success |
||
359 | */ |
||
360 | public function ping($callback = NULL) { |
||
363 | |||
364 | /** |
||
365 | * Sends arbitrary command |
||
366 | * @param string $cmd Command |
||
367 | * @param string $q Data |
||
368 | * @param callable $callback Optional |
||
369 | * @throws ConnectionFinished |
||
370 | * @callback $callback ( Connection $conn, boolean $success ) |
||
371 | * @return boolean Success |
||
372 | */ |
||
373 | public function command($cmd, $q = '', $callback = NULL) { |
||
388 | |||
389 | /** |
||
390 | * Sets default database name |
||
391 | * @param string $name Database name |
||
392 | * @return boolean Success |
||
393 | */ |
||
394 | public function selectDB($name) { |
||
403 | |||
404 | /** |
||
405 | * Called when new data received |
||
406 | * @return void |
||
407 | */ |
||
408 | public function onRead() { |
||
559 | |||
560 | /** |
||
561 | * Called when connection finishes |
||
562 | * @return void |
||
563 | */ |
||
564 | public function onFinish() |
||
569 | |||
570 | /** |
||
571 | * Called when the whole result received |
||
572 | * @return void |
||
573 | */ |
||
574 | public function onResultDone() { |
||
581 | |||
582 | /** |
||
583 | * Called when error occured |
||
584 | * @return void |
||
585 | */ |
||
586 | public function onError() { |
||
601 | } |
||
602 |
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.