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 /** PgsqlDriverMicro */ |
||
| 19 | class PgsqlDriver extends Driver |
||
| 20 | { |
||
| 21 | /** @var string $tableSchema Table schema for postgres */ |
||
| 22 | protected $tableSchema = 'public'; |
||
| 23 | |||
| 24 | |||
| 25 | /** |
||
| 26 | * Driver constructor. |
||
| 27 | * |
||
| 28 | * @access public |
||
| 29 | * |
||
| 30 | * @param string $dsn DSN connection string |
||
| 31 | * @param array $config Configuration of connection |
||
| 32 | * @param array $options Other options |
||
| 33 | * |
||
| 34 | * @result void |
||
| 35 | * @throws Exception |
||
| 36 | */ |
||
| 37 | public function __construct($dsn, array $config = [], array $options = []) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Set current database |
||
| 48 | * |
||
| 49 | * @access public |
||
| 50 | * |
||
| 51 | * @param string $dbName Database name |
||
| 52 | * |
||
| 53 | * @return boolean |
||
| 54 | * @throws \InvalidArgumentException |
||
| 55 | */ |
||
| 56 | public function switchDatabase($dbName) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Info of database |
||
| 63 | * |
||
| 64 | * @access public |
||
| 65 | * |
||
| 66 | * @param string $dbName Database name |
||
| 67 | * |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | public function infoDatabase($dbName) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * List tables in db |
||
| 77 | * |
||
| 78 | * @access public |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | public function listTables() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * List database names on this connection |
||
| 90 | * |
||
| 91 | * @access public |
||
| 92 | * @return mixed |
||
| 93 | */ |
||
| 94 | public function listDatabases() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Create a new table |
||
| 101 | * |
||
| 102 | * @param string $name Table name |
||
| 103 | * @param array $elements Table elements |
||
| 104 | * @param string $params Table params |
||
| 105 | * |
||
| 106 | * @return int |
||
| 107 | */ |
||
| 108 | public function createTable($name, array $elements = [], $params = '') |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Remove table from database |
||
| 115 | * |
||
| 116 | * @access public |
||
| 117 | * |
||
| 118 | * @param string $name Table name |
||
| 119 | * |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | public function removeTable($name) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Clear all data from table |
||
| 129 | * |
||
| 130 | * @access public |
||
| 131 | * |
||
| 132 | * @param string $name Table name |
||
| 133 | * |
||
| 134 | * @return int |
||
| 135 | */ |
||
| 136 | public function clearTable($name) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get array fields into table |
||
| 143 | * |
||
| 144 | * @access public |
||
| 145 | * |
||
| 146 | * @param string $table Table name |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function listFields($table) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get info of a field |
||
| 169 | * |
||
| 170 | * @access public |
||
| 171 | * |
||
| 172 | * @param string $field Field name |
||
| 173 | * @param string $table Table name |
||
| 174 | * |
||
| 175 | * @return array|boolean |
||
| 176 | */ |
||
| 177 | public function fieldInfo($field, $table) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Insert row into table |
||
| 184 | * |
||
| 185 | * @access public |
||
| 186 | * |
||
| 187 | * @param string $table Table name |
||
| 188 | * @param array $line Line or lines to added |
||
| 189 | * @param bool $multi Is multi rows |
||
| 190 | * |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | View Code Duplication | public function insert($table, array $line = [], $multi = false) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Update row in table |
||
| 217 | * |
||
| 218 | * @access public |
||
| 219 | * |
||
| 220 | * @param string $table Table name |
||
| 221 | * @param array $elements Elements to update |
||
| 222 | * @param string $conditions Conditions for search |
||
| 223 | * |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | public function update($table, array $elements = [], $conditions = '') |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Delete row from table |
||
| 233 | * |
||
| 234 | * @access public |
||
| 235 | * |
||
| 236 | * @param string $table Table name |
||
| 237 | * @param string $conditions Conditions to search |
||
| 238 | * @param array $params Params array |
||
| 239 | * |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | public function delete($table, $conditions, array $params = []) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Count element in sub-query |
||
| 249 | * |
||
| 250 | * @access public |
||
| 251 | * |
||
| 252 | * @param string $query Query |
||
| 253 | * @param string $table Table name |
||
| 254 | * |
||
| 255 | * @return integer|boolean |
||
| 256 | */ |
||
| 257 | public function count($query = '', $table = '') |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Exists element in the table by params |
||
| 264 | * |
||
| 265 | * @access public |
||
| 266 | * |
||
| 267 | * @param string $table Table name |
||
| 268 | * @param array $params Params array |
||
| 269 | * |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | public function exists($table, array $params = []) |
||
| 276 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.