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() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Prevent caching on certain pages |
||
| 89 | * |
||
| 90 | * @since 2.0.5 |
||
| 91 | * @access public |
||
| 92 | * @credit WooCommerce |
||
| 93 | */ |
||
| 94 | public static function prevent_caching() { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Set constants to prevent caching by some plugins. |
||
| 116 | * |
||
| 117 | * @since 2.0.5 |
||
| 118 | * @access public |
||
| 119 | * @credit WooCommerce |
||
| 120 | * |
||
| 121 | * @param mixed $return Value to return. Previously hooked into a filter. |
||
| 122 | * |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | public static function set_nocache_constants( $return = true ) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Notices function. |
||
| 135 | * |
||
| 136 | * @since 2.0.5 |
||
| 137 | * @access public |
||
| 138 | * @credit WooCommerce |
||
| 139 | */ |
||
| 140 | public function __notices() { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get cache key. |
||
| 160 | * |
||
| 161 | * @since 1.8.7 |
||
| 162 | * |
||
| 163 | * @param string $action Cache key prefix. |
||
| 164 | * @param array $query_args (optional) Query array. |
||
| 165 | * @param bool $is_prefix |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public static function get_key( $action, $query_args = null, $is_prefix = true ) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get cache. |
||
| 193 | * |
||
| 194 | * @since 1.8.7 |
||
| 195 | * |
||
| 196 | * @param string $cache_key |
||
| 197 | * @param bool $custom_key |
||
| 198 | * @param mixed $query_args |
||
| 199 | * |
||
| 200 | * @return mixed |
||
| 201 | */ |
||
| 202 | public static function get( $cache_key, $custom_key = false, $query_args = array() ) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Set cache. |
||
| 234 | * |
||
| 235 | * @since 1.8.7 |
||
| 236 | * |
||
| 237 | * @param string $cache_key |
||
| 238 | * @param mixed $data |
||
| 239 | * @param int|null $expiration Timestamp should be in GMT format. |
||
| 240 | * @param bool $custom_key |
||
| 241 | * @param mixed $query_args |
||
| 242 | * |
||
| 243 | * @return mixed |
||
| 244 | */ |
||
| 245 | public static function set( $cache_key, $data, $expiration = null, $custom_key = false, $query_args = array() ) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Delete cache. |
||
| 268 | * |
||
| 269 | * Note: only for internal use |
||
| 270 | * |
||
| 271 | * @since 1.8.7 |
||
| 272 | * |
||
| 273 | * @param string|array $cache_keys |
||
| 274 | * |
||
| 275 | * @return bool|WP_Error |
||
| 276 | */ |
||
| 277 | public static function delete( $cache_keys ) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Delete all logging cache. |
||
| 307 | * |
||
| 308 | * Note: only for internal use |
||
| 309 | * |
||
| 310 | * @since 1.8.7 |
||
| 311 | * @access public |
||
| 312 | * @global wpdb $wpdb |
||
| 313 | * |
||
| 314 | * @param bool $force If set to true then all cached values will be delete instead of only expired |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | public static function delete_all_expired( $force = false ) { |
||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * Get list of options like. |
||
| 362 | * |
||
| 363 | * Note: only for internal use |
||
| 364 | * |
||
| 365 | * @since 1.8.7 |
||
| 366 | * @access public |
||
| 367 | * |
||
| 368 | * @param string $option_name |
||
| 369 | * @param bool $fields |
||
| 370 | * |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | public static function get_options_like( $option_name, $fields = false ) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Check cache key validity. |
||
| 414 | * |
||
| 415 | * @since 1.8.7 |
||
| 416 | * @access public |
||
| 417 | * |
||
| 418 | * @param $cache_key |
||
| 419 | * |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | public static function is_valid_cache_key( $cache_key ) { |
||
| 433 | |||
| 434 | |||
| 435 | /** |
||
| 436 | * Get cache from group |
||
| 437 | * |
||
| 438 | * @since 2.0 |
||
| 439 | * @access public |
||
| 440 | * |
||
| 441 | * @param int $id |
||
| 442 | * @param string $group |
||
| 443 | * |
||
| 444 | * @return mixed |
||
| 445 | */ |
||
| 446 | public static function get_group( $id, $group = '' ) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Cache small chunks inside group |
||
| 462 | * |
||
| 463 | * @since 2.0 |
||
| 464 | * @access public |
||
| 465 | * |
||
| 466 | * @param int $id |
||
| 467 | * @param mixed $data |
||
| 468 | * @param string $group |
||
| 469 | * @param int $expire |
||
| 470 | * |
||
| 471 | * @return bool |
||
| 472 | */ |
||
| 473 | public static function set_group( $id, $data, $group = '', $expire = 0 ) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Cache small db query chunks inside group |
||
| 490 | * |
||
| 491 | * @since 2.0 |
||
| 492 | * @access public |
||
| 493 | * |
||
| 494 | * @param int $id |
||
| 495 | * @param mixed $data |
||
| 496 | * |
||
| 497 | * @return bool |
||
| 498 | */ |
||
| 499 | public static function set_db_query( $id, $data ) { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Get cache from group |
||
| 512 | * |
||
| 513 | * @since 2.0 |
||
| 514 | * @access public |
||
| 515 | * |
||
| 516 | * @param string $id |
||
| 517 | * |
||
| 518 | * @return mixed |
||
| 519 | */ |
||
| 520 | public static function get_db_query( $id ) { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Delete group cache |
||
| 526 | * |
||
| 527 | * @since 2.0 |
||
| 528 | * @access public |
||
| 529 | * |
||
| 530 | * @param int|array $ids |
||
| 531 | * @param string $group |
||
| 532 | * @param int $expire |
||
| 533 | * |
||
| 534 | * @return bool |
||
| 535 | */ |
||
| 536 | public static function delete_group( $ids, $group = '', $expire = 0 ) { |
||
| 583 | |||
| 584 | |||
| 585 | /** |
||
| 586 | * Delete form related cache |
||
| 587 | * Note: only use for internal purpose. |
||
| 588 | * |
||
| 589 | * @since 2.0 |
||
| 590 | * @access public |
||
| 591 | * |
||
| 592 | * @param int $form_id |
||
| 593 | */ |
||
| 594 | public function delete_form_related_cache( $form_id ) { |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Delete payment related cache |
||
| 624 | * Note: only use for internal purpose. |
||
| 625 | * |
||
| 626 | * @since 2.0 |
||
| 627 | * @access public |
||
| 628 | * |
||
| 629 | * @param int $donation_id |
||
| 630 | */ |
||
| 631 | public function delete_payment_related_cache( $donation_id ) { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Delete donor related cache |
||
| 648 | * Note: only use for internal purpose. |
||
| 649 | * |
||
| 650 | * @since 2.0 |
||
| 651 | * @access public |
||
| 652 | * |
||
| 653 | * @param string $id |
||
| 654 | * @param string $group |
||
| 655 | * @param int $expire |
||
| 656 | */ |
||
| 657 | public function delete_donor_related_cache( $id, $group, $expire ) { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Delete donations related cache |
||
| 673 | * Note: only use for internal purpose. |
||
| 674 | * |
||
| 675 | * @since 2.0 |
||
| 676 | * @access public |
||
| 677 | * |
||
| 678 | * @param string $id |
||
| 679 | * @param string $group |
||
| 680 | * @param int $expire |
||
| 681 | */ |
||
| 682 | public function delete_donations_related_cache( $id, $group, $expire ) { |
||
| 689 | |||
| 690 | |||
| 691 | /** |
||
| 692 | * Get unique incrementer. |
||
| 693 | * |
||
| 694 | * @see https://core.trac.wordpress.org/ticket/4476 |
||
| 695 | * @see https://www.tollmanz.com/invalidation-schemes/ |
||
| 696 | * |
||
| 697 | * @since 2.0 |
||
| 698 | * @access public |
||
| 699 | * |
||
| 700 | * @param bool $refresh |
||
| 701 | * @param string $incrementer_key |
||
| 702 | * |
||
| 703 | * @return string |
||
| 704 | */ |
||
| 705 | public function get_incrementer( $refresh = false, $incrementer_key = 'give-cache-incrementer-db-queries' ) { |
||
| 715 | |||
| 716 | |||
| 717 | /** |
||
| 718 | * Flush cache on cache setting enable/disable |
||
| 719 | * Note: only for internal use |
||
| 720 | * |
||
| 721 | * @since 2.0 |
||
| 722 | * @access public |
||
| 723 | * |
||
| 724 | * @param bool $force Delte cache forcefully. |
||
| 725 | * |
||
| 726 | * @return bool |
||
| 727 | */ |
||
| 728 | public static function flush_cache( $force = false ) { |
||
| 749 | |||
| 750 | |||
| 751 | /** |
||
| 752 | * Filter the group name |
||
| 753 | * |
||
| 754 | * @since 2.0 |
||
| 755 | * @access private |
||
| 756 | * |
||
| 757 | * @param $group |
||
| 758 | * |
||
| 759 | * @return mixed |
||
| 760 | */ |
||
| 761 | private function filter_group_name( $group ) { |
||
| 787 | |||
| 788 | |||
| 789 | /** |
||
| 790 | * Disable cache. |
||
| 791 | * |
||
| 792 | * @since 2.0 |
||
| 793 | * @access public |
||
| 794 | */ |
||
| 795 | public static function disable() { |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Enable cache. |
||
| 801 | * |
||
| 802 | * @since 2.0 |
||
| 803 | * @access public |
||
| 804 | */ |
||
| 805 | public static function enable() { |
||
| 808 | } |
||
| 809 | |||
| 812 |