Complex classes like CacheManager 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 CacheManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class CacheManager |
||
50 | { |
||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | public static $ReadHits = 0; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | public static $WriteHits = 0; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected static $config = [ |
||
65 | /** |
||
66 | * Specify if the item must provide detailed creation/modification dates |
||
67 | */ |
||
68 | 'itemDetailedDate' => false, |
||
69 | |||
70 | /** |
||
71 | * Automatically attempt to fallback to temporary directory |
||
72 | * if the cache fails to write on the specified directory |
||
73 | */ |
||
74 | 'autoTmpFallback' => false, |
||
75 | |||
76 | /** |
||
77 | * Provide a secure file manipulation mechanism, |
||
78 | * on intensive usage the performance can be affected. |
||
79 | */ |
||
80 | 'secureFileManipulation' => false, |
||
81 | |||
82 | /** |
||
83 | * Ignore Symfony notice for Symfony project which |
||
84 | * do not makes use of PhpFastCache's Symfony Bundle |
||
85 | */ |
||
86 | 'ignoreSymfonyNotice' => false, |
||
87 | |||
88 | /** |
||
89 | * Default time-to-live in second |
||
90 | */ |
||
91 | 'defaultTtl' => 900, |
||
92 | |||
93 | /** |
||
94 | * Default key hash function |
||
95 | * (md5 by default) |
||
96 | */ |
||
97 | 'defaultKeyHashFunction' => '', |
||
98 | |||
99 | /** |
||
100 | * The securityKey that will be used |
||
101 | * to create sub-directory |
||
102 | * (Files-based drivers only) |
||
103 | */ |
||
104 | 'securityKey' => 'Auto', |
||
105 | |||
106 | /** |
||
107 | * Auto-generate .htaccess if it's missing |
||
108 | * (Files-based drivers only) |
||
109 | */ |
||
110 | 'htaccess' => true, |
||
111 | |||
112 | /** |
||
113 | * Default files chmod |
||
114 | * 0777 recommended |
||
115 | * (Files-based drivers only) |
||
116 | */ |
||
117 | 'default_chmod' => 0777, |
||
118 | |||
119 | /** |
||
120 | * The path where we will writecache files |
||
121 | * default value if empty: sys_get_temp_dir() |
||
122 | * (Files-based drivers only) |
||
123 | */ |
||
124 | 'path' => '', |
||
125 | |||
126 | /** |
||
127 | * Driver fallback in case of failure. |
||
128 | * Caution, in case of failure an E_WARNING |
||
129 | * error will always be raised |
||
130 | */ |
||
131 | 'fallback' => false, |
||
132 | |||
133 | /** |
||
134 | * Maximum size (bytes) of object store in memory |
||
135 | * (Memcache(d) drivers only) |
||
136 | */ |
||
137 | 'limited_memory_each_object' => 4096, |
||
138 | |||
139 | /** |
||
140 | * Compress stored data, if the backend supports it |
||
141 | * (Memcache(d) drivers only) |
||
142 | */ |
||
143 | 'compress_data' => false, |
||
144 | |||
145 | /** |
||
146 | * Prevent cache slams when |
||
147 | * making use of heavy cache |
||
148 | * items |
||
149 | */ |
||
150 | 'preventCacheSlams' => false, |
||
151 | |||
152 | /** |
||
153 | * Cache slams timeout |
||
154 | * in seconds |
||
155 | */ |
||
156 | 'cacheSlamsTimeout' => 15, |
||
157 | |||
158 | /** |
||
159 | * Cache slams timeout |
||
160 | * in seconds |
||
161 | */ |
||
162 | 'cacheFileExtension' => 'txt', |
||
163 | |||
164 | ]; |
||
165 | |||
166 | /** |
||
167 | * Feel free to propose your own one |
||
168 | * by opening a pull request :) |
||
169 | * @var array |
||
170 | */ |
||
171 | protected static $safeFileExtensions = [ |
||
172 | 'txt', |
||
173 | 'cache', |
||
174 | 'db', |
||
175 | 'pfc', |
||
176 | ]; |
||
177 | |||
178 | /** |
||
179 | * @var string |
||
180 | */ |
||
181 | protected static $namespacePath; |
||
182 | |||
183 | /** |
||
184 | * @var ExtendedCacheItemPoolInterface[] |
||
185 | */ |
||
186 | protected static $instances = []; |
||
187 | |||
188 | /** |
||
189 | * @param string $driver |
||
190 | * @param array $config |
||
191 | * @return ExtendedCacheItemPoolInterface |
||
192 | * @throws phpFastCacheDriverCheckException |
||
193 | * @throws phpFastCacheInvalidConfigurationException |
||
194 | */ |
||
195 | public static function getInstance($driver = 'auto', array $config = []) |
||
249 | |||
250 | /** |
||
251 | * This method is intended for internal |
||
252 | * use only and should not be used for |
||
253 | * any external development use the |
||
254 | * getInstances() method instead |
||
255 | * |
||
256 | * @internal |
||
257 | * @return ExtendedCacheItemPoolInterface[] |
||
258 | */ |
||
259 | public static function getInstances() |
||
263 | |||
264 | /** |
||
265 | * This method is intended for internal |
||
266 | * use only and should not be used for |
||
267 | * any external development use the |
||
268 | * getInstances() method instead |
||
269 | * |
||
270 | * @internal |
||
271 | * @return ExtendedCacheItemPoolInterface[] |
||
272 | */ |
||
273 | public static function &getInternalInstances() |
||
277 | |||
278 | /** |
||
279 | * @param $config |
||
280 | * @return string |
||
281 | * @throws phpFastCacheDriverCheckException |
||
282 | */ |
||
283 | public static function getAutoClass(array $config = []) |
||
300 | |||
301 | /** |
||
302 | * @param string $name |
||
303 | * @param array $arguments |
||
304 | * @return \Psr\Cache\CacheItemPoolInterface |
||
305 | */ |
||
306 | public static function __callStatic($name, $arguments) |
||
312 | |||
313 | /** |
||
314 | * @return bool |
||
315 | */ |
||
316 | public static function clearInstances() |
||
323 | |||
324 | /** |
||
325 | * @return string |
||
326 | */ |
||
327 | public static function getNamespacePath() |
||
331 | |||
332 | /** |
||
333 | * @param string $path |
||
334 | */ |
||
335 | public static function setNamespacePath($path) |
||
339 | |||
340 | /** |
||
341 | * @param $name string|array |
||
342 | * @param mixed $value |
||
343 | * @throws phpFastCacheInvalidArgumentException |
||
344 | */ |
||
345 | public static function setDefaultConfig($name, $value = null) |
||
346 | { |
||
347 | if (is_array($name)) { |
||
348 | self::$config = array_merge(self::$config, $name); |
||
349 | } else if (is_string($name)) { |
||
350 | self::$config[ $name ] = $value; |
||
351 | } else { |
||
352 | throw new phpFastCacheInvalidArgumentException('Invalid variable type: $name'); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @param $name string|array |
||
358 | * @param mixed $value |
||
359 | * @throws phpFastCacheInvalidConfigurationException |
||
360 | * @deprecated Method "setup" is deprecated, please use "setDefaultConfig" method instead |
||
361 | */ |
||
362 | public static function setup($name, $value = null) |
||
366 | |||
367 | |||
368 | /** |
||
369 | * @return array |
||
370 | */ |
||
371 | public static function getDefaultConfig() |
||
375 | |||
376 | /** |
||
377 | * @return array |
||
378 | */ |
||
379 | public static function getStaticSystemDrivers() |
||
404 | |||
405 | /** |
||
406 | * @return array |
||
407 | */ |
||
408 | public static function getStaticAllDrivers() |
||
416 | |||
417 | /** |
||
418 | * @param $driverName |
||
419 | * @return string |
||
420 | * @throws \phpFastCache\Exceptions\phpFastCacheInvalidArgumentException |
||
421 | */ |
||
422 | public static function standardizeDriverName($driverName) |
||
429 | |||
430 | /** |
||
431 | * @param array $config |
||
432 | * @todo Move this to a config file |
||
433 | * @throws phpFastCacheInvalidConfigurationException |
||
434 | * @return bool |
||
435 | */ |
||
436 | protected static function validateConfig(array $config) |
||
495 | } |
||
496 |