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() { |
||
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() ) { |
||
229 | |||
230 | /** |
||
231 | * Set cache. |
||
232 | * |
||
233 | * @since 1.8.7 |
||
234 | * |
||
235 | * @param string $cache_key |
||
236 | * @param mixed $data |
||
237 | * @param int|null $expiration Timestamp should be in GMT format. |
||
238 | * @param bool $custom_key |
||
239 | * @param mixed $query_args |
||
240 | * |
||
241 | * @return mixed |
||
242 | */ |
||
243 | public static function set( $cache_key, $data, $expiration = null, $custom_key = false, $query_args = array() ) { |
||
263 | |||
264 | /** |
||
265 | * Delete cache. |
||
266 | * |
||
267 | * Note: only for internal use |
||
268 | * |
||
269 | * @since 1.8.7 |
||
270 | * |
||
271 | * @param string|array $cache_keys |
||
272 | * |
||
273 | * @return bool|WP_Error |
||
274 | */ |
||
275 | public static function delete( $cache_keys ) { |
||
302 | |||
303 | /** |
||
304 | * Delete all logging cache. |
||
305 | * |
||
306 | * Note: only for internal use |
||
307 | * |
||
308 | * @since 1.8.7 |
||
309 | * @access public |
||
310 | * @global wpdb $wpdb |
||
311 | * |
||
312 | * @param bool $force If set to true then all cached values will be delete instead of only expired |
||
313 | * |
||
314 | * @return bool |
||
315 | */ |
||
316 | public static function delete_all_expired( $force = false ) { |
||
356 | |||
357 | |||
358 | /** |
||
359 | * Get list of options like. |
||
360 | * |
||
361 | * Note: only for internal use |
||
362 | * |
||
363 | * @since 1.8.7 |
||
364 | * @access public |
||
365 | * |
||
366 | * @param string $option_name |
||
367 | * @param bool $fields |
||
368 | * |
||
369 | * @return array |
||
370 | */ |
||
371 | public static function get_options_like( $option_name, $fields = false ) { |
||
409 | |||
410 | /** |
||
411 | * Check cache key validity. |
||
412 | * |
||
413 | * @since 1.8.7 |
||
414 | * @access public |
||
415 | * |
||
416 | * @param $cache_key |
||
417 | * |
||
418 | * @return bool |
||
419 | */ |
||
420 | public static function is_valid_cache_key( $cache_key ) { |
||
431 | |||
432 | |||
433 | /** |
||
434 | * Get cache from group |
||
435 | * |
||
436 | * @since 2.0 |
||
437 | * @access public |
||
438 | * |
||
439 | * @param int $id |
||
440 | * @param string $group |
||
441 | * |
||
442 | * @return mixed |
||
443 | */ |
||
444 | public static function get_group( $id, $group = '' ) { |
||
457 | |||
458 | /** |
||
459 | * Cache small chunks inside group |
||
460 | * |
||
461 | * @since 2.0 |
||
462 | * @access public |
||
463 | * |
||
464 | * @param int $id |
||
465 | * @param mixed $data |
||
466 | * @param string $group |
||
467 | * @param int $expire |
||
468 | * |
||
469 | * @return bool |
||
470 | */ |
||
471 | public static function set_group( $id, $data, $group = '', $expire = 0 ) { |
||
485 | |||
486 | /** |
||
487 | * Cache small db query chunks inside group |
||
488 | * |
||
489 | * @since 2.0 |
||
490 | * @access public |
||
491 | * |
||
492 | * @param int $id |
||
493 | * @param mixed $data |
||
494 | * |
||
495 | * @return bool |
||
496 | */ |
||
497 | public static function set_db_query( $id, $data ) { |
||
507 | |||
508 | /** |
||
509 | * Get cache from group |
||
510 | * |
||
511 | * @since 2.0 |
||
512 | * @access public |
||
513 | * |
||
514 | * @param int $id |
||
515 | * |
||
516 | * @return mixed |
||
517 | */ |
||
518 | public static function get_db_query( $id ) { |
||
521 | |||
522 | /** |
||
523 | * Delete group cache |
||
524 | * |
||
525 | * @since 2.0 |
||
526 | * @access public |
||
527 | * |
||
528 | * @param int|array $ids |
||
529 | * @param string $group |
||
530 | * @param int $expire |
||
531 | * |
||
532 | * @return bool |
||
533 | */ |
||
534 | public static function delete_group( $ids, $group = '', $expire = 0 ) { |
||
580 | |||
581 | |||
582 | /** |
||
583 | * Delete form related cache |
||
584 | * Note: only use for internal purpose. |
||
585 | * |
||
586 | * @since 2.0 |
||
587 | * @access public |
||
588 | * |
||
589 | * @param int $form_id |
||
590 | */ |
||
591 | public function delete_form_related_cache( $form_id ) { |
||
616 | |||
617 | /** |
||
618 | * Delete payment related cache |
||
619 | * Note: only use for internal purpose. |
||
620 | * |
||
621 | * @since 2.0 |
||
622 | * @access public |
||
623 | * |
||
624 | * @param int $donation_id |
||
625 | */ |
||
626 | public function delete_payment_related_cache( $donation_id ) { |
||
643 | |||
644 | /** |
||
645 | * Delete donor related cache |
||
646 | * Note: only use for internal purpose. |
||
647 | * |
||
648 | * @since 2.0 |
||
649 | * @access public |
||
650 | * |
||
651 | * @param string $id |
||
652 | * @param string $group |
||
653 | * @param int $expire |
||
654 | */ |
||
655 | public function delete_donor_related_cache( $id, $group, $expire ) { |
||
667 | |||
668 | /** |
||
669 | * Delete donations related cache |
||
670 | * Note: only use for internal purpose. |
||
671 | * |
||
672 | * @since 2.0 |
||
673 | * @access public |
||
674 | * |
||
675 | * @param string $id |
||
676 | * @param string $group |
||
677 | * @param int $expire |
||
678 | */ |
||
679 | 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 private |
||
699 | * |
||
700 | * @param bool $refresh |
||
701 | * @param string $incrementer_key |
||
702 | * |
||
703 | * @return string |
||
704 | */ |
||
705 | private 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 | public function flush_cache() { |
||
734 | |||
735 | |||
736 | /** |
||
737 | * Filter the group name |
||
738 | * |
||
739 | * @since 2.0 |
||
740 | * @access private |
||
741 | * |
||
742 | * @param $group |
||
743 | * |
||
744 | * @return mixed |
||
745 | */ |
||
746 | private function filter_group_name( $group ) { |
||
766 | |||
767 | |||
768 | /** |
||
769 | * Disable cache. |
||
770 | * |
||
771 | * @since 2.0 |
||
772 | * @access public |
||
773 | */ |
||
774 | public static function disable() { |
||
777 | |||
778 | /** |
||
779 | * Enable cache. |
||
780 | * |
||
781 | * @since 2.0 |
||
782 | * @access public |
||
783 | */ |
||
784 | public static function enable() { |
||
787 | } |
||
788 | |||
791 |