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 |
||
| 16 | class Connection extends ClientConnection |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var boolean |
||
| 21 | */ |
||
| 22 | public $use_encryption = false; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var boolean |
||
| 26 | */ |
||
| 27 | public $authorized; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var integer |
||
| 31 | */ |
||
| 32 | public $lastId = 0; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var XMPPRoster |
||
| 36 | */ |
||
| 37 | public $roster; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var XMLStream |
||
| 41 | */ |
||
| 42 | public $xml; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | public $fulljid; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var integer|string Timer ID |
||
| 51 | */ |
||
| 52 | public $keepaliveTimer; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get next ID |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function getId() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Called when the connection is handshaked (at low-level), and peer is ready to recv. data |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | public function onReady() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Called when session finishes |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | public function onFinish() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @TODO DESCR |
||
| 96 | * @param string $s |
||
| 97 | */ |
||
| 98 | public function sendXML($s) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @TODO DESCR |
||
| 106 | */ |
||
| 107 | public function startXMLStream() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @TODO DESCR |
||
| 116 | * @param string $xml |
||
| 117 | * @param callable $cb |
||
| 118 | * @callback $cb ( ) |
||
| 119 | * @return boolean |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function iqSet($xml, $cb) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @TODO DESCR |
||
| 134 | * @param string $to |
||
| 135 | * @param string $xml |
||
| 136 | * @param callable $cb |
||
| 137 | * @callback $cb ( ) |
||
| 138 | * @return boolean |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function iqSetTo($to, $xml, $cb) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @TODO DESCR |
||
| 153 | * @param string $xml |
||
| 154 | * @param callable $cb |
||
| 155 | * @callback $cb ( ) |
||
| 156 | * @return boolean |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function iqGet($xml, $cb) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @TODO DESCR |
||
| 171 | * @param string $to |
||
| 172 | * @param string $xml |
||
| 173 | * @param callable $cb |
||
| 174 | * @callback $cb ( ) |
||
| 175 | * @return boolean |
||
| 176 | */ |
||
| 177 | View Code Duplication | public function iqGetTo($to, $xml, $cb) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * @TODO DESCR |
||
| 190 | * @param string $to |
||
| 191 | * @param callable $cb |
||
| 192 | * @callback $cb ( ) |
||
| 193 | * @return boolean |
||
| 194 | */ |
||
| 195 | public function ping($to = null, $cb = null) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @TODO DESCR |
||
| 209 | * @param string $ns |
||
| 210 | * @param callable $cb |
||
| 211 | * @callback $cb ( ) |
||
| 212 | * @return boolean |
||
| 213 | */ |
||
| 214 | public function queryGet($ns, $cb) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @TODO DESCR |
||
| 221 | * @param string $ns |
||
| 222 | * @param string $xml |
||
| 223 | * @param callable $cb |
||
| 224 | * @callback $cb ( ) |
||
| 225 | * @return boolean |
||
| 226 | */ |
||
| 227 | public function querySet($ns, $xml, $cb) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @TODO DESCR |
||
| 234 | * @param string $to |
||
| 235 | * @param string $ns |
||
| 236 | * @param string $xml |
||
| 237 | * @param callable $cb |
||
| 238 | * @callback $cb ( ) |
||
| 239 | * @return boolean |
||
| 240 | */ |
||
| 241 | public function querySetTo($to, $ns, $xml, $cb) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @TODO DESCR |
||
| 248 | */ |
||
| 249 | 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 = null, $cb) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Called when new data received |
||
| 431 | * @return void |
||
| 432 | */ |
||
| 433 | public function onRead() |
||
| 440 | } |
||
| 441 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.