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() { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get cache key. |
||
| 78 | * |
||
| 79 | * @since 1.8.7 |
||
| 80 | * |
||
| 81 | * @param string $action Cache key prefix. |
||
| 82 | * @param array $query_args (optional) Query array. |
||
| 83 | * |
||
| 84 | * @return string|WP_Error |
||
| 85 | */ |
||
| 86 | |||
| 87 | public static function get_key( $action, $query_args = null ) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get cache. |
||
| 121 | * |
||
| 122 | * @since 1.8.7 |
||
| 123 | * |
||
| 124 | * @param string $cache_key |
||
| 125 | * @param bool $custom_key |
||
| 126 | * @param mixed $query_args |
||
| 127 | * |
||
| 128 | * @return mixed |
||
| 129 | */ |
||
| 130 | |||
| 131 | public static function get( $cache_key, $custom_key = false, $query_args = array() ) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Set cache. |
||
| 161 | * |
||
| 162 | * @since 1.8.7 |
||
| 163 | * |
||
| 164 | * @param string $cache_key |
||
| 165 | * @param mixed $data |
||
| 166 | * @param int|null $expiration Timestamp should be in GMT format. |
||
| 167 | * @param bool $custom_key |
||
| 168 | * @param mixed $query_args |
||
| 169 | * |
||
| 170 | * @return mixed |
||
| 171 | */ |
||
| 172 | |||
| 173 | public static function set( $cache_key, $data, $expiration = null, $custom_key = false, $query_args = array() ) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Delete cache. |
||
| 196 | * |
||
| 197 | * Note: only for internal use |
||
| 198 | * |
||
| 199 | * @since 1.8.7 |
||
| 200 | * |
||
| 201 | * @param string|array $cache_keys |
||
| 202 | * |
||
| 203 | * @return bool|WP_Error |
||
| 204 | */ |
||
| 205 | |||
| 206 | public static function delete( $cache_keys ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Delete all logging cache. |
||
| 236 | * |
||
| 237 | * Note: only for internal use |
||
| 238 | * |
||
| 239 | * @since 1.8.7 |
||
| 240 | * @access public |
||
| 241 | * @global wpdb $wpdb |
||
| 242 | * |
||
| 243 | * @param bool $force If set to true then all cached values will be delete instead of only expired |
||
| 244 | * |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | public static function delete_all_expired( $force = false ) { |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * Get list of options like. |
||
| 291 | * |
||
| 292 | * Note: only for internal use |
||
| 293 | * |
||
| 294 | * @since 1.8.7 |
||
| 295 | * @access public |
||
| 296 | * |
||
| 297 | * @param string $option_name |
||
| 298 | * @param bool $fields |
||
| 299 | * |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | public static function get_options_like( $option_name, $fields = false ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Check cache key validity. |
||
| 347 | * |
||
| 348 | * @since 1.8.7 |
||
| 349 | * @access public |
||
| 350 | * |
||
| 351 | * @param $cache_key |
||
| 352 | * |
||
| 353 | * @return bool |
||
| 354 | */ |
||
| 355 | public static function is_valid_cache_key( $cache_key ) { |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * Get cache from group |
||
| 370 | * |
||
| 371 | * @since 2.0 |
||
| 372 | * @access public |
||
| 373 | * |
||
| 374 | * @param int $id |
||
| 375 | * @param string $group |
||
| 376 | * |
||
| 377 | * @return mixed |
||
| 378 | */ |
||
| 379 | public static function get_group( $id, $group = '' ) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Cache small chunks inside group |
||
| 398 | * |
||
| 399 | * @since 2.0 |
||
| 400 | * @access public |
||
| 401 | * |
||
| 402 | * @param int $id |
||
| 403 | * @param mixed $data |
||
| 404 | * @param string $group |
||
| 405 | * @param int $expire |
||
| 406 | * |
||
| 407 | * @return bool |
||
| 408 | */ |
||
| 409 | public static function set_group( $id, $data, $group = '', $expire = 0 ) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Delete group cache |
||
| 430 | * |
||
| 431 | * @since 2.0 |
||
| 432 | * @access public |
||
| 433 | * |
||
| 434 | * @param int $id |
||
| 435 | * @param string $group |
||
| 436 | * @param int $expire |
||
| 437 | * |
||
| 438 | * @return bool |
||
| 439 | */ |
||
| 440 | public static function delete_group( $id, $group = '', $expire = 0 ) { |
||
| 474 | } |
||
| 475 | |||
| 480 |