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 |
||
| 23 | class Mysqli implements DbInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * The primary table we're working with |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $table = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Any filtering for a WHERE SQL clause |
||
| 33 | * @var mixed |
||
| 34 | */ |
||
| 35 | protected $where = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The database connection credentials |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $credentials = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The database object we're piggybacking on |
||
| 45 | * @var \voku\db\DB |
||
| 46 | */ |
||
| 47 | protected $db = null; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Changes the databse connection to use a new database |
||
| 51 | * @param string $db_name |
||
| 52 | */ |
||
| 53 | public function setDbName($db_name) |
||
| 54 | { |
||
| 55 | @mysqli_select_db($this->getDb()->getLink(), $db_name); |
||
|
|
|||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * (non-PHPdoc) |
||
| 60 | * @see \JaegerApp\Db\DbInterface::select() |
||
| 61 | */ |
||
| 62 | public function select($table, $where = '1=1') |
||
| 63 | { |
||
| 64 | $this->table = $table; |
||
| 65 | $this->where = $where; |
||
| 66 | return $this; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * (non-PHPdoc) |
||
| 71 | * @see \JaegerApp\Db\DbInterface::get() |
||
| 72 | */ |
||
| 73 | public function get() |
||
| 74 | { |
||
| 75 | $data = $this->getDb()->select($this->table, $this->where); |
||
| 76 | return $data->fetchAllArray(); |
||
| 77 | |||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * (non-PHPdoc) |
||
| 82 | * @see \JaegerApp\Db\DbInterface::query() |
||
| 83 | */ |
||
| 84 | public function query($sql = '', $return = false) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * (non-PHPdoc) |
||
| 95 | * @see \JaegerApp\Db\DbInterface::getTableStatus() |
||
| 96 | */ |
||
| 97 | public function getTableStatus() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * (non-PHPdoc) |
||
| 105 | * @see \JaegerApp\Db\DbInterface::getCreateTable() |
||
| 106 | */ |
||
| 107 | public function getCreateTable($table, $if_not_exists = false) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * (non-PHPdoc) |
||
| 128 | * @see \JaegerApp\Db\DbInterface::clear() |
||
| 129 | */ |
||
| 130 | public function clear() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * (non-PHPdoc) |
||
| 138 | * @see \JaegerApp\Db\DbInterface::totalRows() |
||
| 139 | */ |
||
| 140 | public function totalRows($table) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * (non-PHPdoc) |
||
| 155 | * @see \JaegerApp\Db\DbInterface::getColumnns() |
||
| 156 | */ |
||
| 157 | View Code Duplication | public function getColumns($table) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * (non-PHPdoc) |
||
| 169 | * @see \JaegerApp\Db\DbInterface::escape() |
||
| 170 | */ |
||
| 171 | public function escape($string) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * (non-PHPdoc) |
||
| 178 | * @see \JaegerApp\Db\DbInterface::getAllTables() |
||
| 179 | */ |
||
| 180 | public function getAllTables() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * (non-PHPdoc) |
||
| 187 | * @see \JaegerApp\Db\DbInterface::insert() |
||
| 188 | */ |
||
| 189 | public function insert($table, array $data = array()) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * (non-PHPdoc) |
||
| 196 | * @see \JaegerApp\Db\DbInterface::update() |
||
| 197 | */ |
||
| 198 | public function update($table, $data, $where) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * |
||
| 205 | * @param array $credentials |
||
| 206 | * @return \JaegerApp\Db\Mysqli |
||
| 207 | */ |
||
| 208 | public function setCredentials(array $credentials) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * (non-PHPdoc) |
||
| 216 | * @see \JaegerApp\Db\DbInterface::getDb() |
||
| 217 | */ |
||
| 218 | public function getDb($force = false) |
||
| 227 | } |
If you suppress an error, we recommend checking for the error condition explicitly: