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 DBMysqli 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 DBMysqli, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class DBMysqli extends DBMysql |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Constructor |
||
| 21 | * @return void |
||
| 22 | */ |
||
| 23 | function DBMysqli($auto_connect = TRUE) |
||
|
|
|||
| 24 | { |
||
| 25 | $this->_setDBInfo(); |
||
| 26 | if($auto_connect) $this->_connect(); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Create an instance of this class |
||
| 31 | * @return DBMysqli return DBMysqli object instance |
||
| 32 | */ |
||
| 33 | function create() |
||
| 37 | |||
| 38 | /** |
||
| 39 | * DB Connect |
||
| 40 | * this method is private |
||
| 41 | * @param array $connection connection's value is db_hostname, db_port, db_database, db_userid, db_password |
||
| 42 | * @return resource |
||
| 43 | */ |
||
| 44 | View Code Duplication | function __connect($connection) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * DB disconnection |
||
| 74 | * this method is private |
||
| 75 | * @param resource $connection |
||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | function _close($connection) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Handles quatation of the string variables from the query |
||
| 85 | * @param string $string |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | View Code Duplication | function addQuotes($string) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Execute the query |
||
| 104 | * this method is private |
||
| 105 | * @param string $query |
||
| 106 | * @param resource $connection |
||
| 107 | * @return resource |
||
| 108 | */ |
||
| 109 | View Code Duplication | function __query($query, $connection) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Before execute query, prepare statement |
||
| 168 | * this method is private |
||
| 169 | * @param string $types |
||
| 170 | * @param array $params |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | View Code Duplication | function _prepareQueryParameters(&$types, &$params) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Fetch the result |
||
| 223 | * @param resource $result |
||
| 224 | * @param int|NULL $arrayIndexEndValue |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | View Code Duplication | function _fetch($result, $arrayIndexEndValue = NULL) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Handles insertAct |
||
| 327 | * @param Object $queryObject |
||
| 328 | * @param boolean $with_values |
||
| 329 | * @return resource |
||
| 330 | */ |
||
| 331 | function _executeInsertAct($queryObject, $with_values = false) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Handles updateAct |
||
| 345 | * @param Object $queryObject |
||
| 346 | * @param boolean $with_values |
||
| 347 | * @return resource |
||
| 348 | */ |
||
| 349 | function _executeUpdateAct($queryObject, $with_values = false) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Handles deleteAct |
||
| 363 | * @param Object $queryObject |
||
| 364 | * @param boolean $with_values |
||
| 365 | * @return resource |
||
| 366 | */ |
||
| 367 | function _executeDeleteAct($queryObject, $with_values = false) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Handle selectAct |
||
| 381 | * In order to get a list of pages easily when selecting \n |
||
| 382 | * it supports a method as navigation |
||
| 383 | * @param Object $queryObject |
||
| 384 | * @param resource $connection |
||
| 385 | * @param boolean $with_values |
||
| 386 | * @return Object |
||
| 387 | */ |
||
| 388 | View Code Duplication | function _executeSelectAct($queryObject, $connection = null, $with_values = false) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Get the ID generated in the last query |
||
| 402 | * Return next sequence from sequence table |
||
| 403 | * This method use only mysql |
||
| 404 | * @return int |
||
| 405 | */ |
||
| 406 | function db_insert_id() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Fetch a result row as an object |
||
| 414 | * @param resource $result |
||
| 415 | * @return object |
||
| 416 | */ |
||
| 417 | function db_fetch_object(&$result) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Free result memory |
||
| 424 | * @param resource $result |
||
| 425 | * @return boolean Returns TRUE on success or FALSE on failure. |
||
| 426 | */ |
||
| 427 | function db_free_result(&$result) |
||
| 431 | |||
| 432 | } |
||
| 433 | |||
| 438 |