Complex classes like CacheData 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 CacheData, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class CacheData { |
||
10 | const CACHEDATA_TYPE_CALLBACK = 'callback'; |
||
11 | const CACHEDATA_TYPE_RECURSION = 'recursion'; |
||
12 | const CACHEDATA_TYPE_RECURSION_DATA = 'recursiondata'; |
||
13 | const CACHEDATA_TYPE_DATA = 'data'; |
||
14 | |||
15 | /** |
||
16 | * |
||
17 | * @var CacheKey |
||
18 | */ |
||
19 | public $key; |
||
20 | |||
21 | /** |
||
22 | * Lifetime: how long this cache should live. It's up to the storage implementation |
||
23 | * the details of how this will be obeyed, if at all. |
||
24 | * @var integer |
||
25 | */ |
||
26 | public $lifetime; |
||
27 | |||
28 | /** |
||
29 | * This is a storage of dependencies for this cache. This is stored here as |
||
30 | * the russian doll model will automatically push up dependencies. In the end |
||
31 | * this will probably be used by the storage for easy invalidation. |
||
32 | * |
||
33 | * @var array:CacheKey |
||
34 | */ |
||
35 | public $dependencies = array(); |
||
36 | |||
37 | /** |
||
38 | * The actual cached data |
||
39 | * @var array |
||
40 | */ |
||
41 | public $data = array(); |
||
42 | |||
43 | private $dependenciesHash = ''; |
||
44 | |||
45 | public function __construct(CacheKey $ck, $data = null) { |
||
52 | |||
53 | /** |
||
54 | * |
||
55 | * @param CacheKey $ck |
||
56 | * @return CacheData |
||
57 | */ |
||
58 | public function setKey(CacheKey $ck) { |
||
62 | |||
63 | /** |
||
64 | * |
||
65 | * @param unknown $callback |
||
66 | * @return CacheData |
||
67 | */ |
||
68 | public function appendCallback(callable $callback) { |
||
72 | |||
73 | /** |
||
74 | * |
||
75 | * @param CacheData $cd |
||
76 | * @return CacheData |
||
77 | */ |
||
78 | public function mergeDependencies(CacheData $cd) { |
||
82 | |||
83 | public function clearDependenciesHash() { |
||
86 | |||
87 | /** |
||
88 | * Checks if dependencies are still fresh. |
||
89 | * @param CacheAbstract $cache |
||
90 | * @return boolean |
||
91 | */ |
||
92 | public function checkUpdateToDate(CacheAbstract $cache) { |
||
102 | |||
103 | /** |
||
104 | * Init dependencies. If increment is not supported by backend return 0. |
||
105 | * |
||
106 | * @param CacheAbstract $cache |
||
107 | * @param CacheKey $k |
||
108 | * @return integer |
||
109 | */ |
||
110 | public function dependencyInit(CacheAbstract $cache, CacheKey $k) { |
||
118 | |||
119 | /** |
||
120 | * Get a fresh hash based on dependencies. Does not update the current hash. |
||
121 | * @param CacheAbstract $cache |
||
122 | * @return string |
||
123 | */ |
||
124 | public function generateDependenciesHash(CacheAbstract $cache) { |
||
131 | |||
132 | public function updateDependenciesHash(CacheAbstract $cache) { |
||
136 | |||
137 | public function updateDependenciesHashIfNull(CacheAbstract $cache) { |
||
143 | |||
144 | public function getKey() { |
||
147 | |||
148 | public function getDependenciesHash() { |
||
151 | |||
152 | public function getDependencies() { |
||
155 | |||
156 | /** |
||
157 | * |
||
158 | * @param mixed $data Any kind of data you want to store. usually strings. |
||
159 | * @return CacheData |
||
160 | */ |
||
161 | public function appendData($data) { |
||
167 | |||
168 | /** |
||
169 | * Convenience function. Returns the first data self::CACHEDATA_TYPE_DATA that you |
||
170 | * stored. Returns null if there is none. |
||
171 | * |
||
172 | * @return any|NULL |
||
173 | */ |
||
174 | public function getFirstData() { |
||
182 | |||
183 | public function appendRecursion(CacheKey $k) { |
||
191 | |||
192 | public function appendRecursionData(CacheData $d) { |
||
203 | |||
204 | /** |
||
205 | * Adds a dependency |
||
206 | * @param CacheKey $k |
||
207 | * @return CacheData This |
||
208 | */ |
||
209 | public function addDependency(CacheKey $k) { |
||
214 | |||
215 | /** |
||
216 | * Adds a dependency |
||
217 | * @param array<CacheKey> $k |
||
218 | * @return CacheData This |
||
219 | */ |
||
220 | public function addDependencies(array $deps) { |
||
226 | |||
227 | /** |
||
228 | * Sets the list of dependencies and updates the dependency hash |
||
229 | * |
||
230 | * @param array<CacheKey> $deps |
||
231 | * @param CacheAbstract $cache |
||
232 | * @return CacheData |
||
233 | */ |
||
234 | public function setDependencies(array $deps, CacheAbstract $cache) { |
||
242 | |||
243 | /** |
||
244 | * |
||
245 | * @param integer $lifetime |
||
246 | * @return CacheData |
||
247 | */ |
||
248 | public function setLifetime($lifetime) { |
||
252 | |||
253 | /** |
||
254 | * Checks if a set of keys clashes with the ones used here. |
||
255 | * @param CacheKey $k |
||
256 | * @return boolean True if they match and there is a clash |
||
257 | */ |
||
258 | public function checkClash(CacheKey $k) { |
||
261 | |||
262 | /** |
||
263 | * Converts this data to a string that can output. This is not a hash |
||
264 | * key or a serialization, but an actual render for humans. |
||
265 | * |
||
266 | * @throws Exceptions\NotCachedException |
||
267 | */ |
||
268 | public function stringify(CacheAbstract $c, $recurse = true) { |
||
298 | |||
299 | /** |
||
300 | * Serialize this object to a string so we can easily store. |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | public function serialize() { |
||
307 | |||
308 | /** |
||
309 | * Unserializes data and returns a new CacheData. This is what you |
||
310 | * should use to get the object data from the storage. |
||
311 | * |
||
312 | * @param string $data |
||
313 | * @return CacheData |
||
314 | */ |
||
315 | static public function unserialize($data) { |
||
318 | } |
||
319 |