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 ) { |
||
602 | |||
603 | /** |
||
604 | * Delete payment related cache |
||
605 | * Note: only use for internal purpose. |
||
606 | * |
||
607 | * @since 2.0 |
||
608 | * @access public |
||
609 | * |
||
610 | * @param int $donation_id |
||
611 | */ |
||
612 | public function delete_payment_related_cache( $donation_id ) { |
||
626 | |||
627 | /** |
||
628 | * Delete donor related cache |
||
629 | * Note: only use for internal purpose. |
||
630 | * |
||
631 | * @since 2.0 |
||
632 | * @access public |
||
633 | * |
||
634 | * @param string $id |
||
635 | * @param string $group |
||
636 | * @param int $expire |
||
637 | */ |
||
638 | public function delete_donor_related_cache( $id, $group, $expire ) { |
||
651 | |||
652 | /** |
||
653 | * Delete donations related cache |
||
654 | * Note: only use for internal purpose. |
||
655 | * |
||
656 | * @since 2.0 |
||
657 | * @access public |
||
658 | * |
||
659 | * @param string $id |
||
660 | * @param string $group |
||
661 | * @param int $expire |
||
662 | */ |
||
663 | public function delete_donations_related_cache( $id, $group, $expire ) { |
||
670 | |||
671 | |||
672 | /** |
||
673 | * Get unique incrementer. |
||
674 | * |
||
675 | * @see https://core.trac.wordpress.org/ticket/4476 |
||
676 | * @see https://www.tollmanz.com/invalidation-schemes/ |
||
677 | * |
||
678 | * @since 2.0 |
||
679 | * @access public |
||
680 | * |
||
681 | * @param bool $refresh |
||
682 | * @param string $incrementer_key |
||
683 | * |
||
684 | * @return string |
||
685 | */ |
||
686 | public function get_incrementer( $refresh = false, $incrementer_key = 'give-cache-incrementer-db-queries' ) { |
||
696 | |||
697 | |||
698 | /** |
||
699 | * Flush cache on cache setting enable/disable |
||
700 | * Note: only for internal use |
||
701 | * |
||
702 | * @since 2.0 |
||
703 | * @access public |
||
704 | * |
||
705 | * @param bool $force Delete cache forcefully. |
||
706 | * |
||
707 | * @return bool |
||
708 | */ |
||
709 | public static function flush_cache( $force = false ) { |
||
730 | |||
731 | |||
732 | /** |
||
733 | * Filter the group name |
||
734 | * |
||
735 | * @since 2.0 |
||
736 | * @access private |
||
737 | * |
||
738 | * @param $group |
||
739 | * |
||
740 | * @return mixed |
||
741 | */ |
||
742 | private function filter_group_name( $group ) { |
||
768 | |||
769 | |||
770 | /** |
||
771 | * Disable cache. |
||
772 | * |
||
773 | * @since 2.0 |
||
774 | * @access public |
||
775 | */ |
||
776 | public static function disable() { |
||
779 | |||
780 | /** |
||
781 | * Enable cache. |
||
782 | * |
||
783 | * @since 2.0 |
||
784 | * @access public |
||
785 | */ |
||
786 | public static function enable() { |
||
789 | } |
||
790 | |||
793 |