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 | |||
| 159 | /** |
||
| 160 | * @var string |
||
| 161 | */ |
||
| 162 | protected static $namespacePath; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var ExtendedCacheItemPoolInterface[] |
||
| 166 | */ |
||
| 167 | protected static $instances = []; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $driver |
||
| 171 | * @param array $config |
||
| 172 | * @return ExtendedCacheItemPoolInterface |
||
| 173 | * @throws phpFastCacheDriverCheckException |
||
| 174 | * @throws phpFastCacheInvalidConfigurationException |
||
| 175 | */ |
||
| 176 | public static function getInstance($driver = 'auto', array $config = []) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * This method is intended for internal |
||
| 231 | * use only and should not be used for |
||
| 232 | * any external development use the |
||
| 233 | * getInstances() method instead |
||
| 234 | * |
||
| 235 | * @internal |
||
| 236 | * @return ExtendedCacheItemPoolInterface[] |
||
| 237 | */ |
||
| 238 | public static function getInstances() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * This method is intended for internal |
||
| 245 | * use only and should not be used for |
||
| 246 | * any external development use the |
||
| 247 | * getInstances() method instead |
||
| 248 | * |
||
| 249 | * @internal |
||
| 250 | * @return ExtendedCacheItemPoolInterface[] |
||
| 251 | */ |
||
| 252 | public static function &getInternalInstances() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param $config |
||
| 259 | * @return string |
||
| 260 | * @throws phpFastCacheDriverCheckException |
||
| 261 | */ |
||
| 262 | public static function getAutoClass(array $config = []) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $name |
||
| 282 | * @param array $arguments |
||
| 283 | * @return \Psr\Cache\CacheItemPoolInterface |
||
| 284 | */ |
||
| 285 | public static function __callStatic($name, $arguments) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | public static function clearInstances() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public static function getNamespacePath() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param string $path |
||
| 313 | */ |
||
| 314 | public static function setNamespacePath($path) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param $name string|array |
||
| 321 | * @param mixed $value |
||
| 322 | * @throws phpFastCacheInvalidArgumentException |
||
| 323 | */ |
||
| 324 | public static function setDefaultConfig($name, $value = null) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param $name string|array |
||
| 337 | * @param mixed $value |
||
| 338 | * @throws phpFastCacheInvalidConfigurationException |
||
| 339 | * @deprecated Method "setup" is deprecated, please use "setDefaultConfig" method instead |
||
| 340 | */ |
||
| 341 | public static function setup($name, $value = null) |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | public static function getDefaultConfig() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | public static function getStaticSystemDrivers() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @return array |
||
| 384 | */ |
||
| 385 | public static function getStaticAllDrivers() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param $driverName |
||
| 396 | * @return string |
||
| 397 | * @throws \phpFastCache\Exceptions\phpFastCacheInvalidArgumentException |
||
| 398 | */ |
||
| 399 | public static function standardizeDriverName($driverName) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param array $config |
||
| 409 | * @todo Move this to a config file |
||
| 410 | * @throws phpFastCacheInvalidConfigurationException |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | protected static function validateConfig(array $config) |
||
| 488 | } |
||
| 489 |
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..