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:
Complex classes like Give_DB often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Give_DB, and based on these observations, apply Extract Interface, too.
| 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 | * Set Minimum Index Length |
||
| 38 | * |
||
| 39 | * @since 2.0.1 |
||
| 40 | * @access public |
||
| 41 | * |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | public $min_index_length = 191; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The version of our database table |
||
| 48 | * |
||
| 49 | * @since 1.0 |
||
| 50 | * @access public |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | public $version; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The name of the primary column |
||
| 58 | * |
||
| 59 | * @since 1.0 |
||
| 60 | * @access public |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | public $primary_key; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Class Constructor |
||
| 68 | * |
||
| 69 | * Set up the Give DB Class. |
||
| 70 | * |
||
| 71 | * @since 1.0 |
||
| 72 | * @access public |
||
| 73 | */ |
||
| 74 | public function __construct() { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Whitelist of columns |
||
| 82 | * |
||
| 83 | * @since 1.0 |
||
| 84 | * @access public |
||
| 85 | * |
||
| 86 | * @return array Columns and formats. |
||
| 87 | */ |
||
| 88 | public function get_columns() { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Default column values |
||
| 94 | * |
||
| 95 | * @since 1.0 |
||
| 96 | * @access public |
||
| 97 | * |
||
| 98 | * @return array Default column values. |
||
| 99 | */ |
||
| 100 | public function get_column_defaults() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Retrieve a row by the primary key |
||
| 106 | * |
||
| 107 | * @since 1.0 |
||
| 108 | * @access public |
||
| 109 | * |
||
| 110 | * @param int $row_id Row ID. |
||
| 111 | * |
||
| 112 | * @return object |
||
| 113 | */ |
||
| 114 | public function get( $row_id ) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Retrieve a row by a specific column / value |
||
| 128 | * |
||
| 129 | * @since 1.0 |
||
| 130 | * @access public |
||
| 131 | * |
||
| 132 | * @param int $column Column ID. |
||
| 133 | * @param int $row_id Row ID. |
||
| 134 | * |
||
| 135 | * @return object |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function get_by( $column, $row_id ) { |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Retrieve all rows by a specific column / value |
||
| 153 | * |
||
| 154 | * @since 2.2.4 |
||
| 155 | * @access public |
||
| 156 | * |
||
| 157 | * @param int $column Column ID. |
||
| 158 | * @param int $row_id Row ID. |
||
| 159 | * |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | View Code Duplication | public function get_results_by( $column, $row_id ) { |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Retrieve a specific column's value by the primary key |
||
| 178 | * |
||
| 179 | * @since 1.0 |
||
| 180 | * @access public |
||
| 181 | * |
||
| 182 | * @param int $column Column ID. |
||
| 183 | * @param int $row_id Row ID. |
||
| 184 | * |
||
| 185 | * @return string Column value. |
||
| 186 | */ |
||
| 187 | View Code Duplication | public function get_column( $column, $row_id ) { |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Retrieve a specific column's value by the the specified column / value |
||
| 203 | * |
||
| 204 | * @since 1.0 |
||
| 205 | * @access public |
||
| 206 | * |
||
| 207 | * @param int $column Column ID. |
||
| 208 | * @param string $column_where Column name. |
||
| 209 | * @param string $column_value Column value. |
||
| 210 | * |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | public function get_column_by( $column, $column_where, $column_value ) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Insert a new row |
||
| 230 | * |
||
| 231 | * @since 1.0 |
||
| 232 | * @access public |
||
| 233 | * |
||
| 234 | * @param array $data |
||
| 235 | * @param string $type |
||
| 236 | * |
||
| 237 | * @return int |
||
| 238 | */ |
||
| 239 | public function insert( $data, $type = '' ) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Update a row |
||
| 285 | * |
||
| 286 | * @since 1.0 |
||
| 287 | * @access public |
||
| 288 | * |
||
| 289 | * @param int $row_id Column ID |
||
| 290 | * @param array $data |
||
| 291 | * @param string $where Column value |
||
| 292 | * |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | public function update( $row_id, $data = array(), $where = '' ) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Delete a row identified by the primary key |
||
| 332 | * |
||
| 333 | * @since 1.0 |
||
| 334 | * @access public |
||
| 335 | * |
||
| 336 | * @param int $row_id Column ID. |
||
| 337 | * |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | public function delete( $row_id = 0 ) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Check if the given table exists |
||
| 360 | * |
||
| 361 | * @since 1.3.2 |
||
| 362 | * @access public |
||
| 363 | * |
||
| 364 | * @param string $table The table name. |
||
| 365 | * |
||
| 366 | * @return bool If the table name exists. |
||
| 367 | */ |
||
| 368 | public function table_exists( $table ) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Checks whether column exists in a table or not. |
||
| 379 | * |
||
| 380 | * @param string $column_name Name of the Column in Database Table. |
||
| 381 | * |
||
| 382 | * @since 1.8.18 |
||
| 383 | * |
||
| 384 | * @see https://gist.github.com/datafeedr/54e89e07f87232fb055121bb766743fe |
||
| 385 | * |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | public function does_column_exist( $column_name ) { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Check if the table was ever installed |
||
| 406 | * |
||
| 407 | * @since 1.6 |
||
| 408 | * @access public |
||
| 409 | * |
||
| 410 | * @return bool Returns if the customers table was installed and upgrade routine run. |
||
| 411 | */ |
||
| 412 | public function installed() { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Register tables |
||
| 418 | * |
||
| 419 | * @since 1.8.9 |
||
| 420 | * @access public |
||
| 421 | */ |
||
| 422 | public function register_table() { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Create table |
||
| 431 | * |
||
| 432 | * @since 1.8.9 |
||
| 433 | * @access public |
||
| 434 | */ |
||
| 435 | public function create_table() { |
||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * Given a ID, make sure it's a positive number, greater than zero before inserting or adding. |
||
| 441 | * |
||
| 442 | * @access private |
||
| 443 | * @since 2.0 |
||
| 444 | * |
||
| 445 | * @param int $id A passed ID. |
||
| 446 | * |
||
| 447 | * @return int|bool The normalized log ID or false if it's found to not be valid. |
||
| 448 | */ |
||
| 449 | public function sanitize_id( $id ) { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Handle switch blog on multi-site |
||
| 471 | * |
||
| 472 | * @since 2.0.4 |
||
| 473 | * |
||
| 474 | * @access public |
||
| 475 | * |
||
| 476 | * @param $new_blog_id |
||
| 477 | * @param $prev_blog_id |
||
| 478 | */ |
||
| 479 | public function handle_switch_blog( $new_blog_id, $prev_blog_id ) { |
||
| 499 | } |
||
| 500 |