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 |
||
48 | class CacheManager |
||
49 | { |
||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | public static $ReadHits = 0; |
||
54 | |||
55 | /** |
||
56 | * @var int |
||
57 | */ |
||
58 | public static $WriteHits = 0; |
||
59 | |||
60 | /** |
||
61 | * @var ExtendedCacheItemPoolInterface[] |
||
62 | */ |
||
63 | protected static $config = [ |
||
64 | /** |
||
65 | * Specify if the item must provide detailed creation/modification dates |
||
66 | */ |
||
67 | 'itemDetailedDate' => false, |
||
68 | |||
69 | /** |
||
70 | * Automatically attempt to fallback to temporary directory |
||
71 | * if the cache fails to write on the specified directory |
||
72 | */ |
||
73 | 'autoTmpFallback' => false, |
||
74 | |||
75 | /** |
||
76 | * Provide a secure file manipulation mechanism, |
||
77 | * on intensive usage the performance can be affected. |
||
78 | */ |
||
79 | 'secureFileManipulation' => false, |
||
80 | |||
81 | /** |
||
82 | * Ignore Symfony notice for Symfony project which |
||
83 | * do not makes use of PhpFastCache's Symfony Bundle |
||
84 | */ |
||
85 | 'ignoreSymfonyNotice' => false, |
||
86 | |||
87 | /** |
||
88 | * Default time-to-live in second |
||
89 | */ |
||
90 | 'defaultTtl' => 900, |
||
91 | |||
92 | /** |
||
93 | * Default key hash function |
||
94 | * (md5 by default) |
||
95 | */ |
||
96 | 'defaultKeyHashFunction' => '', |
||
97 | |||
98 | /** |
||
99 | * The securityKey that will be used |
||
100 | * to create sub-directory |
||
101 | * (Files-based drivers only) |
||
102 | */ |
||
103 | 'securityKey' => 'Auto', |
||
104 | |||
105 | /** |
||
106 | * Auto-generate .htaccess if it's missing |
||
107 | * (Files-based drivers only) |
||
108 | */ |
||
109 | 'htaccess' => true, |
||
110 | |||
111 | /** |
||
112 | * Default files chmod |
||
113 | * 0777 recommended |
||
114 | * (Files-based drivers only) |
||
115 | */ |
||
116 | 'default_chmod' => 0777, |
||
117 | |||
118 | /** |
||
119 | * The path where we will writecache files |
||
120 | * default value if empty: sys_get_temp_dir() |
||
121 | * (Files-based drivers only) |
||
122 | */ |
||
123 | 'path' => '', |
||
124 | |||
125 | /** |
||
126 | * Driver fallback in case of failure. |
||
127 | * Caution, in case of failure an E_WARNING |
||
128 | * error will always be raised |
||
129 | */ |
||
130 | 'fallback' => false, |
||
131 | |||
132 | /** |
||
133 | * Maximum size (bytes) of object store in memory |
||
134 | * (Memcache(d) drivers only) |
||
135 | */ |
||
136 | 'limited_memory_each_object' => 4096, |
||
137 | |||
138 | /** |
||
139 | * Compress stored data, if the backend supports it |
||
140 | * (Memcache(d) drivers only) |
||
141 | */ |
||
142 | 'compress_data' => false, |
||
143 | |||
144 | /** |
||
145 | * Prevent cache slams when |
||
146 | * making use of heavy cache |
||
147 | * items |
||
148 | */ |
||
149 | 'preventCacheSlams' => false, |
||
150 | |||
151 | /** |
||
152 | * Cache slams timeout |
||
153 | * in seconds |
||
154 | */ |
||
155 | 'cacheSlamsTimeout' => 15, |
||
156 | |||
157 | /** |
||
158 | * Cache slams timeout |
||
159 | * in seconds |
||
160 | */ |
||
161 | 'cacheFileExtension' => 'txt', |
||
162 | |||
163 | ]; |
||
164 | |||
165 | /** |
||
166 | * Feel free to propose your own one |
||
167 | * by opening a pull request :) |
||
168 | * @var array |
||
169 | */ |
||
170 | protected static $safeFileExtensions = [ |
||
171 | 'txt', 'cache', 'db', 'pfc' |
||
172 | ]; |
||
173 | |||
174 | /** |
||
175 | * @var string |
||
176 | */ |
||
177 | protected static $namespacePath; |
||
178 | |||
179 | /** |
||
180 | * @var ExtendedCacheItemPoolInterface[] |
||
181 | */ |
||
182 | protected static $instances = []; |
||
183 | |||
184 | /** |
||
185 | * @param string $driver |
||
186 | * @param array $config |
||
187 | * @return ExtendedCacheItemPoolInterface |
||
188 | * @throws phpFastCacheDriverCheckException |
||
189 | * @throws phpFastCacheInvalidConfigurationException |
||
190 | */ |
||
191 | public static function getInstance($driver = 'auto', array $config = []) |
||
243 | |||
244 | /** |
||
245 | * This method is intended for internal |
||
246 | * use only and should not be used for |
||
247 | * any external development use the |
||
248 | * getInstances() method instead |
||
249 | * |
||
250 | * @internal |
||
251 | * @return ExtendedCacheItemPoolInterface[] |
||
252 | */ |
||
253 | public static function getInstances() |
||
257 | |||
258 | /** |
||
259 | * This method is intended for internal |
||
260 | * use only and should not be used for |
||
261 | * any external development use the |
||
262 | * getInstances() method instead |
||
263 | * |
||
264 | * @internal |
||
265 | * @return ExtendedCacheItemPoolInterface[] |
||
266 | */ |
||
267 | public static function &getInternalInstances() |
||
271 | |||
272 | /** |
||
273 | * @param $config |
||
274 | * @return string |
||
275 | * @throws phpFastCacheDriverCheckException |
||
276 | */ |
||
277 | public static function getAutoClass(array $config = []) |
||
294 | |||
295 | /** |
||
296 | * @param string $name |
||
297 | * @param array $arguments |
||
298 | * @return \Psr\Cache\CacheItemPoolInterface |
||
299 | */ |
||
300 | public static function __callStatic($name, $arguments) |
||
306 | |||
307 | /** |
||
308 | * @return bool |
||
309 | */ |
||
310 | public static function clearInstances() |
||
317 | |||
318 | /** |
||
319 | * @return string |
||
320 | */ |
||
321 | public static function getNamespacePath() |
||
325 | |||
326 | /** |
||
327 | * @param string $path |
||
328 | */ |
||
329 | public static function setNamespacePath($path) |
||
333 | |||
334 | /** |
||
335 | * @param $name string|array |
||
336 | * @param mixed $value |
||
337 | * @throws phpFastCacheInvalidArgumentException |
||
338 | */ |
||
339 | public static function setDefaultConfig($name, $value = null) |
||
349 | |||
350 | /** |
||
351 | * @param $name string|array |
||
352 | * @param mixed $value |
||
353 | * @throws phpFastCacheInvalidConfigurationException |
||
354 | * @deprecated Method "setup" is deprecated, please use "setDefaultConfig" method instead |
||
355 | */ |
||
356 | public static function setup($name, $value = null) |
||
360 | |||
361 | |||
362 | /** |
||
363 | * @return array |
||
364 | */ |
||
365 | public static function getDefaultConfig() |
||
369 | |||
370 | /** |
||
371 | * @return array |
||
372 | */ |
||
373 | public static function getStaticSystemDrivers() |
||
396 | |||
397 | /** |
||
398 | * @return array |
||
399 | */ |
||
400 | public static function getStaticAllDrivers() |
||
408 | |||
409 | /** |
||
410 | * @param $driverName |
||
411 | * @return string |
||
412 | * @throws \phpFastCache\Exceptions\phpFastCacheInvalidArgumentException |
||
413 | */ |
||
414 | public static function standardizeDriverName($driverName) |
||
421 | |||
422 | /** |
||
423 | * @param array $config |
||
424 | * @todo Move this to a config file |
||
425 | * @throws phpFastCacheInvalidConfigurationException |
||
426 | * @return bool |
||
427 | */ |
||
428 | protected static function validateConfig(array $config) |
||
516 | } |
||
517 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..