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 | /** |
||
| 13 | * @var string Protocol version |
||
| 14 | */ |
||
| 15 | public $protover = '3.0'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var integer Maximum packet size |
||
| 19 | */ |
||
| 20 | public $maxPacketSize = 0x1000000; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var integer Charset number |
||
| 24 | */ |
||
| 25 | public $charsetNumber = 0x08; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string Database name |
||
| 29 | */ |
||
| 30 | public $dbname = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string Username |
||
| 34 | */ |
||
| 35 | protected $user = 'root'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string Password |
||
| 39 | */ |
||
| 40 | protected $password = ''; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string Default options |
||
| 44 | */ |
||
| 45 | public $options = ''; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var integer Connection's state. 0 - start, 1 - got initial packet, 2 - auth. packet sent, 3 - auth. error, 4 - handshaked OK |
||
|
|
|||
| 49 | */ |
||
| 50 | public $state = 0; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string State of pointer of incoming data. 0 - Result Set Header Packet, 1 - Field Packet, 2 - Row Packet |
||
| 54 | */ |
||
| 55 | public $instate = 0; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array Resulting rows |
||
| 59 | */ |
||
| 60 | public $resultRows = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array Resulting fields |
||
| 64 | */ |
||
| 65 | public $resultFields = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string Equals to INSERT_ID(). |
||
| 69 | */ |
||
| 70 | public $insertId; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var integer Inserted rows number |
||
| 74 | */ |
||
| 75 | public $insertNum; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var integer Number of affected rows |
||
| 79 | */ |
||
| 80 | public $affectedRows; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array Runtime parameters from server |
||
| 84 | */ |
||
| 85 | public $parameters = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var string Backend key |
||
| 89 | */ |
||
| 90 | public $backendKey; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * State: authentication packet sent |
||
| 94 | */ |
||
| 95 | const STATE_AUTH_PACKET_SENT = 2; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * State: authencation error |
||
| 99 | */ |
||
| 100 | const STATE_AUTH_ERROR = 3; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * State: authentication passed |
||
| 104 | */ |
||
| 105 | const STATE_AUTH_OK = 4; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Called when the connection is ready to accept new data |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | public function onReady() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Executes the given callback when/if the connection is handshaked. |
||
| 134 | * @param callable $cb Callback |
||
| 135 | * @callback $cb ( ) |
||
| 136 | * @return void |
||
| 137 | */ |
||
| 138 | View Code Duplication | public function onConnected($cb) |
|
| 139 | { |
||
| 140 | if ($this->state === self::STATE_AUTH_ERROR) { |
||
| 141 | $cb($this, false); |
||
| 142 | } elseif ($this->state === self::STATE_AUTH_OK) { |
||
| 143 | $cb($this, true); |
||
| 144 | } else { |
||
| 145 | if (!$this->onConnected) { |
||
| 146 | $this->onConnected = new StackCallbacks(); |
||
| 147 | } |
||
| 148 | $this->onConnected->push($cb); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Converts binary string to integer |
||
| 154 | * @param string $str Binary string |
||
| 155 | * @param boolean $l Optional. Little endian. Default value - true. |
||
| 156 | * @return integer Resulting integer |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function bytes2int($str, $l = true) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Converts integer to binary string |
||
| 176 | * @param integer $len Length |
||
| 177 | * @param integer $int Integer |
||
| 178 | * @param boolean $l Optional. Little endian. Default value - true. |
||
| 179 | * @return string Resulting binary string |
||
| 180 | */ |
||
| 181 | View Code Duplication | public function int2bytes($len, $int = 0, $l = true) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Send a packet |
||
| 205 | * @param string $type Data |
||
| 206 | * @param string $packet Packet |
||
| 207 | * @return boolean Success |
||
| 208 | */ |
||
| 209 | public function sendPacket($type, $packet) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Builds length-encoded binary string |
||
| 225 | * @param string $s String |
||
| 226 | * @return string Resulting binary string |
||
| 227 | */ |
||
| 228 | public function buildLenEncodedBinary($s) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Parses length-encoded binary |
||
| 253 | * @param string &$s Reference to source string |
||
| 254 | * @param integer &$p |
||
| 255 | * @return integer Result |
||
| 256 | */ |
||
| 257 | public function parseEncodedBinary(&$s, &$p) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Parse length-encoded string |
||
| 296 | * @param string &$s Reference to source string |
||
| 297 | * @param integer &$p Reference to pointer |
||
| 298 | * @return integer Result |
||
| 299 | */ |
||
| 300 | public function parseEncodedString(&$s, &$p) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Send SQL-query |
||
| 316 | * @param string $q Query |
||
| 317 | * @param callable $callback Optional. Callback called when response received. |
||
| 318 | * @callback $callback ( ) |
||
| 319 | * @return boolean Success |
||
| 320 | */ |
||
| 321 | public function query($q, $callback = null) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Send echo-request |
||
| 328 | * @param callable $callback Optional. Callback called when response received |
||
| 329 | * @callback $callback ( ) |
||
| 330 | * @return boolean Success |
||
| 331 | */ |
||
| 332 | public function ping($callback = null) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Sends sync-request |
||
| 340 | * @param callable $cb Optional. Callback called when response received. |
||
| 341 | * @callback $cb ( ) |
||
| 342 | * @return boolean Success |
||
| 343 | */ |
||
| 344 | public function sync($cb = null) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Send terminate-request to shutdown the connection |
||
| 351 | * @param callable $cb Optional. Callback called when response received. |
||
| 352 | * @callback $cb ( ) |
||
| 353 | * @return boolean Success |
||
| 354 | */ |
||
| 355 | public function terminate($cb = null) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Sends arbitrary command |
||
| 362 | * @param integer $cmd Command's code. See constants above. |
||
| 363 | * @param string $q Data |
||
| 364 | * @param callable $cb Optional. Callback called when response received. |
||
| 365 | * @callback $cb ( ) |
||
| 366 | * @return boolean Success |
||
| 367 | */ |
||
| 368 | View Code Duplication | public function command($cmd, $q = '', $cb = null) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Set default database name |
||
| 382 | * @param string $name Database name |
||
| 383 | * @return boolean Success |
||
| 384 | */ |
||
| 385 | public function selectDB($name) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Called when new data received |
||
| 398 | * @param string $buf New data |
||
| 399 | * @return void |
||
| 400 | */ |
||
| 401 | public function stdin($buf) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Decode strings from the NUL-terminated representation |
||
| 622 | * @param string $data Binary data |
||
| 623 | * @param integer $limit Optional. Limit of count. Default is 1. |
||
| 624 | * @param reference &$p Optional. Pointer. |
||
| 625 | * @return array Decoded strings |
||
| 626 | */ |
||
| 627 | public function decodeNULstrings($data, $limit = 1, &$p = 0) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Called when the whole result received |
||
| 648 | * @return void |
||
| 649 | */ |
||
| 650 | public function onResultDone() |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Called when error occured |
||
| 664 | * @return void |
||
| 665 | */ |
||
| 666 | public function onError() |
||
| 681 | } |
||
| 682 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.