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() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get cache key. |
||
| 86 | * |
||
| 87 | * @since 1.8.7 |
||
| 88 | * |
||
| 89 | * @param string $action Cache key prefix. |
||
| 90 | * @param array $query_args (optional) Query array. |
||
| 91 | * @param bool $is_prefix |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | public static function get_key( $action, $query_args = null, $is_prefix = true ) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get cache. |
||
| 119 | * |
||
| 120 | * @since 1.8.7 |
||
| 121 | * |
||
| 122 | * @param string $cache_key |
||
| 123 | * @param bool $custom_key |
||
| 124 | * @param mixed $query_args |
||
| 125 | * |
||
| 126 | * @return mixed |
||
| 127 | */ |
||
| 128 | public static function get( $cache_key, $custom_key = false, $query_args = array() ) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Set cache. |
||
| 158 | * |
||
| 159 | * @since 1.8.7 |
||
| 160 | * |
||
| 161 | * @param string $cache_key |
||
| 162 | * @param mixed $data |
||
| 163 | * @param int|null $expiration Timestamp should be in GMT format. |
||
| 164 | * @param bool $custom_key |
||
| 165 | * @param mixed $query_args |
||
| 166 | * |
||
| 167 | * @return mixed |
||
| 168 | */ |
||
| 169 | public static function set( $cache_key, $data, $expiration = null, $custom_key = false, $query_args = array() ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Delete cache. |
||
| 192 | * |
||
| 193 | * Note: only for internal use |
||
| 194 | * |
||
| 195 | * @since 1.8.7 |
||
| 196 | * |
||
| 197 | * @param string|array $cache_keys |
||
| 198 | * |
||
| 199 | * @return bool|WP_Error |
||
| 200 | */ |
||
| 201 | public static function delete( $cache_keys ) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Delete all logging cache. |
||
| 231 | * |
||
| 232 | * Note: only for internal use |
||
| 233 | * |
||
| 234 | * @since 1.8.7 |
||
| 235 | * @access public |
||
| 236 | * @global wpdb $wpdb |
||
| 237 | * |
||
| 238 | * @param bool $force If set to true then all cached values will be delete instead of only expired |
||
| 239 | * |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | public static function delete_all_expired( $force = false ) { |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * Get list of options like. |
||
| 286 | * |
||
| 287 | * Note: only for internal use |
||
| 288 | * |
||
| 289 | * @since 1.8.7 |
||
| 290 | * @access public |
||
| 291 | * |
||
| 292 | * @param string $option_name |
||
| 293 | * @param bool $fields |
||
| 294 | * |
||
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | public static function get_options_like( $option_name, $fields = false ) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Check cache key validity. |
||
| 338 | * |
||
| 339 | * @since 1.8.7 |
||
| 340 | * @access public |
||
| 341 | * |
||
| 342 | * @param $cache_key |
||
| 343 | * |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | public static function is_valid_cache_key( $cache_key ) { |
||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * Get cache from group |
||
| 361 | * |
||
| 362 | * @since 2.0 |
||
| 363 | * @access public |
||
| 364 | * |
||
| 365 | * @param int $id |
||
| 366 | * @param string $group |
||
| 367 | * |
||
| 368 | * @return mixed |
||
| 369 | */ |
||
| 370 | public static function get_group( $id, $group = '' ) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Cache small chunks inside group |
||
| 386 | * |
||
| 387 | * @since 2.0 |
||
| 388 | * @access public |
||
| 389 | * |
||
| 390 | * @param int $id |
||
| 391 | * @param mixed $data |
||
| 392 | * @param string $group |
||
| 393 | * @param int $expire |
||
| 394 | * |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | public static function set_group( $id, $data, $group = '', $expire = 0 ) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Cache small db query chunks inside group |
||
| 414 | * |
||
| 415 | * @since 2.0 |
||
| 416 | * @access public |
||
| 417 | * |
||
| 418 | * @param int $id |
||
| 419 | * @param mixed $data |
||
| 420 | * |
||
| 421 | * @return bool |
||
| 422 | */ |
||
| 423 | public static function set_db_query( $id, $data ) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get cache from group |
||
| 436 | * |
||
| 437 | * @since 2.0 |
||
| 438 | * @access public |
||
| 439 | * |
||
| 440 | * @param int $id |
||
| 441 | * |
||
| 442 | * @return mixed |
||
| 443 | */ |
||
| 444 | public static function get_db_query( $id ) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Delete group cache |
||
| 450 | * |
||
| 451 | * @since 2.0 |
||
| 452 | * @access public |
||
| 453 | * |
||
| 454 | * @param int|array $ids |
||
| 455 | * @param string $group |
||
| 456 | * @param int $expire |
||
| 457 | * |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | public static function delete_group( $ids, $group = '', $expire = 0 ) { |
||
| 506 | |||
| 507 | |||
| 508 | /** |
||
| 509 | * Delete form related cache |
||
| 510 | * Note: only use for internal purpose. |
||
| 511 | * |
||
| 512 | * @since 2.0 |
||
| 513 | * @access public |
||
| 514 | * |
||
| 515 | * @param int $form_id |
||
| 516 | */ |
||
| 517 | public function delete_form_related_cache( $form_id ) { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Delete payment related cache |
||
| 545 | * Note: only use for internal purpose. |
||
| 546 | * |
||
| 547 | * @since 2.0 |
||
| 548 | * @access public |
||
| 549 | * |
||
| 550 | * @param int $donation_id |
||
| 551 | */ |
||
| 552 | public function delete_payment_related_cache( $donation_id ) { |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Delete donor related cache |
||
| 572 | * Note: only use for internal purpose. |
||
| 573 | * |
||
| 574 | * @since 2.0 |
||
| 575 | * @access public |
||
| 576 | * |
||
| 577 | * @param string $id |
||
| 578 | * @param string $group |
||
| 579 | * @param int $expire |
||
| 580 | */ |
||
| 581 | public function delete_donor_related_cache( $id, $group, $expire ) { |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Delete donations related cache |
||
| 596 | * Note: only use for internal purpose. |
||
| 597 | * |
||
| 598 | * @since 2.0 |
||
| 599 | * @access public |
||
| 600 | * |
||
| 601 | * @param string $id |
||
| 602 | * @param string $group |
||
| 603 | * @param int $expire |
||
| 604 | */ |
||
| 605 | public function delete_donations_related_cache( $id, $group, $expire ) { |
||
| 615 | |||
| 616 | |||
| 617 | /** |
||
| 618 | * Get unique incrementer. |
||
| 619 | * |
||
| 620 | * @see https://core.trac.wordpress.org/ticket/4476 |
||
| 621 | * @see https://www.tollmanz.com/invalidation-schemes/ |
||
| 622 | * |
||
| 623 | * @since 2.0 |
||
| 624 | * @access private |
||
| 625 | * |
||
| 626 | * @param bool $refresh |
||
| 627 | * @param string $incrementer_key |
||
| 628 | * |
||
| 629 | * @return string |
||
| 630 | */ |
||
| 631 | private function get_incrementer( $refresh = false, $incrementer_key = 'give-cache-incrementer-db-queries' ) { |
||
| 641 | |||
| 642 | |||
| 643 | /** |
||
| 644 | * Flush cache on cache setting enable/disable |
||
| 645 | * Note: only for internal use |
||
| 646 | * |
||
| 647 | * @since 2.0 |
||
| 648 | * @access public |
||
| 649 | */ |
||
| 650 | public function flush_cache() { |
||
| 660 | |||
| 661 | |||
| 662 | /** |
||
| 663 | * Filter the group name |
||
| 664 | * |
||
| 665 | * @since 2.0 |
||
| 666 | * @access private |
||
| 667 | * |
||
| 668 | * @param $group |
||
| 669 | * |
||
| 670 | * @return mixed |
||
| 671 | */ |
||
| 672 | private function filter_group_name( $group ) { |
||
| 690 | |||
| 691 | |||
| 692 | /** |
||
| 693 | * Disable cache. |
||
| 694 | * |
||
| 695 | * @since 2.0 |
||
| 696 | * @access public |
||
| 697 | */ |
||
| 698 | public static function disable() { |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Enable cache. |
||
| 704 | * |
||
| 705 | * @since 2.0 |
||
| 706 | * @access public |
||
| 707 | */ |
||
| 708 | public static function enable() { |
||
| 711 | } |
||
| 712 | |||
| 715 |