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 |
||
| 14 | class Connection extends ClientConnection |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var boolean |
||
| 19 | */ |
||
| 20 | public $use_encryption = false; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var boolean |
||
| 24 | */ |
||
| 25 | public $authorized; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var integer |
||
| 29 | */ |
||
| 30 | public $lastId = 0; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var XMPPRoster |
||
| 34 | */ |
||
| 35 | public $roster; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var XMLStream |
||
| 39 | */ |
||
| 40 | public $xml; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | public $fulljid; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var integer|string Timer ID |
||
| 49 | */ |
||
| 50 | public $keepaliveTimer; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get next ID |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function getId() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Called when the connection is handshaked (at low-level), and peer is ready to recv. data |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | public function onReady() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Called when session finishes |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | public function onFinish() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @TODO DESCR |
||
| 94 | * @param string $s |
||
| 95 | */ |
||
| 96 | public function sendXML($s) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @TODO DESCR |
||
| 104 | */ |
||
| 105 | public function startXMLStream() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @TODO DESCR |
||
| 115 | * @param string $xml |
||
| 116 | * @param callable $cb |
||
| 117 | * @callback $cb ( ) |
||
| 118 | * @return boolean |
||
| 119 | */ |
||
| 120 | View Code Duplication | public function iqSet($xml, $cb) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @TODO DESCR |
||
| 133 | * @param string $to |
||
| 134 | * @param string $xml |
||
| 135 | * @param callable $cb |
||
| 136 | * @callback $cb ( ) |
||
| 137 | * @return boolean |
||
| 138 | */ |
||
| 139 | View Code Duplication | public function iqSetTo($to, $xml, $cb) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @TODO DESCR |
||
| 152 | * @param string $xml |
||
| 153 | * @param callable $cb |
||
| 154 | * @callback $cb ( ) |
||
| 155 | * @return boolean |
||
| 156 | */ |
||
| 157 | View Code Duplication | public function iqGet($xml, $cb) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @TODO DESCR |
||
| 170 | * @param string $to |
||
| 171 | * @param string $xml |
||
| 172 | * @param callable $cb |
||
| 173 | * @callback $cb ( ) |
||
| 174 | * @return boolean |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function iqGetTo($to, $xml, $cb) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @TODO DESCR |
||
| 189 | * @param string $to |
||
| 190 | * @param callable $cb |
||
| 191 | * @callback $cb ( ) |
||
| 192 | * @return boolean |
||
| 193 | */ |
||
| 194 | public function ping($to = null, $cb = null) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @TODO DESCR |
||
| 208 | * @param string $ns |
||
| 209 | * @param callable $cb |
||
| 210 | * @callback $cb ( ) |
||
| 211 | * @return boolean |
||
| 212 | */ |
||
| 213 | public function queryGet($ns, $cb) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @TODO DESCR |
||
| 220 | * @param string $ns |
||
| 221 | * @param string $xml |
||
| 222 | * @param callable $cb |
||
| 223 | * @callback $cb ( ) |
||
| 224 | * @return boolean |
||
| 225 | */ |
||
| 226 | public function querySet($ns, $xml, $cb) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @TODO DESCR |
||
| 233 | * @param string $to |
||
| 234 | * @param string $ns |
||
| 235 | * @param string $xml |
||
| 236 | * @param callable $cb |
||
| 237 | * @callback $cb ( ) |
||
| 238 | * @return boolean |
||
| 239 | */ |
||
| 240 | public function querySetTo($to, $ns, $xml, $cb) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @TODO DESCR |
||
| 247 | */ |
||
| 248 | public function createXMLStream() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Send XMPP Message |
||
| 318 | * @param string $to |
||
| 319 | * @param string $body |
||
| 320 | * @param string $type |
||
| 321 | * @param string $subject |
||
| 322 | */ |
||
| 323 | public function message($to, $body, $type = 'chat', $subject = null, $payload = null) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Set Presence |
||
| 348 | * @param string $status |
||
| 349 | * @param string $show |
||
| 350 | * @param string $to |
||
| 351 | * @param string $type |
||
| 352 | * @param integer $priority |
||
| 353 | */ |
||
| 354 | public function presence($status = null, $show = 'available', $to = null, $type = 'available', $priority = 0) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @TODO DESCR |
||
| 397 | * @param string $jid |
||
| 398 | * @param callable $cb |
||
| 399 | * @callback $cb ( ) |
||
| 400 | */ |
||
| 401 | public function getVCard($jid, $cb) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Called when new data received |
||
| 432 | * @return void |
||
| 433 | */ |
||
| 434 | public function onRead() |
||
| 441 | } |
||
| 442 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.