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 | ); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Give_DB_Meta constructor. |
||
| 71 | * |
||
| 72 | * @since 2.0 |
||
| 73 | */ |
||
| 74 | function __construct() { |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Retrieve payment meta field for a payment. |
||
| 108 | * |
||
| 109 | * @access public |
||
| 110 | * @since 2.0 |
||
| 111 | * |
||
| 112 | * @param int $id Pst Type ID. |
||
| 113 | * @param string $meta_key The meta key to retrieve. |
||
| 114 | * @param bool $single Whether to return a single value. |
||
| 115 | * |
||
| 116 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. |
||
| 117 | */ |
||
| 118 | public function get_meta( $id = 0, $meta_key = '', $single = false ) { |
||
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * Add meta data field to a payment. |
||
| 144 | * |
||
| 145 | * For internal use only. Use Give_Payment->add_meta() for public usage. |
||
| 146 | * |
||
| 147 | * @access private |
||
| 148 | * @since 2.0 |
||
| 149 | * |
||
| 150 | * @param int $id Post Type ID. |
||
| 151 | * @param string $meta_key Metadata name. |
||
| 152 | * @param mixed $meta_value Metadata value. |
||
| 153 | * @param bool $unique Optional, default is false. Whether the same key should not be added. |
||
| 154 | * |
||
| 155 | * @return bool False for failure. True for success. |
||
| 156 | */ |
||
| 157 | View Code Duplication | public function add_meta( $id = 0, $meta_key = '', $meta_value, $unique = false ) { |
|
| 158 | $id = $this->sanitize_id( $id ); |
||
| 159 | |||
| 160 | // Bailout. |
||
| 161 | if ( ! $this->is_valid_post_type( $id ) ) { |
||
| 162 | return $this->check; |
||
| 163 | } |
||
| 164 | |||
| 165 | return add_metadata( $this->meta_type, $id, $meta_key, $meta_value, $unique ); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Update payment meta field based on Post Type ID. |
||
| 170 | * |
||
| 171 | * For internal use only. Use Give_Payment->update_meta() for public usage. |
||
| 172 | * |
||
| 173 | * Use the $prev_value parameter to differentiate between meta fields with the |
||
| 174 | * same key and Post Type ID. |
||
| 175 | * |
||
| 176 | * If the meta field for the payment does not exist, it will be added. |
||
| 177 | * |
||
| 178 | * @access public |
||
| 179 | * @since 2.0 |
||
| 180 | * |
||
| 181 | * @param int $id Post Type ID. |
||
| 182 | * @param string $meta_key Metadata key. |
||
| 183 | * @param mixed $meta_value Metadata value. |
||
| 184 | * @param mixed $prev_value Optional. Previous value to check before removing. |
||
| 185 | * |
||
| 186 | * @return bool False on failure, true if success. |
||
| 187 | */ |
||
| 188 | View Code Duplication | public function update_meta( $id = 0, $meta_key = '', $meta_value, $prev_value = '' ) { |
|
| 189 | $id = $this->sanitize_id( $id ); |
||
| 190 | |||
| 191 | // Bailout. |
||
| 192 | if ( ! $this->is_valid_post_type( $id ) ) { |
||
| 193 | return $this->check; |
||
| 194 | } |
||
| 195 | |||
| 196 | return update_metadata( $this->meta_type, $id, $meta_key, $meta_value, $prev_value ); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Remove metadata matching criteria from a payment. |
||
| 201 | * |
||
| 202 | * You can match based on the key, or key and value. Removing based on key and |
||
| 203 | * value, will keep from removing duplicate metadata with the same key. It also |
||
| 204 | * allows removing all metadata matching key, if needed. |
||
| 205 | * |
||
| 206 | * @access public |
||
| 207 | * @since 2.0 |
||
| 208 | * |
||
| 209 | * @param int $id Post Type ID. |
||
| 210 | * @param string $meta_key Metadata name. |
||
| 211 | * @param mixed $meta_value Optional. Metadata value. |
||
| 212 | * @param mixed $delete_all Optional. |
||
| 213 | * |
||
| 214 | * @return bool False for failure. True for success. |
||
| 215 | */ |
||
| 216 | View Code Duplication | public function delete_meta( $id = 0, $meta_key = '', $meta_value = '', $delete_all = '' ) { |
|
| 217 | $id = $this->sanitize_id( $id ); |
||
| 218 | |||
| 219 | // Bailout. |
||
| 220 | if ( ! $this->is_valid_post_type( $id ) ) { |
||
| 221 | return $this->check; |
||
| 222 | } |
||
| 223 | |||
| 224 | return delete_metadata( $this->meta_type, $id, $meta_key, $meta_value, $delete_all ); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Filter where clause of every query for new payment meta table |
||
| 229 | * |
||
| 230 | * @since 2.0 |
||
| 231 | * @access public |
||
| 232 | * |
||
| 233 | * @param string $where |
||
| 234 | * @param WP_Query $wp_query |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public function __posts_where( $where, $wp_query ) { |
||
| 239 | global $wpdb; |
||
| 240 | |||
| 241 | $is_payment_post_type = false; |
||
| 242 | |||
| 243 | // Check if it is payment query. |
||
| 244 | View Code Duplication | if ( ! empty( $wp_query->query['post_type'] ) ) { |
|
| 245 | if ( is_string( $wp_query->query['post_type'] ) && $this->post_type === $wp_query->query['post_type'] ) { |
||
| 246 | $is_payment_post_type = true; |
||
| 247 | } elseif ( is_array( $wp_query->query['post_type'] ) && in_array( $this->post_type, $wp_query->query['post_type'] ) ) { |
||
| 248 | $is_payment_post_type = true; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | // Add new table to sql query. |
||
| 253 | if ( $is_payment_post_type && ! empty( $wp_query->meta_query->queries ) ) { |
||
| 254 | $where = str_replace( $wpdb->postmeta, $this->table_name, $where ); |
||
| 255 | } |
||
| 256 | |||
| 257 | return $where; |
||
| 258 | } |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Filter join clause of every query for new payment meta table |
||
| 263 | * |
||
| 264 | * @since 2.0 |
||
| 265 | * @access public |
||
| 266 | * |
||
| 267 | * @param string $join |
||
| 268 | * @param WP_Query $wp_query |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | public function __posts_join( $join, $wp_query ) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Add support for hidden functions. |
||
| 297 | * |
||
| 298 | * @since 2.0 |
||
| 299 | * @access public |
||
| 300 | * |
||
| 301 | * @param $name |
||
| 302 | * @param $arguments |
||
| 303 | * |
||
| 304 | * @return mixed |
||
| 305 | */ |
||
| 306 | public function __call( $name, $arguments ) { |
||
| 307 | switch ( $name ) { |
||
| 308 | View Code Duplication | case '__add_meta': |
|
| 309 | $this->check = $arguments[0]; |
||
| 310 | $id = $arguments[1]; |
||
| 311 | $meta_key = $arguments[2]; |
||
| 312 | $meta_value = $arguments[3]; |
||
| 313 | $unique = $arguments[4]; |
||
| 314 | |||
| 315 | return $this->add_meta( $id, $meta_key, $meta_value, $unique ); |
||
| 316 | |||
| 317 | case '__get_meta': |
||
| 318 | $this->check = $arguments[0]; |
||
| 319 | $id = $arguments[1]; |
||
| 320 | $meta_key = $arguments[2]; |
||
| 321 | $single = $arguments[3]; |
||
| 322 | |||
| 323 | $this->raw_result = true; |
||
| 324 | |||
| 325 | return $this->get_meta( $id, $meta_key, $single ); |
||
| 326 | |||
| 327 | case '__update_meta': |
||
| 328 | $this->check = $arguments[0]; |
||
| 329 | $id = $arguments[1]; |
||
| 330 | $meta_key = $arguments[2]; |
||
| 331 | $meta_value = $arguments[3]; |
||
| 332 | |||
| 333 | return $this->update_meta( $id, $meta_key, $meta_value ); |
||
| 334 | |||
| 335 | View Code Duplication | case '__delete_meta': |
|
| 336 | $this->check = $arguments[0]; |
||
| 337 | $id = $arguments[1]; |
||
| 338 | $meta_key = $arguments[2]; |
||
| 339 | $meta_value = $arguments[3]; |
||
| 340 | $delete_all = $arguments[3]; |
||
| 341 | |||
| 342 | return $this->delete_meta( $id, $meta_key, $meta_value, $delete_all ); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Check if current id of post type or not |
||
| 348 | * |
||
| 349 | * @since 2.0 |
||
| 350 | * @access protected |
||
| 351 | * |
||
| 352 | * @param $ID |
||
| 353 | * |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | protected function is_valid_post_type( $ID ) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * check if custom meta table enabled or not. |
||
| 362 | * |
||
| 363 | * @since 2.0 |
||
| 364 | * @access protected |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | protected function is_custom_meta_table_active() { |
||
| 370 | } |
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.