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 static |
||
| 24 | * @var |
||
| 25 | */ |
||
| 26 | static private $instance; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Singleton pattern. |
||
| 30 | * |
||
| 31 | * @since 1.8.7 |
||
| 32 | * @access private |
||
| 33 | * Give_Cache constructor. |
||
| 34 | */ |
||
| 35 | private function __construct() { |
||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * Get instance. |
||
| 41 | * |
||
| 42 | * @since 1.8.7 |
||
| 43 | * @access public |
||
| 44 | * @return static |
||
| 45 | */ |
||
| 46 | public static function get_instance() { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Setup hooks. |
||
| 56 | * |
||
| 57 | * @since 1.8.7 |
||
| 58 | * @access public |
||
| 59 | */ |
||
| 60 | public function setup_hooks() { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get cache key. |
||
| 67 | * |
||
| 68 | * @since 1.8.7 |
||
| 69 | * |
||
| 70 | * @param string $action Cache key prefix. |
||
| 71 | * @param array $query_args (optional) Query array. |
||
| 72 | * |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | |||
| 76 | public static function get_key( $action, $query_args = null ) { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get cache. |
||
| 89 | * |
||
| 90 | * @since 1.8.7 |
||
| 91 | * |
||
| 92 | * @param string $cache_key |
||
| 93 | * @param bool $custom_key |
||
| 94 | * @param mixed $query_args |
||
| 95 | * |
||
| 96 | * @return mixed |
||
| 97 | */ |
||
| 98 | |||
| 99 | public static function get( $cache_key, $custom_key = false, $query_args = array() ) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Set cache. |
||
| 129 | * |
||
| 130 | * @since 1.8.7 |
||
| 131 | * |
||
| 132 | * @param string $cache_key |
||
| 133 | * @param mixed $data |
||
| 134 | * @param int|null $expiration Timestamp should be in GMT format. |
||
| 135 | * @param bool $custom_key |
||
| 136 | * @param mixed $query_args |
||
| 137 | * |
||
| 138 | * @return mixed |
||
| 139 | */ |
||
| 140 | |||
| 141 | public static function set( $cache_key, $data, $expiration = null, $custom_key = false, $query_args = array() ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Delete cache. |
||
| 164 | * |
||
| 165 | * @since 1.8.7 |
||
| 166 | * |
||
| 167 | * @param string|array $cache_keys |
||
| 168 | * |
||
| 169 | * @return bool|WP_Error |
||
| 170 | */ |
||
| 171 | |||
| 172 | public static function delete( $cache_keys ) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Delete all logging cache. |
||
| 202 | * |
||
| 203 | * @since 1.8.7 |
||
| 204 | * @access public |
||
| 205 | * @global wpdb $wpdb |
||
| 206 | * |
||
| 207 | * @param bool $force If set to true then all cached values will be delete instead of only expired |
||
| 208 | * |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | public static function delete_all_expired( $force = false ) { |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * Get list of options like. |
||
| 255 | * |
||
| 256 | * @since 1.8.7 |
||
| 257 | * @access public |
||
| 258 | * |
||
| 259 | * @param string $option_name |
||
| 260 | * @param bool $fields |
||
| 261 | * |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public static function get_options_like( $option_name, $fields = false ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Check cache key validity. |
||
| 309 | * |
||
| 310 | * @since 1.8.7 |
||
| 311 | * @access public |
||
| 312 | * |
||
| 313 | * @param $cache_key |
||
| 314 | * |
||
| 315 | * @return bool|int |
||
| 316 | */ |
||
| 317 | public static function is_valid_cache_key( $cache_key ) { |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * Get cache from group |
||
| 324 | * |
||
| 325 | * @since 2.0 |
||
| 326 | * @access public |
||
| 327 | * |
||
| 328 | * @param int $id |
||
| 329 | * @param string $group |
||
| 330 | * @param string $cache_type |
||
| 331 | * |
||
| 332 | * @return mixed |
||
| 333 | */ |
||
| 334 | View Code Duplication | public static function get_group( $id, $group = '', $cache_type = 'persistent' ) { |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Cache small chunks inside group |
||
| 358 | * |
||
| 359 | * @since 2.0 |
||
| 360 | * @access public |
||
| 361 | * |
||
| 362 | * @param int $id |
||
| 363 | * @param mixed $data |
||
| 364 | * @param string $group |
||
| 365 | * @param int $expire |
||
| 366 | * @param string $cache_type |
||
| 367 | * |
||
| 368 | * @return bool |
||
| 369 | */ |
||
| 370 | View Code Duplication | public static function set_group( $id, $data, $group = '', $expire = 0, $cache_type = 'persistent' ) { |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Delete group cache |
||
| 394 | * |
||
| 395 | * @since 2.0 |
||
| 396 | * @access public |
||
| 397 | * |
||
| 398 | * @param int $id |
||
| 399 | * @param string $group |
||
| 400 | * @param int $expire |
||
| 401 | * @param string $cache_type |
||
| 402 | * |
||
| 403 | * @return bool |
||
| 404 | */ |
||
| 405 | View Code Duplication | public static function delete_group( $id, $group = '', $expire = 0, $cache_type = 'persistent' ) { |
|
| 426 | } |
||
| 427 | |||
| 432 |