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() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get cache key. |
||
| 84 | * |
||
| 85 | * @since 1.8.7 |
||
| 86 | * |
||
| 87 | * @param string $action Cache key prefix. |
||
| 88 | * @param array $query_args (optional) Query array. |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | |||
| 93 | public static function get_key( $action, $query_args = null ) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get cache. |
||
| 123 | * |
||
| 124 | * @since 1.8.7 |
||
| 125 | * |
||
| 126 | * @param string $cache_key |
||
| 127 | * @param bool $custom_key |
||
| 128 | * @param mixed $query_args |
||
| 129 | * |
||
| 130 | * @return mixed |
||
| 131 | */ |
||
| 132 | |||
| 133 | public static function get( $cache_key, $custom_key = false, $query_args = array() ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Set cache. |
||
| 163 | * |
||
| 164 | * @since 1.8.7 |
||
| 165 | * |
||
| 166 | * @param string $cache_key |
||
| 167 | * @param mixed $data |
||
| 168 | * @param int|null $expiration Timestamp should be in GMT format. |
||
| 169 | * @param bool $custom_key |
||
| 170 | * @param mixed $query_args |
||
| 171 | * |
||
| 172 | * @return mixed |
||
| 173 | */ |
||
| 174 | |||
| 175 | public static function set( $cache_key, $data, $expiration = null, $custom_key = false, $query_args = array() ) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Delete cache. |
||
| 198 | * |
||
| 199 | * Note: only for internal use |
||
| 200 | * |
||
| 201 | * @since 1.8.7 |
||
| 202 | * |
||
| 203 | * @param string|array $cache_keys |
||
| 204 | * |
||
| 205 | * @return bool|WP_Error |
||
| 206 | */ |
||
| 207 | |||
| 208 | public static function delete( $cache_keys ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Delete all logging cache. |
||
| 238 | * |
||
| 239 | * Note: only for internal use |
||
| 240 | * |
||
| 241 | * @since 1.8.7 |
||
| 242 | * @access public |
||
| 243 | * @global wpdb $wpdb |
||
| 244 | * |
||
| 245 | * @param bool $force If set to true then all cached values will be delete instead of only expired |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | public static function delete_all_expired( $force = false ) { |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * Get list of options like. |
||
| 293 | * |
||
| 294 | * Note: only for internal use |
||
| 295 | * |
||
| 296 | * @since 1.8.7 |
||
| 297 | * @access public |
||
| 298 | * |
||
| 299 | * @param string $option_name |
||
| 300 | * @param bool $fields |
||
| 301 | * |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | public static function get_options_like( $option_name, $fields = false ) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Check cache key validity. |
||
| 349 | * |
||
| 350 | * @since 1.8.7 |
||
| 351 | * @access public |
||
| 352 | * |
||
| 353 | * @param $cache_key |
||
| 354 | * |
||
| 355 | * @return bool |
||
| 356 | */ |
||
| 357 | public static function is_valid_cache_key( $cache_key ) { |
||
| 368 | |||
| 369 | |||
| 370 | /** |
||
| 371 | * Get cache from group |
||
| 372 | * |
||
| 373 | * @since 2.0 |
||
| 374 | * @access public |
||
| 375 | * |
||
| 376 | * @param int $id |
||
| 377 | * @param string $group |
||
| 378 | * |
||
| 379 | * @return mixed |
||
| 380 | */ |
||
| 381 | View Code Duplication | public static function get_group( $id, $group = '' ) { |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Cache small chunks inside group |
||
| 396 | * |
||
| 397 | * @since 2.0 |
||
| 398 | * @access public |
||
| 399 | * |
||
| 400 | * @param int $id |
||
| 401 | * @param mixed $data |
||
| 402 | * @param string $group |
||
| 403 | * @param int $expire |
||
| 404 | * |
||
| 405 | * @return bool |
||
| 406 | */ |
||
| 407 | View Code Duplication | public static function set_group( $id, $data, $group = '', $expire = 0 ) { |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Delete group cache |
||
| 422 | * |
||
| 423 | * @since 2.0 |
||
| 424 | * @access public |
||
| 425 | * |
||
| 426 | * @param int|array $ids |
||
| 427 | * @param string $group |
||
| 428 | * @param int $expire |
||
| 429 | * |
||
| 430 | * @return bool |
||
| 431 | */ |
||
| 432 | public static function delete_group( $ids, $group = '', $expire = 0 ) { |
||
| 476 | |||
| 477 | |||
| 478 | /** |
||
| 479 | * Delete form related cache |
||
| 480 | * Note: only use for internal purpose. |
||
| 481 | * |
||
| 482 | * @since 2.0 |
||
| 483 | * @access public |
||
| 484 | * |
||
| 485 | * @param int $form_id |
||
| 486 | */ |
||
| 487 | public function delete_form_related_cache( $form_id ) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Delete payment related cache |
||
| 518 | * Note: only use for internal purpose. |
||
| 519 | * |
||
| 520 | * @since 2.0 |
||
| 521 | * @access public |
||
| 522 | * |
||
| 523 | * @param int $donation_id |
||
| 524 | */ |
||
| 525 | public function delete_payment_related_cache( $donation_id ) { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Delete donor related cache |
||
| 545 | * Note: only use for internal purpose. |
||
| 546 | * |
||
| 547 | * @since 2.0 |
||
| 548 | * @access public |
||
| 549 | * |
||
| 550 | * @param string $id |
||
| 551 | * @param string $group |
||
| 552 | * @param int $expire |
||
| 553 | */ |
||
| 554 | public function delete_donor_related_cache( $id, $group, $expire ) { |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Delete donations 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_donations_related_cache( $id, $group, $expire ) { |
||
| 591 | |||
| 592 | |||
| 593 | /** |
||
| 594 | * Get unique incrementor. |
||
| 595 | * |
||
| 596 | * @see https://core.trac.wordpress.org/ticket/4476 |
||
| 597 | * @see https://www.tollmanz.com/invalidation-schemes/ |
||
| 598 | * |
||
| 599 | * @since 2.0 |
||
| 600 | * @access private |
||
| 601 | * |
||
| 602 | * @param bool $refresh |
||
| 603 | * |
||
| 604 | * @return string |
||
| 605 | */ |
||
| 606 | private function get_incrementor( $refresh = false ) { |
||
| 617 | } |
||
| 618 | |||
| 621 |