Complex classes like CCachePool 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 CCachePool, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class CCachePool implements \Psr\Cache\CacheItemPoolInterface |
||
9 | { |
||
10 | /** |
||
11 | * An array with all cache items, |
||
12 | * @var \Psr\Cache\CacheItemInterface[] |
||
13 | */ |
||
14 | private $cacheItems; |
||
15 | |||
16 | /** |
||
17 | * An array of items to be saved later |
||
18 | * @var \Psr\Cache\CacheItemInterface[] |
||
19 | */ |
||
20 | private $defferedItems; |
||
21 | |||
22 | /** |
||
23 | * Inits one array with the cached items and one with the deffered |
||
24 | * |
||
25 | */ |
||
26 | 1 | public function __construct() |
|
36 | |||
37 | /** |
||
38 | * Returns a Cache Item representing the specified key. |
||
39 | * |
||
40 | * This method must always return a CacheItemInterface object, even in case of |
||
41 | * a cache miss. It MUST NOT return null. |
||
42 | * |
||
43 | * @param string $key |
||
44 | * The key for which to return the corresponding Cache Item. |
||
45 | * |
||
46 | * @throws InvalidArgumentException |
||
47 | * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
||
48 | * MUST be thrown. |
||
49 | * |
||
50 | * @return CacheItemInterface |
||
51 | * The corresponding Cache Item. |
||
52 | */ |
||
53 | public function getItem($key) |
||
71 | |||
72 | /** |
||
73 | * Generates a key from a string. |
||
74 | * |
||
75 | * @param string $str |
||
76 | * The value to convert to a key-string, must be unique |
||
77 | * |
||
78 | * @return string |
||
79 | * A generated key-string |
||
80 | */ |
||
81 | public function generateKey($str) |
||
86 | |||
87 | /** |
||
88 | * Checks if a key string is valid, invalid values are {}()/\@: |
||
89 | * |
||
90 | * @param string $key |
||
91 | * A string to ckeck, invalid values are {}()/\@: |
||
92 | * |
||
93 | * @throws InvalidArgumentException |
||
94 | * If $key are not a legal value |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | 1 | public function checkKey($key) |
|
112 | |||
113 | /** |
||
114 | * Returns a traversable set of cache items. |
||
115 | * |
||
116 | * @param array $keys |
||
117 | * An indexed array of keys of items to retrieve. |
||
118 | * |
||
119 | * @throws InvalidArgumentException |
||
120 | * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
||
121 | * MUST be thrown. |
||
122 | * |
||
123 | * @return array|\Traversable |
||
124 | * A traversable collection of Cache Items keyed by the cache keys of |
||
125 | * each item. A Cache item will be returned for each key, even if that |
||
126 | * key is not found. However, if no keys are specified then an empty |
||
127 | * traversable MUST be returned instead. |
||
128 | */ |
||
129 | public function getItems(array $keys = array()) |
||
145 | |||
146 | /** |
||
147 | * Confirms if the cache contains specified cache item. |
||
148 | * |
||
149 | * Note: This method MAY avoid retrieving the cached value for performance reasons. |
||
150 | * This could result in a race condition with CacheItemInterface::get(). To avoid |
||
151 | * such situation use CacheItemInterface::isHit() instead. |
||
152 | * |
||
153 | * @param string $key |
||
154 | * The key for which to check existence. |
||
155 | * |
||
156 | * @throws InvalidArgumentException |
||
157 | * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
||
158 | * MUST be thrown. |
||
159 | * |
||
160 | * @return bool |
||
161 | * True if item exists in the cache, false otherwise. |
||
162 | */ |
||
163 | 1 | public function hasItem($key) |
|
175 | |||
176 | /** |
||
177 | * Deletes all items in the pool. |
||
178 | * |
||
179 | * @return bool |
||
180 | * True if the pool was successfully cleared. False if there was an error. |
||
181 | */ |
||
182 | public function clear() |
||
198 | |||
199 | /** |
||
200 | * Removes the item from the pool. |
||
201 | * |
||
202 | * @param string $key |
||
203 | * The key for which to delete |
||
204 | * |
||
205 | * @throws InvalidArgumentException |
||
206 | * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
||
207 | * MUST be thrown. |
||
208 | * |
||
209 | * @return bool |
||
210 | * True if the item was successfully removed. False if there was an error. |
||
211 | */ |
||
212 | public function deleteItem($key) |
||
231 | |||
232 | /** |
||
233 | * Removes multiple items from the pool. |
||
234 | * |
||
235 | * @param array $keys |
||
236 | * An array of keys that should be removed from the pool. |
||
237 | |||
238 | * @throws InvalidArgumentException |
||
239 | * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
||
240 | * MUST be thrown. |
||
241 | * |
||
242 | * @return bool |
||
243 | * True if the items were successfully removed. False if there was an error. |
||
244 | */ |
||
245 | public function deleteItems(array $keys) |
||
259 | |||
260 | /** |
||
261 | * Persists a cache item immediately. |
||
262 | * |
||
263 | * @param CacheItemInterface $item |
||
264 | * The cache item to save. |
||
265 | * |
||
266 | * @return bool |
||
267 | * True if the item was successfully persisted. False if there was an error. |
||
268 | */ |
||
269 | public function save(\Psr\Cache\CacheItemInterface $item) |
||
281 | |||
282 | /** |
||
283 | * Sets a cache item to be persisted later. |
||
284 | * |
||
285 | * @param CacheItemInterface $item |
||
286 | * The cache item to save. |
||
287 | * |
||
288 | * @return bool |
||
289 | * False if the item could not be queued or if a commit was attempted and failed. True otherwise. |
||
290 | */ |
||
291 | public function saveDeferred(\Psr\Cache\CacheItemInterface $item) |
||
307 | |||
308 | /** |
||
309 | * Persists any deferred cache items. |
||
310 | * |
||
311 | * @return bool |
||
312 | * True if all not-yet-saved items were successfully saved or there were none. False otherwise. |
||
313 | */ |
||
314 | public function commit() |
||
327 | |||
328 | /** |
||
329 | * Saves the current items/defferedItems in the session |
||
330 | * |
||
331 | * @param bool $value If the items/defferedItems should be saved in the session |
||
332 | * |
||
333 | * @return void |
||
334 | */ |
||
335 | public function saveInSession($value) |
||
342 | |||
343 | /** |
||
344 | * Clears the current session |
||
345 | * |
||
346 | * @return void |
||
347 | */ |
||
348 | public function clearSession() |
||
357 | } |
||
358 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: