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:
| 1 | <?php |
||
| 18 | class MySqlDatabase { |
||
| 19 | |||
| 20 | private $host; |
||
| 21 | private $user; |
||
| 22 | private $password; |
||
| 23 | private $database; |
||
| 24 | public $queryCount = 0; |
||
| 25 | public $queries = array(); |
||
| 26 | public $conn; |
||
| 27 | |||
| 28 | /*------------------------------------ |
||
| 29 | CONFIG CONNECTION |
||
| 30 | ------------------------------------*/ |
||
| 31 | |||
| 32 | function __construct($host, $user, $password) { |
||
| 37 | |||
| 38 | function connect($new = false) { |
||
| 44 | |||
| 45 | function changeDatabase($database) { |
||
| 53 | |||
| 54 | function lazyLoadConnection() { |
||
| 58 | |||
| 59 | /*----------------------------------- |
||
| 60 | QUERY |
||
| 61 | ------------------------------------*/ |
||
| 62 | |||
| 63 | function query($sql) { |
||
| 74 | |||
| 75 | /*----------------------------------- |
||
| 76 | DEBUGGING |
||
| 77 | ------------------------------------*/ |
||
| 78 | |||
| 79 | function logQuery($sql, $start) { |
||
| 86 | |||
| 87 | function getTime() { |
||
| 94 | |||
| 95 | View Code Duplication | public function getReadableTime($time) { |
|
| 110 | |||
| 111 | function __destruct() { |
||
| 114 | |||
| 115 | } |
||
| 116 | |||
| 118 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.