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 TransientCacheStorage 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 TransientCacheStorage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class TransientCacheStorage implements CacheStorageInterface |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * wp-option option_name for tracking transients |
||
24 | * |
||
25 | * @type string |
||
26 | */ |
||
27 | const TRANSIENT_SCHEDULE_OPTIONS_KEY = 'ee_transient_schedule'; |
||
28 | |||
29 | /** |
||
30 | * @var int $current_time |
||
31 | */ |
||
32 | private $current_time; |
||
33 | |||
34 | /** |
||
35 | * how often to perform transient cleanup |
||
36 | * |
||
37 | * @var string $transient_cleanup_frequency |
||
38 | */ |
||
39 | private $transient_cleanup_frequency; |
||
40 | |||
41 | /** |
||
42 | * options for how often to perform transient cleanup |
||
43 | * |
||
44 | * @var array $transient_cleanup_frequency_options |
||
45 | */ |
||
46 | private $transient_cleanup_frequency_options = array(); |
||
47 | |||
48 | /** |
||
49 | * @var array $transients |
||
50 | */ |
||
51 | private $transients; |
||
52 | |||
53 | |||
54 | |||
55 | /** |
||
56 | * TransientCacheStorage constructor. |
||
57 | */ |
||
58 | public function __construct() |
||
68 | |||
69 | |||
70 | |||
71 | /** |
||
72 | * Sets how often transient cleanup occurs |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | private function setTransientCleanupFrequency() |
||
101 | |||
102 | |||
103 | |||
104 | /** |
||
105 | * we need to be able to round timestamps off to match the set transient cleanup frequency |
||
106 | * so if a transient is set to expire at 1:17 pm for example, and our cleanup schedule is every hour, |
||
107 | * then that timestamp needs to be rounded up to 2:00 pm so that it is removed |
||
108 | * during the next scheduled cleanup after its expiration. |
||
109 | * We also round off the current time timestamp to the closest 5 minutes |
||
110 | * just to make the timestamps a little easier to round which helps with debugging. |
||
111 | * |
||
112 | * @param int $timestamp [required] |
||
113 | * @param string $cleanup_frequency |
||
114 | * @param bool $round_up |
||
115 | * @return int |
||
116 | */ |
||
117 | private function roundTimestamp($timestamp, $cleanup_frequency = 'hour', $round_up = true) |
||
164 | |||
165 | |||
166 | |||
167 | /** |
||
168 | * Saves supplied data to a transient |
||
169 | * if an expiration is set, then it automatically schedules the transient for cleanup |
||
170 | * |
||
171 | * @param string $transient_key [required] |
||
172 | * @param string $data [required] |
||
173 | * @param int $expiration number of seconds until the cache expires |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function add($transient_key, $data, $expiration = 0) |
||
185 | |||
186 | |||
187 | |||
188 | /** |
||
189 | * retrieves transient data |
||
190 | * automatically triggers early cache refresh for standard cache items |
||
191 | * in order to avoid cache stampedes on busy sites. |
||
192 | * For non-standard cache items like PHP Session data where early refreshing is not wanted, |
||
193 | * the $standard_cache parameter should be set to false when retrieving data |
||
194 | * |
||
195 | * @param string $transient_key [required] |
||
196 | * @param bool $standard_cache |
||
197 | * @return mixed|null |
||
198 | */ |
||
199 | public function get($transient_key, $standard_cache = true) |
||
228 | |||
229 | |||
230 | |||
231 | /** |
||
232 | * delete a single transient and remove tracking |
||
233 | * |
||
234 | * @param string $transient_key [required] full or partial transient key to be deleted |
||
235 | */ |
||
236 | public function delete($transient_key) |
||
240 | |||
241 | |||
242 | |||
243 | /** |
||
244 | * delete multiple transients and remove tracking |
||
245 | * |
||
246 | * @param array $transient_keys [required] array of full or partial transient keys to be deleted |
||
247 | * @param bool $force_delete [optional] if true, then will not check incoming keys against those being tracked |
||
248 | * and proceed directly to deleting those entries from the cache storage |
||
249 | */ |
||
250 | public function deleteMany(array $transient_keys, $force_delete = false) |
||
266 | |||
267 | |||
268 | |||
269 | /** |
||
270 | * sorts transients numerically by timestamp |
||
271 | * then saves the transient schedule to a WP option |
||
272 | */ |
||
273 | private function updateTransients() |
||
281 | |||
282 | |||
283 | |||
284 | /** |
||
285 | * schedules a transient for cleanup by adding it to the transient tracking |
||
286 | * |
||
287 | * @param string $transient_key [required] |
||
288 | * @param int $expiration [required] |
||
289 | */ |
||
290 | private function scheduleTransientCleanup($transient_key, $expiration) |
||
300 | |||
301 | |||
302 | |||
303 | /** |
||
304 | * Since our tracked transients are sorted by their timestamps |
||
305 | * we can grab the first transient and see when it is scheduled for cleanup. |
||
306 | * If that timestamp is less than or equal to the current time, |
||
307 | * then cleanup is triggered |
||
308 | */ |
||
309 | public function checkTransientCleanupSchedule() |
||
324 | |||
325 | |||
326 | |||
327 | /** |
||
328 | * loops through the array of tracked transients, |
||
329 | * compiles a list of those that have expired, and sends that list off for deletion. |
||
330 | * Also removes any bad records from the transients array |
||
331 | * |
||
332 | * @return bool |
||
333 | */ |
||
334 | private function cleanupExpiredTransients() |
||
364 | |||
365 | |||
366 | |||
367 | /** |
||
368 | * calls delete_transient() on each transient key provided, up to the specified limit |
||
369 | * |
||
370 | * @param array $transient_keys [required] |
||
371 | * @param int $limit |
||
372 | * @return bool |
||
373 | */ |
||
374 | private function deleteTransientKeys(array $transient_keys, $limit = 50) |
||
397 | |||
398 | |||
399 | |||
400 | } |
||
401 | // End of file TransientCacheStorage.php |
||
403 |