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 |
||
| 24 | abstract class Give_DB { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The name of our database table |
||
| 28 | * |
||
| 29 | * @since 1.0 |
||
| 30 | * @access public |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | public $table_name; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The version of our database table |
||
| 38 | * |
||
| 39 | * @since 1.0 |
||
| 40 | * @access public |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | public $version; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The name of the primary column |
||
| 48 | * |
||
| 49 | * @since 1.0 |
||
| 50 | * @access public |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | public $primary_key; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Class Constructor |
||
| 58 | * |
||
| 59 | * Set up the Give DB Class. |
||
| 60 | * |
||
| 61 | * @since 1.0 |
||
| 62 | * @access public |
||
| 63 | */ |
||
| 64 | public function __construct() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Whitelist of columns |
||
| 69 | * |
||
| 70 | * @since 1.0 |
||
| 71 | * @access public |
||
| 72 | * |
||
| 73 | * @return array Columns and formats. |
||
| 74 | */ |
||
| 75 | public function get_columns() { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Default column values |
||
| 81 | * |
||
| 82 | * @since 1.0 |
||
| 83 | * @access public |
||
| 84 | * |
||
| 85 | * @return array Default column values. |
||
| 86 | */ |
||
| 87 | public function get_column_defaults() { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Retrieve a row by the primary key |
||
| 93 | * |
||
| 94 | * @since 1.0 |
||
| 95 | * @access public |
||
| 96 | * |
||
| 97 | * @param int $row_id Row ID. |
||
| 98 | * |
||
| 99 | * @return object |
||
| 100 | */ |
||
| 101 | public function get( $row_id ) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Retrieve a row by a specific column / value |
||
| 115 | * |
||
| 116 | * @since 1.0 |
||
| 117 | * @access public |
||
| 118 | * |
||
| 119 | * @param int $column Column ID. |
||
| 120 | * @param int $row_id Row ID. |
||
| 121 | * |
||
| 122 | * @return object |
||
| 123 | */ |
||
| 124 | View Code Duplication | public function get_by( $column, $row_id ) { |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Retrieve a specific column's value by the primary key |
||
| 140 | * |
||
| 141 | * @since 1.0 |
||
| 142 | * @access public |
||
| 143 | * |
||
| 144 | * @param int $column Column ID. |
||
| 145 | * @param int $row_id Row ID. |
||
| 146 | * |
||
| 147 | * @return string Column value. |
||
| 148 | */ |
||
| 149 | View Code Duplication | public function get_column( $column, $row_id ) { |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Retrieve a specific column's value by the the specified column / value |
||
| 165 | * |
||
| 166 | * @since 1.0 |
||
| 167 | * @access public |
||
| 168 | * |
||
| 169 | * @param int $column Column ID. |
||
| 170 | * @param string $column_where Column name. |
||
| 171 | * @param string $column_value Column value. |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function get_column_by( $column, $column_where, $column_value ) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Insert a new row |
||
| 191 | * |
||
| 192 | * @since 1.0 |
||
| 193 | * @access public |
||
| 194 | * |
||
| 195 | * @param array $data |
||
| 196 | * @param string $type |
||
| 197 | * |
||
| 198 | * @return int |
||
| 199 | */ |
||
| 200 | public function insert( $data, $type = '' ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Update a row |
||
| 246 | * |
||
| 247 | * @since 1.0 |
||
| 248 | * @access public |
||
| 249 | * |
||
| 250 | * @param int $row_id Column ID |
||
| 251 | * @param array $data |
||
| 252 | * @param string $where Column value |
||
| 253 | * |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | public function update( $row_id, $data = array(), $where = '' ) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Delete a row identified by the primary key |
||
| 293 | * |
||
| 294 | * @since 1.0 |
||
| 295 | * @access public |
||
| 296 | * |
||
| 297 | * @param int $row_id Column ID. |
||
| 298 | * |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | public function delete( $row_id = 0 ) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Check if the given table exists |
||
| 321 | * |
||
| 322 | * @since 1.3.2 |
||
| 323 | * @access public |
||
| 324 | * |
||
| 325 | * @param string $table The table name. |
||
| 326 | * |
||
| 327 | * @return bool If the table name exists. |
||
| 328 | */ |
||
| 329 | public function table_exists( $table ) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Checks whether column exists in a table or not. |
||
| 340 | * |
||
| 341 | * @param string $column_name Name of the Column in Database Table. |
||
| 342 | * |
||
| 343 | * @since 1.8.18 |
||
| 344 | * |
||
| 345 | * @see https://gist.github.com/datafeedr/54e89e07f87232fb055121bb766743fe |
||
| 346 | * |
||
| 347 | * @return bool |
||
| 348 | */ |
||
| 349 | public function is_column_exists( $column_name ) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Check if the table was ever installed |
||
| 367 | * |
||
| 368 | * @since 1.6 |
||
| 369 | * @access public |
||
| 370 | * |
||
| 371 | * @return bool Returns if the customers table was installed and upgrade routine run. |
||
| 372 | */ |
||
| 373 | public function installed() { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Register tables |
||
| 379 | * |
||
| 380 | * @since 1.8.9 |
||
| 381 | * @access public |
||
| 382 | */ |
||
| 383 | public function register_table() { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Create table |
||
| 392 | * |
||
| 393 | * @since 1.8.9 |
||
| 394 | * @access public |
||
| 395 | */ |
||
| 396 | public function create_table(){} |
||
| 397 | } |
||
| 398 |