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_Meta 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_Meta, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Give_DB_Meta extends Give_DB { |
||
| 18 | /** |
||
| 19 | * Post type |
||
| 20 | * |
||
| 21 | * @since 2.0 |
||
| 22 | * @access protected |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | protected $post_type = ''; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Meta type |
||
| 29 | * |
||
| 30 | * @since 2.0 |
||
| 31 | * @access protected |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | protected $meta_type = ''; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Flag to handle result type |
||
| 38 | * |
||
| 39 | * @since 2.0 |
||
| 40 | * @access protected |
||
| 41 | */ |
||
| 42 | protected $raw_result = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Flag for short circuit of meta function |
||
| 46 | * |
||
| 47 | * @since 2.0 |
||
| 48 | * @access protected |
||
| 49 | */ |
||
| 50 | protected $check = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Flag to check whether meta function called by WP filter or directly |
||
| 54 | * |
||
| 55 | * @since 2.0 |
||
| 56 | * @access protected |
||
| 57 | */ |
||
| 58 | private $is_filter_callback = false; |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * Meta supports. |
||
| 63 | * |
||
| 64 | * @since 2.0 |
||
| 65 | * @access protected |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $supports = array( |
||
| 69 | 'add_post_metadata', |
||
| 70 | 'get_post_metadata', |
||
| 71 | 'update_post_metadata', |
||
| 72 | 'delete_post_metadata', |
||
| 73 | 'posts_where', |
||
| 74 | 'posts_join', |
||
| 75 | 'posts_groupby', |
||
| 76 | 'posts_orderby', |
||
| 77 | ); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Give_DB_Meta constructor. |
||
| 81 | * |
||
| 82 | * @since 2.0 |
||
| 83 | */ |
||
| 84 | function __construct() { |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * Retrieve payment meta field for a payment. |
||
| 128 | * |
||
| 129 | * @access public |
||
| 130 | * @since 2.0 |
||
| 131 | * |
||
| 132 | * @param int $id Pst Type ID. |
||
| 133 | * @param string $meta_key The meta key to retrieve. |
||
| 134 | * @param bool $single Whether to return a single value. |
||
| 135 | * |
||
| 136 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single |
||
| 137 | * is true. |
||
| 138 | */ |
||
| 139 | public function get_meta( $id = 0, $meta_key = '', $single = false ) { |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * Add meta data field to a payment. |
||
| 171 | * |
||
| 172 | * For internal use only. Use Give_Payment->add_meta() for public usage. |
||
| 173 | * |
||
| 174 | * @access private |
||
| 175 | * @since 2.0 |
||
| 176 | * |
||
| 177 | * @param int $id Post Type ID. |
||
| 178 | * @param string $meta_key Metadata name. |
||
| 179 | * @param mixed $meta_value Metadata value. |
||
| 180 | * @param bool $unique Optional, default is false. Whether the same key should not be added. |
||
| 181 | * |
||
| 182 | * @return int|bool False for failure. True for success. |
||
| 183 | */ |
||
| 184 | public function add_meta( $id, $meta_key, $meta_value, $unique = false ) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Update payment meta field based on Post Type ID. |
||
| 207 | * |
||
| 208 | * For internal use only. Use Give_Payment->update_meta() for public usage. |
||
| 209 | * |
||
| 210 | * Use the $prev_value parameter to differentiate between meta fields with the |
||
| 211 | * same key and Post Type ID. |
||
| 212 | * |
||
| 213 | * If the meta field for the payment does not exist, it will be added. |
||
| 214 | * |
||
| 215 | * @access public |
||
| 216 | * @since 2.0 |
||
| 217 | * |
||
| 218 | * @param int $id Post Type ID. |
||
| 219 | * @param string $meta_key Metadata key. |
||
| 220 | * @param mixed $meta_value Metadata value. |
||
| 221 | * @param mixed $prev_value Optional. Previous value to check before removing. |
||
| 222 | * |
||
| 223 | * @return int|bool False on failure, true if success. |
||
| 224 | */ |
||
| 225 | View Code Duplication | public function update_meta( $id, $meta_key, $meta_value, $prev_value = '' ) { |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Remove metadata matching criteria from a payment. |
||
| 248 | * |
||
| 249 | * You can match based on the key, or key and value. Removing based on key and |
||
| 250 | * value, will keep from removing duplicate metadata with the same key. It also |
||
| 251 | * allows removing all metadata matching key, if needed. |
||
| 252 | * |
||
| 253 | * @access public |
||
| 254 | * @since 2.0 |
||
| 255 | * |
||
| 256 | * @param int $id Post Type ID. |
||
| 257 | * @param string $meta_key Metadata name. |
||
| 258 | * @param mixed $meta_value Optional. Metadata value. |
||
| 259 | * @param mixed $delete_all Optional. |
||
| 260 | * |
||
| 261 | * @return bool False for failure. True for success. |
||
| 262 | */ |
||
| 263 | View Code Duplication | public function delete_meta( $id = 0, $meta_key = '', $meta_value = '', $delete_all = '' ) { |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Rename query clauses of every query for new meta table |
||
| 287 | * |
||
| 288 | * @since 2.0 |
||
| 289 | * @access public |
||
| 290 | * |
||
| 291 | * @param string $clause |
||
| 292 | * @param WP_Query $wp_query |
||
| 293 | * |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public function __rename_meta_table_name_in_query( $clause, $wp_query ) { |
||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * Rename query clauses for new meta table |
||
| 308 | * |
||
| 309 | * @param $clause |
||
| 310 | * @param $filter |
||
| 311 | * |
||
| 312 | * @return mixed |
||
| 313 | */ |
||
| 314 | public function __rename_meta_table_name( $clause, $filter ) { |
||
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * Check if current query for post type or not. |
||
| 359 | * |
||
| 360 | * @since 2.0 |
||
| 361 | * @access protected |
||
| 362 | * |
||
| 363 | * @param WP_Query $wp_query |
||
| 364 | * |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | protected function is_post_type_query( $wp_query ) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Check if current id of post type or not |
||
| 391 | * |
||
| 392 | * @since 2.0 |
||
| 393 | * @access protected |
||
| 394 | * |
||
| 395 | * @param $ID |
||
| 396 | * |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | protected function is_valid_post_type( $ID ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * check if custom meta table enabled or not. |
||
| 405 | * |
||
| 406 | * @since 2.0 |
||
| 407 | * @access protected |
||
| 408 | * @return bool |
||
| 409 | */ |
||
| 410 | protected function is_custom_meta_table_active() { |
||
| 413 | |||
| 414 | |||
| 415 | /** |
||
| 416 | * Update last_changed key |
||
| 417 | * |
||
| 418 | * @since 2.0 |
||
| 419 | * @access private |
||
| 420 | * |
||
| 421 | * @param int $id |
||
| 422 | * @param string $meta_type |
||
| 423 | * |
||
| 424 | * @return void |
||
| 425 | */ |
||
| 426 | private function delete_cache( $id, $meta_type = '' ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Add support for hidden functions. |
||
| 444 | * |
||
| 445 | * @since 2.0 |
||
| 446 | * @access public |
||
| 447 | * |
||
| 448 | * @param $name |
||
| 449 | * @param $arguments |
||
| 450 | * |
||
| 451 | * @return mixed |
||
| 452 | */ |
||
| 453 | public function __call( $name, $arguments ) { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Create Meta Tables. |
||
| 519 | * |
||
| 520 | * @since 2.0.1 |
||
| 521 | * @access public |
||
| 522 | */ |
||
| 523 | public function create_table() { |
||
| 543 | |||
| 544 | |||
| 545 | /** |
||
| 546 | * Get meta type |
||
| 547 | * |
||
| 548 | * @since 2.0.4 |
||
| 549 | * @access public |
||
| 550 | * |
||
| 551 | * @return string |
||
| 552 | */ |
||
| 553 | public function get_meta_type() { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Remove all meta data matching criteria from a meta table. |
||
| 559 | * |
||
| 560 | * @since 2.1.3 |
||
| 561 | * @access public |
||
| 562 | * |
||
| 563 | * @param int $id ID. |
||
| 564 | * |
||
| 565 | * @return bool False for failure. True for success. |
||
| 566 | */ |
||
| 567 | public function delete_all_meta( $id = 0 ) { |
||
| 577 | } |
||
| 578 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.