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 | * Note: currently support string comparision |
||
| 154 | * |
||
| 155 | * @since 2.2.4 |
||
| 156 | * @access public |
||
| 157 | * |
||
| 158 | * @param array $column_args Array contains column key and expected value. |
||
| 159 | * |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | public function get_results_by( $column_args ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Retrieve a specific column's value by the primary key |
||
| 192 | * |
||
| 193 | * @since 1.0 |
||
| 194 | * @access public |
||
| 195 | * |
||
| 196 | * @param int $column Column ID. |
||
| 197 | * @param int $row_id Row ID. |
||
| 198 | * |
||
| 199 | * @return string Column value. |
||
| 200 | */ |
||
| 201 | View Code Duplication | public function get_column( $column, $row_id ) { |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Retrieve a specific column's value by the the specified column / value |
||
| 217 | * |
||
| 218 | * @since 1.0 |
||
| 219 | * @access public |
||
| 220 | * |
||
| 221 | * @param int $column Column ID. |
||
| 222 | * @param string $column_where Column name. |
||
| 223 | * @param string $column_value Column value. |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function get_column_by( $column, $column_where, $column_value ) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Insert a new row |
||
| 244 | * |
||
| 245 | * @since 1.0 |
||
| 246 | * @access public |
||
| 247 | * |
||
| 248 | * @param array $data |
||
| 249 | * @param string $type |
||
| 250 | * |
||
| 251 | * @return int |
||
| 252 | */ |
||
| 253 | public function insert( $data, $type = '' ) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Update a row |
||
| 299 | * |
||
| 300 | * @since 1.0 |
||
| 301 | * @access public |
||
| 302 | * |
||
| 303 | * @param int $row_id Column ID |
||
| 304 | * @param array $data |
||
| 305 | * @param string $where Column value |
||
| 306 | * |
||
| 307 | * @return bool |
||
| 308 | */ |
||
| 309 | public function update( $row_id, $data = array(), $where = '' ) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Delete a row identified by the primary key |
||
| 346 | * |
||
| 347 | * @since 1.0 |
||
| 348 | * @access public |
||
| 349 | * |
||
| 350 | * @param int $row_id Column ID. |
||
| 351 | * |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | public function delete( $row_id = 0 ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Check if the given table exists |
||
| 374 | * |
||
| 375 | * @since 1.3.2 |
||
| 376 | * @access public |
||
| 377 | * |
||
| 378 | * @param string $table The table name. |
||
| 379 | * |
||
| 380 | * @return bool If the table name exists. |
||
| 381 | */ |
||
| 382 | public function table_exists( $table ) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Checks whether column exists in a table or not. |
||
| 393 | * |
||
| 394 | * @param string $column_name Name of the Column in Database Table. |
||
| 395 | * |
||
| 396 | * @since 1.8.18 |
||
| 397 | * |
||
| 398 | * @see https://gist.github.com/datafeedr/54e89e07f87232fb055121bb766743fe |
||
| 399 | * |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | public function does_column_exist( $column_name ) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Check if the table was ever installed |
||
| 420 | * |
||
| 421 | * @since 1.6 |
||
| 422 | * @access public |
||
| 423 | * |
||
| 424 | * @return bool Returns if the customers table was installed and upgrade routine run. |
||
| 425 | */ |
||
| 426 | public function installed() { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Register tables |
||
| 432 | * |
||
| 433 | * @since 1.8.9 |
||
| 434 | * @access public |
||
| 435 | */ |
||
| 436 | public function register_table() { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Create table |
||
| 445 | * |
||
| 446 | * @since 1.8.9 |
||
| 447 | * @access public |
||
| 448 | */ |
||
| 449 | public function create_table() { |
||
| 451 | |||
| 452 | |||
| 453 | /** |
||
| 454 | * Given a ID, make sure it's a positive number, greater than zero before inserting or adding. |
||
| 455 | * |
||
| 456 | * @access private |
||
| 457 | * @since 2.0 |
||
| 458 | * |
||
| 459 | * @param int $id A passed ID. |
||
| 460 | * |
||
| 461 | * @return int|bool The normalized log ID or false if it's found to not be valid. |
||
| 462 | */ |
||
| 463 | public function sanitize_id( $id ) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Handle switch blog on multi-site |
||
| 485 | * |
||
| 486 | * @since 2.0.4 |
||
| 487 | * |
||
| 488 | * @access public |
||
| 489 | * |
||
| 490 | * @param $new_blog_id |
||
| 491 | * @param $prev_blog_id |
||
| 492 | */ |
||
| 493 | public function handle_switch_blog( $new_blog_id, $prev_blog_id ) { |
||
| 513 | } |
||
| 514 |
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.