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 |
||
23 | class TransientCacheStorage implements CacheStorageInterface |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * wp-option option_name for tracking transients |
||
28 | * |
||
29 | * @type string |
||
30 | */ |
||
31 | const TRANSIENT_SCHEDULE_OPTIONS_KEY = 'ee_transient_schedule'; |
||
32 | |||
33 | /** |
||
34 | * @var int $current_time |
||
35 | */ |
||
36 | private $current_time = 0; |
||
37 | |||
38 | /** |
||
39 | * how often to perform transient cleanup |
||
40 | * |
||
41 | * @var string $transient_cleanup_frequency |
||
42 | */ |
||
43 | private $transient_cleanup_frequency = 'hour'; |
||
44 | |||
45 | /** |
||
46 | * options for how often to perform transient cleanup |
||
47 | * |
||
48 | * @var array $transient_cleanup_frequency_options |
||
49 | */ |
||
50 | private $transient_cleanup_frequency_options = array(); |
||
51 | |||
52 | /** |
||
53 | * @var array $transients |
||
54 | */ |
||
55 | private $transients = array(); |
||
56 | |||
57 | |||
58 | |||
59 | /** |
||
60 | * TransientCacheStorage constructor. |
||
61 | */ |
||
62 | public function __construct() |
||
72 | |||
73 | |||
74 | |||
75 | /** |
||
76 | * Sets how often transient cleanup occurs |
||
77 | * |
||
78 | * @return int |
||
79 | */ |
||
80 | private function setTransientCleanupFrequency() |
||
105 | |||
106 | |||
107 | |||
108 | /** |
||
109 | * we need to be able to round timestamps off to match the set transient cleanup frequency |
||
110 | * so if a transient is set to expire at 1:17 pm for example, and our cleanup schedule is every hour, |
||
111 | * then that timestamp needs to be rounded up to 2:00 pm so that it is removed |
||
112 | * during the next scheduled cleanup after its expiration. |
||
113 | * We also round off the current time timestamp to the closest 5 minutes |
||
114 | * just to make the timestamps a little easier to round which helps with debugging. |
||
115 | * |
||
116 | * @param int $timestamp [required] |
||
117 | * @param string $cleanup_frequency |
||
118 | * @param bool $round_up |
||
119 | * @return false|int |
||
120 | */ |
||
121 | private function roundTimestamp($timestamp, $cleanup_frequency = 'hour', $round_up = true) |
||
168 | |||
169 | |||
170 | |||
171 | /** |
||
172 | * Saves supplied data to a transient |
||
173 | * if an expiration is set, then it automatically schedules the transient for cleanup |
||
174 | * |
||
175 | * @param string $transient_key [required] |
||
176 | * @param string $data [required] |
||
177 | * @param int $expiration number of seconds until the cache expires |
||
178 | * @return bool |
||
179 | */ |
||
180 | public function add($transient_key, $data, $expiration = 0) |
||
189 | |||
190 | |||
191 | |||
192 | /** |
||
193 | * retrieves transient data |
||
194 | * automatically triggers early cache refresh for standard cache items |
||
195 | * in order to avoid cache stampedes on busy sites. |
||
196 | * For non-standard cache items like PHP Session data where early refreshing is not wanted, |
||
197 | * the $standard_cache parameter should be set to false when retrieving data |
||
198 | * |
||
199 | * @param string $transient_key [required] |
||
200 | * @param bool $standard_cache |
||
201 | * @return mixed|null |
||
202 | */ |
||
203 | public function get($transient_key, $standard_cache = true) |
||
223 | |||
224 | |||
225 | |||
226 | /** |
||
227 | * delete a single transient and remove tracking |
||
228 | * |
||
229 | * @param string $transient_key [required] full or partial transient key to be deleted |
||
230 | */ |
||
231 | public function delete($transient_key) |
||
235 | |||
236 | |||
237 | |||
238 | /** |
||
239 | * delete multiple transients and remove tracking |
||
240 | * |
||
241 | * @param array $transient_keys [required] array of full or partial transient keys to be deleted |
||
242 | */ |
||
243 | public function deleteMany(array $transient_keys) |
||
257 | |||
258 | |||
259 | |||
260 | /** |
||
261 | * sorts transients numerically by timestamp |
||
262 | * then saves the transient schedule to a WP option |
||
263 | */ |
||
264 | private function updateTransients() |
||
272 | |||
273 | |||
274 | |||
275 | /** |
||
276 | * schedules a transient for cleanup by adding it to the transient tracking |
||
277 | * |
||
278 | * @param string $transient_key [required] |
||
279 | * @param int $expiration [required] |
||
280 | */ |
||
281 | private function scheduleTransientCleanup($transient_key, $expiration) |
||
291 | |||
292 | |||
293 | |||
294 | /** |
||
295 | * Since our tracked transients are sorted by their timestamps |
||
296 | * we can grab the first transient and see when it is scheduled for cleanup. |
||
297 | * If that timestamp is less than or equal to the current time, |
||
298 | * then cleanup is triggered |
||
299 | */ |
||
300 | public function checkTransientCleanupSchedule() |
||
315 | |||
316 | |||
317 | |||
318 | /** |
||
319 | * loops through the array of tracked transients, |
||
320 | * compiles a list of those that have expired, and sends that list off for deletion. |
||
321 | * Also removes any bad records from the transients array |
||
322 | * |
||
323 | * @return bool |
||
324 | */ |
||
325 | private function cleanupExpiredTransients() |
||
355 | |||
356 | |||
357 | |||
358 | /** |
||
359 | * calls delete_transient() on each transient key provided, up to the specified limit |
||
360 | * |
||
361 | * @param array $transient_keys [required] |
||
362 | * @param int $limit |
||
363 | * @return bool |
||
364 | */ |
||
365 | private function deleteTransientKeys(array $transient_keys, $limit = 50) |
||
381 | |||
382 | |||
383 | } |
||
384 | // End of file TransientCacheStorage.php |
||
385 | // Location: EventEspresso\core\services\cache/TransientCacheStorage.php |