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 | public function onConnected($cb) { |
||
| 198 | View Code Duplication | if ($this->phase === self::PHASE_AUTH_ERR) { |
|
| 199 | $cb($this, false); |
||
| 200 | } |
||
| 201 | elseif ($this->phase === self::PHASE_HANDSHAKED) { |
||
| 202 | $cb($this, true); |
||
| 203 | } |
||
| 204 | else { |
||
| 205 | if (!$this->onConnected) { |
||
| 206 | $this->onConnected = new StackCallbacks(); |
||
| 207 | } |
||
| 208 | $this->onConnected->push($cb); |
||
| 209 | } |
||
| 210 | } |
||
| 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 $cb Optional. Callback called when response received |
||
| 347 | * @callback $cb ( Connection $conn, boolean $success ) |
||
| 348 | * @return boolean Success |
||
| 349 | */ |
||
| 350 | public function query($q, $cb = null) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Begins a transaction |
||
| 359 | * @param callable $cb Optional. Callback called when response received |
||
| 360 | * @throws ConnectionFinished |
||
| 361 | */ |
||
| 362 | public function begin($cb = null) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Commit a transaction |
||
| 369 | * @param callable $cb Optional. Callback called when response received |
||
| 370 | * @throws ConnectionFinished |
||
| 371 | */ |
||
| 372 | public function commit($cb = null) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Rollback a transaction |
||
| 379 | * @throws ConnectionFinished |
||
| 380 | */ |
||
| 381 | public function rollback($cb = null) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Sends echo-request |
||
| 388 | * @param callable $cb Optional. Callback called when response received |
||
| 389 | * @callback $cb ( Connection $conn, boolean $success ) |
||
| 390 | * @return boolean Success |
||
| 391 | */ |
||
| 392 | public function ping($cb = NULL) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Sends arbitrary command |
||
| 398 | * @param string $cmd Command |
||
| 399 | * @param string $q Data |
||
| 400 | * @param callable $cb Optional |
||
| 401 | * @throws ConnectionFinished |
||
| 402 | * @callback $cb ( Connection $conn, boolean $success ) |
||
| 403 | * @return boolean Success |
||
| 404 | */ |
||
| 405 | View Code Duplication | public function command($cmd, $q = '', $cb = NULL) { |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Sets default database name |
||
| 419 | * @param string $name Database name |
||
| 420 | * @return boolean Success |
||
| 421 | */ |
||
| 422 | public function selectDB($name) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Called when new data received |
||
| 434 | * @return void |
||
| 435 | */ |
||
| 436 | public function onRead() { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Called when connection finishes |
||
| 593 | * @return void |
||
| 594 | */ |
||
| 595 | public function onFinish() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Called when the whole result received |
||
| 603 | * @return void |
||
| 604 | */ |
||
| 605 | public function onResultDone() { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Called when error occured |
||
| 615 | * @return void |
||
| 616 | */ |
||
| 617 | public function onError() { |
||
| 632 | } |
||
| 633 |
This check looks
TODOcomments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.