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 | 52 | * |
|
| 110 | 52 | * @param int $row_id Row ID. |
|
| 111 | 52 | * |
|
| 112 | 52 | * @return object |
|
| 113 | */ |
||
| 114 | public function get( $row_id ) { |
||
| 125 | 2 | ||
| 126 | 2 | /** |
|
| 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 | 52 | */ |
|
| 137 | 52 | View Code Duplication | public function get_by( $column, $row_id ) { |
| 150 | |||
| 151 | 52 | /** |
|
| 152 | * Retrieve a specific column's value by the primary key |
||
| 153 | * |
||
| 154 | 52 | * @since 1.0 |
|
| 155 | 52 | * @access public |
|
| 156 | * |
||
| 157 | 52 | * @param int $column Column ID. |
|
| 158 | * @param int $row_id Row ID. |
||
| 159 | 52 | * |
|
| 160 | * @return string Column value. |
||
| 161 | 52 | */ |
|
| 162 | View Code Duplication | public function get_column( $column, $row_id ) { |
|
| 175 | |||
| 176 | 52 | /** |
|
| 177 | * Retrieve a specific column's value by the the specified column / value |
||
| 178 | 52 | * |
|
| 179 | 1 | * @since 1.0 |
|
| 180 | * @access public |
||
| 181 | * |
||
| 182 | 52 | * @param int $column Column ID. |
|
| 183 | 52 | * @param string $column_where Column name. |
|
| 184 | 52 | * @param string $column_value Column value. |
|
| 185 | * |
||
| 186 | * @return string |
||
| 187 | 52 | */ |
|
| 188 | public function get_column_by( $column, $column_where, $column_value ) { |
||
| 202 | |||
| 203 | 52 | /** |
|
| 204 | * Insert a new row |
||
| 205 | * |
||
| 206 | * @since 1.0 |
||
| 207 | * @access public |
||
| 208 | * |
||
| 209 | * @param array $data |
||
| 210 | * @param string $type |
||
| 211 | * |
||
| 212 | * @return int |
||
| 213 | */ |
||
| 214 | public function insert( $data, $type = '' ) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Update a row |
||
| 260 | * |
||
| 261 | * @since 1.0 |
||
| 262 | * @access public |
||
| 263 | * |
||
| 264 | * @param int $row_id Column ID |
||
| 265 | * @param array $data |
||
| 266 | * @param string $where Column value |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | public function update( $row_id, $data = array(), $where = '' ) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Delete a row identified by the primary key |
||
| 307 | * |
||
| 308 | * @since 1.0 |
||
| 309 | * @access public |
||
| 310 | * |
||
| 311 | * @param int $row_id Column ID. |
||
| 312 | * |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | View Code Duplication | public function delete( $row_id = 0 ) { |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Check if the given table exists |
||
| 335 | * |
||
| 336 | * @since 1.3.2 |
||
| 337 | * @access public |
||
| 338 | * |
||
| 339 | * @param string $table The table name. |
||
| 340 | * |
||
| 341 | * @return bool If the table name exists. |
||
| 342 | */ |
||
| 343 | public function table_exists( $table ) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Checks whether column exists in a table or not. |
||
| 354 | * |
||
| 355 | * @param string $column_name Name of the Column in Database Table. |
||
| 356 | * |
||
| 357 | * @since 1.8.18 |
||
| 358 | * |
||
| 359 | * @see https://gist.github.com/datafeedr/54e89e07f87232fb055121bb766743fe |
||
| 360 | * |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | public function does_column_exist( $column_name ) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Check if the table was ever installed |
||
| 381 | * |
||
| 382 | * @since 1.6 |
||
| 383 | * @access public |
||
| 384 | * |
||
| 385 | * @return bool Returns if the customers table was installed and upgrade routine run. |
||
| 386 | */ |
||
| 387 | public function installed() { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Register tables |
||
| 393 | * |
||
| 394 | * @since 1.8.9 |
||
| 395 | * @access public |
||
| 396 | */ |
||
| 397 | public function register_table() { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Create table |
||
| 406 | * |
||
| 407 | * @since 1.8.9 |
||
| 408 | * @access public |
||
| 409 | */ |
||
| 410 | public function create_table() { |
||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * Given a ID, make sure it's a positive number, greater than zero before inserting or adding. |
||
| 416 | * |
||
| 417 | * @access private |
||
| 418 | * @since 2.0 |
||
| 419 | * |
||
| 420 | * @param int $id A passed ID. |
||
| 421 | * |
||
| 422 | * @return int|bool The normalized log ID or false if it's found to not be valid. |
||
| 423 | */ |
||
| 424 | public function sanitize_id( $id ) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Handle switch blog on multi-site |
||
| 446 | * |
||
| 447 | * @since 2.0.4 |
||
| 448 | * |
||
| 449 | * @access public |
||
| 450 | * |
||
| 451 | * @param $new_blog_id |
||
| 452 | * @param $prev_blog_id |
||
| 453 | */ |
||
| 454 | public function handle_switch_blog( $new_blog_id, $prev_blog_id ) { |
||
| 474 | } |
||
| 475 |