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 | /** |
||
| 54 | * Meta supports. |
||
| 55 | * |
||
| 56 | * @since 2.0 |
||
| 57 | * @access protected |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $supports = array( |
||
| 61 | 'add_post_metadata', |
||
| 62 | 'get_post_metadata', |
||
| 63 | 'update_post_metadata', |
||
| 64 | 'delete_post_metadata', |
||
| 65 | 'posts_where', |
||
| 66 | 'posts_join', |
||
| 67 | 'posts_groupby', |
||
| 68 | 'posts_orderby' |
||
|
|
|||
| 69 | ); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Give_DB_Meta constructor. |
||
| 73 | * |
||
| 74 | * @since 2.0 |
||
| 75 | */ |
||
| 76 | function __construct() { |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * Retrieve payment meta field for a payment. |
||
| 118 | * |
||
| 119 | * @access public |
||
| 120 | * @since 2.0 |
||
| 121 | * |
||
| 122 | * @param int $id Pst Type ID. |
||
| 123 | * @param string $meta_key The meta key to retrieve. |
||
| 124 | * @param bool $single Whether to return a single value. |
||
| 125 | * |
||
| 126 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single |
||
| 127 | * is true. |
||
| 128 | */ |
||
| 129 | public function get_meta( $id = 0, $meta_key = '', $single = false ) { |
||
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * Add meta data field to a payment. |
||
| 155 | * |
||
| 156 | * For internal use only. Use Give_Payment->add_meta() for public usage. |
||
| 157 | * |
||
| 158 | * @access private |
||
| 159 | * @since 2.0 |
||
| 160 | * |
||
| 161 | * @param int $id Post Type ID. |
||
| 162 | * @param string $meta_key Metadata name. |
||
| 163 | * @param mixed $meta_value Metadata value. |
||
| 164 | * @param bool $unique Optional, default is false. Whether the same key should not be added. |
||
| 165 | * |
||
| 166 | * @return int|bool False for failure. True for success. |
||
| 167 | */ |
||
| 168 | View Code Duplication | public function add_meta( $id = 0, $meta_key = '', $meta_value, $unique = false ) { |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Update payment meta field based on Post Type ID. |
||
| 187 | * |
||
| 188 | * For internal use only. Use Give_Payment->update_meta() for public usage. |
||
| 189 | * |
||
| 190 | * Use the $prev_value parameter to differentiate between meta fields with the |
||
| 191 | * same key and Post Type ID. |
||
| 192 | * |
||
| 193 | * If the meta field for the payment does not exist, it will be added. |
||
| 194 | * |
||
| 195 | * @access public |
||
| 196 | * @since 2.0 |
||
| 197 | * |
||
| 198 | * @param int $id Post Type ID. |
||
| 199 | * @param string $meta_key Metadata key. |
||
| 200 | * @param mixed $meta_value Metadata value. |
||
| 201 | * @param mixed $prev_value Optional. Previous value to check before removing. |
||
| 202 | * |
||
| 203 | * @return int|bool False on failure, true if success. |
||
| 204 | */ |
||
| 205 | View Code Duplication | public function update_meta( $id = 0, $meta_key = '', $meta_value, $prev_value = '' ) { |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Remove metadata matching criteria from a payment. |
||
| 224 | * |
||
| 225 | * You can match based on the key, or key and value. Removing based on key and |
||
| 226 | * value, will keep from removing duplicate metadata with the same key. It also |
||
| 227 | * allows removing all metadata matching key, if needed. |
||
| 228 | * |
||
| 229 | * @access public |
||
| 230 | * @since 2.0 |
||
| 231 | * |
||
| 232 | * @param int $id Post Type ID. |
||
| 233 | * @param string $meta_key Metadata name. |
||
| 234 | * @param mixed $meta_value Optional. Metadata value. |
||
| 235 | * @param mixed $delete_all Optional. |
||
| 236 | * |
||
| 237 | * @return bool False for failure. True for success. |
||
| 238 | */ |
||
| 239 | View Code Duplication | public function delete_meta( $id = 0, $meta_key = '', $meta_value = '', $delete_all = '' ) { |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Rename query clauses of every query for new meta table |
||
| 258 | * |
||
| 259 | * @since 2.0 |
||
| 260 | * @access public |
||
| 261 | * |
||
| 262 | * @param string $clause |
||
| 263 | * @param WP_Query $wp_query |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function __rename_meta_table_name_in_query( $clause, $wp_query ) { |
||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * Check if current query for post type or not. |
||
| 312 | * |
||
| 313 | * @since 2.0 |
||
| 314 | * @access protected |
||
| 315 | * |
||
| 316 | * @param WP_Query $wp_query |
||
| 317 | * |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | protected function is_post_type_query( $wp_query ) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Check if current id of post type or not |
||
| 343 | * |
||
| 344 | * @since 2.0 |
||
| 345 | * @access protected |
||
| 346 | * |
||
| 347 | * @param $ID |
||
| 348 | * |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | protected function is_valid_post_type( $ID ) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * check if custom meta table enabled or not. |
||
| 357 | * |
||
| 358 | * @since 2.0 |
||
| 359 | * @access protected |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | protected function is_custom_meta_table_active() { |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * Update last_changed key |
||
| 369 | * |
||
| 370 | * @since 2.0 |
||
| 371 | * @access private |
||
| 372 | * |
||
| 373 | * @param int $id |
||
| 374 | * @param string $meta_type |
||
| 375 | * |
||
| 376 | * @return void |
||
| 377 | */ |
||
| 378 | private function delete_cache( $id, $meta_type = '' ) { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Add support for hidden functions. |
||
| 396 | * |
||
| 397 | * @since 2.0 |
||
| 398 | * @access public |
||
| 399 | * |
||
| 400 | * @param $name |
||
| 401 | * @param $arguments |
||
| 402 | * |
||
| 403 | * @return mixed |
||
| 404 | */ |
||
| 405 | public function __call( $name, $arguments ) { |
||
| 464 | } |