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 | 13 | 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 \Psr\Cache\CacheItemInterface |
||
51 | * The corresponding Cache Item. |
||
52 | */ |
||
53 | 9 | 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 | 2 | 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 | 11 | 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 | * @throws InvalidArgumentException |
||
119 | * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
||
120 | * MUST be thrown. |
||
121 | * |
||
122 | * @return array|\Traversable |
||
123 | * A traversable collection of Cache Items keyed by the cache keys of |
||
124 | * each item. A Cache item will be returned for each key, even if that |
||
125 | * key is not found. However, if no keys are specified then an empty |
||
126 | * traversable MUST be returned instead. |
||
127 | */ |
||
128 | 1 | public function getItems(array $keys = array()) |
|
144 | |||
145 | /** |
||
146 | * Confirms if the cache contains specified cache item. |
||
147 | * |
||
148 | * Note: This method MAY avoid retrieving the cached value for performance reasons. |
||
149 | * This could result in a race condition with CacheItemInterface::get(). To avoid |
||
150 | * such situation use CacheItemInterface::isHit() instead. |
||
151 | * |
||
152 | * @param string $key |
||
153 | * The key for which to check existence. |
||
154 | * |
||
155 | * @throws InvalidArgumentException |
||
156 | * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
||
157 | * MUST be thrown. |
||
158 | * |
||
159 | * @return bool |
||
160 | * True if item exists in the cache, false otherwise. |
||
161 | */ |
||
162 | 1 | public function hasItem($key) |
|
174 | |||
175 | /** |
||
176 | * Deletes all items in the pool. |
||
177 | * |
||
178 | * @return bool |
||
179 | * True if the pool was successfully cleared. False if there was an error. |
||
180 | */ |
||
181 | 1 | public function clear() |
|
197 | |||
198 | /** |
||
199 | * Removes the item from the pool. |
||
200 | * |
||
201 | * @param string $key |
||
202 | * The key for which to delete |
||
203 | * |
||
204 | * @throws InvalidArgumentException |
||
205 | * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException |
||
206 | * MUST be thrown. |
||
207 | * |
||
208 | * @return bool |
||
209 | * True if the item was successfully removed. False if there was an error. |
||
210 | */ |
||
211 | 2 | public function deleteItem($key) |
|
230 | |||
231 | /** |
||
232 | * Removes multiple items from the pool. |
||
233 | * |
||
234 | * @param array $keys |
||
235 | * An array of keys that should be removed from the pool. |
||
236 | |||
237 | * @throws InvalidArgumentException |
||
238 | * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException |
||
239 | * MUST be thrown. |
||
240 | * |
||
241 | * @return bool |
||
242 | * True if the items were successfully removed. False if there was an error. |
||
243 | */ |
||
244 | 2 | public function deleteItems(array $keys) |
|
258 | |||
259 | /** |
||
260 | * Persists a cache item immediately. |
||
261 | * |
||
262 | * @param \Psr\Cache\CacheItemInterface $item |
||
263 | * The cache item to save. |
||
264 | * |
||
265 | * @return bool |
||
266 | * True if the item was successfully persisted. False if there was an error. |
||
267 | */ |
||
268 | 5 | public function save(\Psr\Cache\CacheItemInterface $item) |
|
280 | |||
281 | /** |
||
282 | * Sets a cache item to be persisted later. |
||
283 | * |
||
284 | * @param \Psr\Cache\CacheItemInterface $item |
||
285 | * The cache item to save. |
||
286 | * |
||
287 | * @return bool |
||
288 | * False if the item could not be queued or if a commit was attempted and failed. True otherwise. |
||
289 | */ |
||
290 | 1 | public function saveDeferred(\Psr\Cache\CacheItemInterface $item) |
|
306 | |||
307 | /** |
||
308 | * Persists any deferred cache items. |
||
309 | * |
||
310 | * @return bool |
||
311 | * True if all not-yet-saved items were successfully saved or there were none. False otherwise. |
||
312 | */ |
||
313 | 1 | public function commit() |
|
326 | |||
327 | /** |
||
328 | * Saves the current items/defferedItems in the session |
||
329 | * |
||
330 | * @param bool $value If the items/defferedItems should be saved in the session |
||
331 | * |
||
332 | * @return void |
||
333 | */ |
||
334 | 1 | public function saveInSession($value) |
|
341 | |||
342 | /** |
||
343 | * Clears the current session |
||
344 | * |
||
345 | * @return void |
||
346 | */ |
||
347 | 2 | public function clearSession() |
|
356 | } |
||
357 |
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: