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 |
||
| 17 | class Give_Cache { |
||
|
|
|||
| 18 | /** |
||
| 19 | * Instance. |
||
| 20 | * |
||
| 21 | * @since 1.8.7 |
||
| 22 | * @access static |
||
| 23 | * @var |
||
| 24 | */ |
||
| 25 | static private $instance; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Singleton pattern. |
||
| 29 | * |
||
| 30 | * @since 1.8.7 |
||
| 31 | * @access private |
||
| 32 | * Give_Cache constructor. |
||
| 33 | */ |
||
| 34 | private function __construct() { |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Get instance. |
||
| 40 | * |
||
| 41 | * @since 1.8.7 |
||
| 42 | * @access public |
||
| 43 | * @return static |
||
| 44 | */ |
||
| 45 | public static function get_instance() { |
||
| 46 | if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Give_Cache ) ) { |
||
| 47 | self::$instance = new Give_Cache(); |
||
| 48 | } |
||
| 49 | |||
| 50 | return self::$instance; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Setup hooks. |
||
| 55 | * |
||
| 56 | * @since 1.8.7 |
||
| 57 | * @access public |
||
| 58 | */ |
||
| 59 | public function setup_hooks() { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get cache key. |
||
| 66 | * |
||
| 67 | * @since 1.8.7 |
||
| 68 | * |
||
| 69 | * @param string $action Cache key prefix. |
||
| 70 | * @param array $query_args (optional) Query array. |
||
| 71 | * |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | |||
| 75 | public static function get_key( $action, $query_args = null ) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get cache. |
||
| 88 | * |
||
| 89 | * @since 1.8.7 |
||
| 90 | * |
||
| 91 | * @param string $cache_key |
||
| 92 | * @param bool $custom_key |
||
| 93 | * @param mixed $query_args |
||
| 94 | * |
||
| 95 | * @return mixed |
||
| 96 | */ |
||
| 97 | |||
| 98 | public static function get( $cache_key, $custom_key = false, $query_args = array() ) { |
||
| 99 | View Code Duplication | if ( ! self::is_valid_cache_key( $cache_key ) ) { |
|
| 100 | if ( ! $custom_key ) { |
||
| 101 | return new WP_Error( 'give_invalid_cache_key', __( 'Cache key format should be give_cache_*', 'give' ) ); |
||
| 102 | } |
||
| 103 | |||
| 104 | $cache_key = self::get_key( $cache_key, $query_args ); |
||
| 105 | } |
||
| 106 | |||
| 107 | $option = get_option( $cache_key ); |
||
| 108 | |||
| 109 | // Backward compatibility (<1.8.7). |
||
| 110 | if ( ! is_array( $option ) || empty( $option ) || ! array_key_exists( 'expiration', $option ) ) { |
||
| 111 | return $option; |
||
| 112 | } |
||
| 113 | |||
| 114 | // Get current time. |
||
| 115 | $current_time = current_time( 'timestamp', 1 ); |
||
| 116 | |||
| 117 | if ( empty( $option['expiration'] ) || ( $current_time < $option['expiration'] ) ) { |
||
| 118 | $option = $option['data']; |
||
| 119 | } else { |
||
| 120 | $option = false; |
||
| 121 | } |
||
| 122 | |||
| 123 | return $option; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Set cache. |
||
| 128 | * |
||
| 129 | * @since 1.8.7 |
||
| 130 | * |
||
| 131 | * @param string $cache_key |
||
| 132 | * @param mixed $data |
||
| 133 | * @param int|null $expiration Timestamp should be in GMT format. |
||
| 134 | * @param bool $custom_key |
||
| 135 | * @param mixed $query_args |
||
| 136 | * |
||
| 137 | * @return mixed |
||
| 138 | */ |
||
| 139 | |||
| 140 | public static function set( $cache_key, $data, $expiration = null, $custom_key = false, $query_args = array() ) { |
||
| 141 | View Code Duplication | if ( ! self::is_valid_cache_key( $cache_key ) ) { |
|
| 142 | if ( ! $custom_key ) { |
||
| 143 | return new WP_Error( 'give_invalid_cache_key', __( 'Cache key format should be give_cache_*', 'give' ) ); |
||
| 144 | } |
||
| 145 | |||
| 146 | $cache_key = self::get_key( $cache_key, $query_args ); |
||
| 147 | } |
||
| 148 | |||
| 149 | $option_value = array( |
||
| 150 | 'data' => $data, |
||
| 151 | 'expiration' => ! is_null( $expiration ) |
||
| 152 | ? ( $expiration + current_time( 'timestamp', 1 ) ) |
||
| 153 | : null, |
||
| 154 | ); |
||
| 155 | |||
| 156 | $result = update_option( $cache_key, $option_value, 'no' ); |
||
| 157 | |||
| 158 | return $result; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Delete cache. |
||
| 163 | * |
||
| 164 | * @since 1.8.7 |
||
| 165 | * |
||
| 166 | * @param string|array $cache_keys |
||
| 167 | * |
||
| 168 | * @return bool|WP_Error |
||
| 169 | */ |
||
| 170 | |||
| 171 | public static function delete( $cache_keys ) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Delete all logging cache. |
||
| 201 | * |
||
| 202 | * @since 1.8.7 |
||
| 203 | * @access public |
||
| 204 | * @global wpdb $wpdb |
||
| 205 | * |
||
| 206 | * @param bool $force If set to true then all cached values will be delete instead of only expired |
||
| 207 | * |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public static function delete_all_expired( $force = false ) { |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * Get list of options like. |
||
| 254 | * |
||
| 255 | * @since 1.8.7 |
||
| 256 | * @access public |
||
| 257 | * |
||
| 258 | * @param string $option_name |
||
| 259 | * @param bool $fields |
||
| 260 | * |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | public static function get_options_like( $option_name, $fields = false ) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Check cache key validity. |
||
| 308 | * |
||
| 309 | * @since 1.8.7 |
||
| 310 | * @access public |
||
| 311 | * |
||
| 312 | * @param $cache_key |
||
| 313 | * |
||
| 314 | * @return bool|int |
||
| 315 | */ |
||
| 316 | public static function is_valid_cache_key( $cache_key ) { |
||
| 319 | |||
| 320 | |||
| 321 | /** |
||
| 322 | * Cache small chunks inside group |
||
| 323 | * |
||
| 324 | * @since 2.0 |
||
| 325 | * @access public |
||
| 326 | * |
||
| 327 | * @param $group_type |
||
| 328 | * @param array $args { |
||
| 329 | * |
||
| 330 | * @type string $id |
||
| 331 | * @type string $key |
||
| 332 | * @type string $data |
||
| 333 | * |
||
| 334 | * } |
||
| 335 | * |
||
| 336 | * @return bool|array|WP_Error |
||
| 337 | */ |
||
| 338 | public static function group( $group_type, $args = array() ) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Delete payment cache. |
||
| 366 | * |
||
| 367 | * @since 2.0 |
||
| 368 | * @access public |
||
| 369 | * |
||
| 370 | * @param string $group_type |
||
| 371 | * @param string $id |
||
| 372 | * |
||
| 373 | * @return mixed |
||
| 374 | */ |
||
| 375 | public static function delete_group( $group_type, $id ) { |
||
| 384 | } |
||
| 385 | |||
| 386 | // Initialize |
||
| 388 |