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() { |
||
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() ) { |
||
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() ) { |
||
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 | |||
325 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.