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 /** MysqlDriverMicro */ |
||
| 17 | class MysqlDriver extends Driver |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Set current database |
||
| 21 | * |
||
| 22 | * @access public |
||
| 23 | * |
||
| 24 | * @param string $dbName Database name |
||
| 25 | * |
||
| 26 | * @return boolean |
||
| 27 | */ |
||
| 28 | public function switchDatabase($dbName) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Info of database |
||
| 35 | * |
||
| 36 | * @access public |
||
| 37 | * @param string $dbName Database name |
||
| 38 | * @return array |
||
| 39 | */ |
||
| 40 | public function infoDatabase($dbName) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * List database names on this connection |
||
| 61 | * |
||
| 62 | * @access public |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | public function listDatabases() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * List tables in db |
||
| 85 | * |
||
| 86 | * @access public |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public function listTables() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Create a new table |
||
| 96 | * |
||
| 97 | * @param string $name Table name |
||
| 98 | * @param array $elements Table elements |
||
| 99 | * @param string $params Table params |
||
| 100 | * |
||
| 101 | * @return int |
||
| 102 | */ |
||
| 103 | public function createTable($name, array $elements = [], $params = '') |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get array fields into table |
||
| 112 | * |
||
| 113 | * @access public |
||
| 114 | * |
||
| 115 | * @param string $table Table name |
||
| 116 | * |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | public function listFields($table) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Insert row into table |
||
| 140 | * |
||
| 141 | * @access public |
||
| 142 | * |
||
| 143 | * @param string $table Table name |
||
| 144 | * @param array $line Line or lines to added |
||
| 145 | * @param bool $multi Is multi rows |
||
| 146 | * |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | public function insert($table, array $line = [], $multi = false) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Update row in table |
||
| 174 | * |
||
| 175 | * @access public |
||
| 176 | * |
||
| 177 | * @param string $table Table name |
||
| 178 | * @param array $elements Elements to update |
||
| 179 | * @param string $conditions Conditions for search |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function update($table, array $elements = [], $conditions = '') |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Exists element in the table by params |
||
| 208 | * |
||
| 209 | * @access public |
||
| 210 | * |
||
| 211 | * @param string $table Table name |
||
| 212 | * @param array $params Params array |
||
| 213 | * |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | View Code Duplication | public function exists($table, array $params = []) |
|
| 230 | } |
||
| 231 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.