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 ) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get cache. |
||
| 126 | * |
||
| 127 | * @since 1.8.7 |
||
| 128 | * |
||
| 129 | * @param string $cache_key |
||
| 130 | * @param bool $custom_key |
||
| 131 | * @param mixed $query_args |
||
| 132 | * |
||
| 133 | * @return mixed |
||
| 134 | */ |
||
| 135 | |||
| 136 | public static function get( $cache_key, $custom_key = false, $query_args = array() ) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Set cache. |
||
| 166 | * |
||
| 167 | * @since 1.8.7 |
||
| 168 | * |
||
| 169 | * @param string $cache_key |
||
| 170 | * @param mixed $data |
||
| 171 | * @param int|null $expiration Timestamp should be in GMT format. |
||
| 172 | * @param bool $custom_key |
||
| 173 | * @param mixed $query_args |
||
| 174 | * |
||
| 175 | * @return mixed |
||
| 176 | */ |
||
| 177 | |||
| 178 | public static function set( $cache_key, $data, $expiration = null, $custom_key = false, $query_args = array() ) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Delete cache. |
||
| 201 | * |
||
| 202 | * Note: only for internal use |
||
| 203 | * |
||
| 204 | * @since 1.8.7 |
||
| 205 | * |
||
| 206 | * @param string|array $cache_keys |
||
| 207 | * |
||
| 208 | * @return bool|WP_Error |
||
| 209 | */ |
||
| 210 | |||
| 211 | public static function delete( $cache_keys ) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Delete all logging cache. |
||
| 241 | * |
||
| 242 | * Note: only for internal use |
||
| 243 | * |
||
| 244 | * @since 1.8.7 |
||
| 245 | * @access public |
||
| 246 | * @global wpdb $wpdb |
||
| 247 | * |
||
| 248 | * @param bool $force If set to true then all cached values will be delete instead of only expired |
||
| 249 | * |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | public static function delete_all_expired( $force = false ) { |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * Get list of options like. |
||
| 296 | * |
||
| 297 | * Note: only for internal use |
||
| 298 | * |
||
| 299 | * @since 1.8.7 |
||
| 300 | * @access public |
||
| 301 | * |
||
| 302 | * @param string $option_name |
||
| 303 | * @param bool $fields |
||
| 304 | * |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | public static function get_options_like( $option_name, $fields = false ) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Check cache key validity. |
||
| 352 | * |
||
| 353 | * @since 1.8.7 |
||
| 354 | * @access public |
||
| 355 | * |
||
| 356 | * @param $cache_key |
||
| 357 | * |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | public static function is_valid_cache_key( $cache_key ) { |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Get cache from group |
||
| 375 | * |
||
| 376 | * @since 2.0 |
||
| 377 | * @access public |
||
| 378 | * |
||
| 379 | * @param int $id |
||
| 380 | * @param string $group |
||
| 381 | * |
||
| 382 | * @return mixed |
||
| 383 | */ |
||
| 384 | public static function get_group( $id, $group = '' ) { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Cache small chunks inside group |
||
| 399 | * |
||
| 400 | * @since 2.0 |
||
| 401 | * @access public |
||
| 402 | * |
||
| 403 | * @param int $id |
||
| 404 | * @param mixed $data |
||
| 405 | * @param string $group |
||
| 406 | * @param int $expire |
||
| 407 | * |
||
| 408 | * @return bool |
||
| 409 | */ |
||
| 410 | public static function set_group( $id, $data, $group = '', $expire = 0 ) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Delete group cache |
||
| 427 | * |
||
| 428 | * @since 2.0 |
||
| 429 | * @access public |
||
| 430 | * |
||
| 431 | * @param int|array $ids |
||
| 432 | * @param string $group |
||
| 433 | * @param int $expire |
||
| 434 | * |
||
| 435 | * @return bool |
||
| 436 | */ |
||
| 437 | public static function delete_group( $ids, $group = '', $expire = 0 ) { |
||
| 483 | |||
| 484 | |||
| 485 | /** |
||
| 486 | * Delete form related cache |
||
| 487 | * Note: only use for internal purpose. |
||
| 488 | * |
||
| 489 | * @since 2.0 |
||
| 490 | * @access public |
||
| 491 | * |
||
| 492 | * @param int $form_id |
||
| 493 | */ |
||
| 494 | public function delete_form_related_cache( $form_id ) { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Delete payment related cache |
||
| 523 | * Note: only use for internal purpose. |
||
| 524 | * |
||
| 525 | * @since 2.0 |
||
| 526 | * @access public |
||
| 527 | * |
||
| 528 | * @param int $donation_id |
||
| 529 | */ |
||
| 530 | public function delete_payment_related_cache( $donation_id ) { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Delete donor related cache |
||
| 548 | * Note: only use for internal purpose. |
||
| 549 | * |
||
| 550 | * @since 2.0 |
||
| 551 | * @access public |
||
| 552 | * |
||
| 553 | * @param string $id |
||
| 554 | * @param string $group |
||
| 555 | * @param int $expire |
||
| 556 | */ |
||
| 557 | public function delete_donor_related_cache( $id, $group, $expire ) { |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Delete donations related cache |
||
| 573 | * Note: only use for internal purpose. |
||
| 574 | * |
||
| 575 | * @since 2.0 |
||
| 576 | * @access public |
||
| 577 | * |
||
| 578 | * @param string $id |
||
| 579 | * @param string $group |
||
| 580 | * @param int $expire |
||
| 581 | */ |
||
| 582 | public function delete_donations_related_cache( $id, $group, $expire ) { |
||
| 590 | |||
| 591 | |||
| 592 | /** |
||
| 593 | * Get unique timestamp. |
||
| 594 | * |
||
| 595 | * @since 2.0 |
||
| 596 | * @access private |
||
| 597 | * |
||
| 598 | * @param $context |
||
| 599 | * |
||
| 600 | * @return string |
||
| 601 | */ |
||
| 602 | private function get_unique_timestamp( $context ) { |
||
| 616 | } |
||
| 617 | |||
| 620 |