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_Cache 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_Cache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Give_Cache { |
||
|
|
|||
| 19 | /** |
||
| 20 | * Instance. |
||
| 21 | * |
||
| 22 | * @since 1.8.7 |
||
| 23 | * @access private |
||
| 24 | * @var Give_Cache |
||
| 25 | */ |
||
| 26 | static private $instance; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Flag to check if caching enabled or not. |
||
| 30 | * |
||
| 31 | * @since 2.0 |
||
| 32 | * @access private |
||
| 33 | * @var |
||
| 34 | */ |
||
| 35 | private $is_cache; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Singleton pattern. |
||
| 39 | * |
||
| 40 | * @since 1.8.7 |
||
| 41 | * @access private |
||
| 42 | * Give_Cache constructor. |
||
| 43 | */ |
||
| 44 | private function __construct() { |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Get instance. |
||
| 50 | * |
||
| 51 | * @since 1.8.7 |
||
| 52 | * @access public |
||
| 53 | * @return static |
||
| 54 | */ |
||
| 55 | public static function get_instance() { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Setup hooks. |
||
| 65 | * |
||
| 66 | * @since 1.8.7 |
||
| 67 | * @access public |
||
| 68 | */ |
||
| 69 | public function setup() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get cache key. |
||
| 83 | * |
||
| 84 | * @since 1.8.7 |
||
| 85 | * |
||
| 86 | * @param string $action Cache key prefix. |
||
| 87 | * @param array $query_args (optional) Query array. |
||
| 88 | * |
||
| 89 | * @return string|WP_Error |
||
| 90 | */ |
||
| 91 | |||
| 92 | public static function get_key( $action, $query_args = null ) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get cache. |
||
| 130 | * |
||
| 131 | * @since 1.8.7 |
||
| 132 | * |
||
| 133 | * @param string $cache_key |
||
| 134 | * @param bool $custom_key |
||
| 135 | * @param mixed $query_args |
||
| 136 | * |
||
| 137 | * @return mixed |
||
| 138 | */ |
||
| 139 | |||
| 140 | public static function get( $cache_key, $custom_key = false, $query_args = array() ) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Set cache. |
||
| 170 | * |
||
| 171 | * @since 1.8.7 |
||
| 172 | * |
||
| 173 | * @param string $cache_key |
||
| 174 | * @param mixed $data |
||
| 175 | * @param int|null $expiration Timestamp should be in GMT format. |
||
| 176 | * @param bool $custom_key |
||
| 177 | * @param mixed $query_args |
||
| 178 | * |
||
| 179 | * @return mixed |
||
| 180 | */ |
||
| 181 | |||
| 182 | public static function set( $cache_key, $data, $expiration = null, $custom_key = false, $query_args = array() ) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Delete cache. |
||
| 205 | * |
||
| 206 | * Note: only for internal use |
||
| 207 | * |
||
| 208 | * @since 1.8.7 |
||
| 209 | * |
||
| 210 | * @param string|array $cache_keys |
||
| 211 | * |
||
| 212 | * @return bool|WP_Error |
||
| 213 | */ |
||
| 214 | |||
| 215 | public static function delete( $cache_keys ) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Delete all logging cache. |
||
| 245 | * |
||
| 246 | * Note: only for internal use |
||
| 247 | * |
||
| 248 | * @since 1.8.7 |
||
| 249 | * @access public |
||
| 250 | * @global wpdb $wpdb |
||
| 251 | * |
||
| 252 | * @param bool $force If set to true then all cached values will be delete instead of only expired |
||
| 253 | * |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | public static function delete_all_expired( $force = false ) { |
||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * Get list of options like. |
||
| 300 | * |
||
| 301 | * Note: only for internal use |
||
| 302 | * |
||
| 303 | * @since 1.8.7 |
||
| 304 | * @access public |
||
| 305 | * |
||
| 306 | * @param string $option_name |
||
| 307 | * @param bool $fields |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | public static function get_options_like( $option_name, $fields = false ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Check cache key validity. |
||
| 356 | * |
||
| 357 | * @since 1.8.7 |
||
| 358 | * @access public |
||
| 359 | * |
||
| 360 | * @param $cache_key |
||
| 361 | * |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | public static function is_valid_cache_key( $cache_key ) { |
||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * Get cache from group |
||
| 379 | * |
||
| 380 | * @since 2.0 |
||
| 381 | * @access public |
||
| 382 | * |
||
| 383 | * @param int $id |
||
| 384 | * @param string $group |
||
| 385 | * |
||
| 386 | * @return mixed |
||
| 387 | */ |
||
| 388 | public static function get_group( $id, $group = '' ) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Cache small chunks inside group |
||
| 403 | * |
||
| 404 | * @since 2.0 |
||
| 405 | * @access public |
||
| 406 | * |
||
| 407 | * @param int $id |
||
| 408 | * @param mixed $data |
||
| 409 | * @param string $group |
||
| 410 | * @param int $expire |
||
| 411 | * |
||
| 412 | * @return bool |
||
| 413 | */ |
||
| 414 | public static function set_group( $id, $data, $group = '', $expire = 0 ) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Delete group cache |
||
| 431 | * |
||
| 432 | * @since 2.0 |
||
| 433 | * @access public |
||
| 434 | * |
||
| 435 | * @param int|array $ids |
||
| 436 | * @param string $group |
||
| 437 | * @param int $expire |
||
| 438 | * |
||
| 439 | * @return bool |
||
| 440 | */ |
||
| 441 | public static function delete_group( $ids, $group = '', $expire = 0 ) { |
||
| 485 | |||
| 486 | |||
| 487 | /** |
||
| 488 | * Delete form related cache |
||
| 489 | * Note: only use for internal purpose. |
||
| 490 | * |
||
| 491 | * @since 2.0 |
||
| 492 | * @access public |
||
| 493 | * |
||
| 494 | * @param int $form_id |
||
| 495 | */ |
||
| 496 | public function delete_form_related_cache( $form_id ) { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Delete payment related cache |
||
| 525 | * Note: only use for internal purpose. |
||
| 526 | * |
||
| 527 | * @since 2.0 |
||
| 528 | * @access public |
||
| 529 | * |
||
| 530 | * @param int $donation_id |
||
| 531 | */ |
||
| 532 | public function delete_payment_related_cache( $donation_id ) { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Delete donor related cache |
||
| 550 | * Note: only use for internal purpose. |
||
| 551 | * |
||
| 552 | * @since 2.0 |
||
| 553 | * @access public |
||
| 554 | * |
||
| 555 | * @param string $id |
||
| 556 | * @param string $group |
||
| 557 | * @param int $expire |
||
| 558 | */ |
||
| 559 | public function delete_donor_related_cache( $id, $group, $expire ) { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Delete donations related cache |
||
| 575 | * Note: only use for internal purpose. |
||
| 576 | * |
||
| 577 | * @since 2.0 |
||
| 578 | * @access public |
||
| 579 | * |
||
| 580 | * @param string $id |
||
| 581 | * @param string $group |
||
| 582 | * @param int $expire |
||
| 583 | */ |
||
| 584 | public function delete_donations_related_cache( $id, $group, $expire ) { |
||
| 592 | |||
| 593 | |||
| 594 | /** |
||
| 595 | * Get unique timestamp. |
||
| 596 | * |
||
| 597 | * @since 2.0 |
||
| 598 | * @access private |
||
| 599 | * |
||
| 600 | * @param $context |
||
| 601 | * |
||
| 602 | * @return string |
||
| 603 | */ |
||
| 604 | private function get_unique_timestamp( $context ) { |
||
| 618 | |||
| 619 | |||
| 620 | /** |
||
| 621 | * Update cache version |
||
| 622 | * |
||
| 623 | * @since 2.0 |
||
| 624 | * @access private |
||
| 625 | * |
||
| 626 | * @param string $group |
||
| 627 | */ |
||
| 628 | private function update_cache_version( $group ) { |
||
| 644 | } |
||
| 645 | |||
| 648 |