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 |
||
| 53 | class CI_DB_sqlite3_driver extends CI_DB { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Database driver |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | public $dbdriver = 'sqlite3'; |
||
| 61 | |||
| 62 | // -------------------------------------------------------------------- |
||
| 63 | |||
| 64 | /** |
||
| 65 | * ORDER BY random keyword |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $_random_keyword = array('RANDOM()', 'RANDOM()'); |
||
| 70 | |||
| 71 | // -------------------------------------------------------------------- |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Non-persistent database connection |
||
| 75 | * |
||
| 76 | * @param bool $persistent |
||
| 77 | * @return SQLite3 |
||
| 78 | */ |
||
| 79 | public function db_connect($persistent = FALSE) |
||
| 97 | |||
| 98 | // -------------------------------------------------------------------- |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Database version number |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function version() |
||
| 115 | |||
| 116 | // -------------------------------------------------------------------- |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Execute the query |
||
| 120 | * |
||
| 121 | * @todo Implement use of SQLite3::querySingle(), if needed |
||
| 122 | * @param string $sql |
||
| 123 | * @return mixed SQLite3Result object or bool |
||
| 124 | */ |
||
| 125 | protected function _execute($sql) |
||
| 131 | |||
| 132 | // -------------------------------------------------------------------- |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Begin Transaction |
||
| 136 | * |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | protected function _trans_begin() |
||
| 143 | |||
| 144 | // -------------------------------------------------------------------- |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Commit Transaction |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | protected function _trans_commit() |
||
| 155 | |||
| 156 | // -------------------------------------------------------------------- |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Rollback Transaction |
||
| 160 | * |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | protected function _trans_rollback() |
||
| 167 | |||
| 168 | // -------------------------------------------------------------------- |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Platform-dependant string escape |
||
| 172 | * |
||
| 173 | * @param string |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | protected function _escape_str($str) |
||
| 180 | |||
| 181 | // -------------------------------------------------------------------- |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Affected Rows |
||
| 185 | * |
||
| 186 | * @return int |
||
| 187 | */ |
||
| 188 | public function affected_rows() |
||
| 192 | |||
| 193 | // -------------------------------------------------------------------- |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Insert ID |
||
| 197 | * |
||
| 198 | * @return int |
||
| 199 | */ |
||
| 200 | public function insert_id() |
||
| 204 | |||
| 205 | // -------------------------------------------------------------------- |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Show table query |
||
| 209 | * |
||
| 210 | * Generates a platform-specific query string so that the table names can be fetched |
||
| 211 | * |
||
| 212 | * @param bool $prefix_limit |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | protected function _list_tables($prefix_limit = FALSE) |
||
| 222 | |||
| 223 | // -------------------------------------------------------------------- |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Fetch Field Names |
||
| 227 | * |
||
| 228 | * @param string $table Table name |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | View Code Duplication | public function list_fields($table) |
|
| 252 | |||
| 253 | // -------------------------------------------------------------------- |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Returns an object with field data |
||
| 257 | * |
||
| 258 | * @param string $table |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | View Code Duplication | public function field_data($table) |
|
| 287 | |||
| 288 | // -------------------------------------------------------------------- |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Error |
||
| 292 | * |
||
| 293 | * Returns an array containing code and message of the last |
||
| 294 | * database error that has occured. |
||
| 295 | * |
||
| 296 | * @return array |
||
| 297 | */ |
||
| 298 | public function error() |
||
| 302 | |||
| 303 | // -------------------------------------------------------------------- |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Replace statement |
||
| 307 | * |
||
| 308 | * Generates a platform-specific replace string from the supplied data |
||
| 309 | * |
||
| 310 | * @param string $table Table name |
||
| 311 | * @param array $keys INSERT keys |
||
| 312 | * @param array $values INSERT values |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | protected function _replace($table, $keys, $values) |
||
| 319 | |||
| 320 | // -------------------------------------------------------------------- |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Truncate statement |
||
| 324 | * |
||
| 325 | * Generates a platform-specific truncate string from the supplied data |
||
| 326 | * |
||
| 327 | * If the database does not support the TRUNCATE statement, |
||
| 328 | * then this method maps to 'DELETE FROM table' |
||
| 329 | * |
||
| 330 | * @param string $table |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | protected function _truncate($table) |
||
| 337 | |||
| 338 | // -------------------------------------------------------------------- |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Close DB Connection |
||
| 342 | * |
||
| 343 | * @return void |
||
| 344 | */ |
||
| 345 | protected function _close() |
||
| 349 | |||
| 350 | } |
||
| 351 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.