Complex classes like AdapterFile 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 AdapterFile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class AdapterFile implements iAdapter |
||
| 11 | { |
||
| 12 | const CACHE_FILE_PREFIX = '__'; |
||
| 13 | const CACHE_FILE_SUBFIX = '.php.cache'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var bool |
||
| 17 | */ |
||
| 18 | public $installed = false; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $cacheDir; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var iSerializer |
||
| 27 | */ |
||
| 28 | protected $serializer; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $fileMode = '0755'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $internalFileValueCache = array(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param string $cacheDir |
||
| 42 | */ |
||
| 43 | 9 | public function __construct($cacheDir = null) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Recursively creates & chmod directories. |
||
| 60 | * |
||
| 61 | * @param string $path |
||
| 62 | * |
||
| 63 | * @return bool |
||
| 64 | */ |
||
| 65 | 9 | protected function createCacheDirectory($path) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @param $cacheFile |
||
| 114 | * |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | 1 | protected function deleteFile($cacheFile) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $key |
||
| 128 | * |
||
| 129 | * @return mixed <p>Will return null if the was no value, otherwise it will return the cache value.</p> |
||
| 130 | */ |
||
| 131 | 5 | protected function getInternalFileValueCache($key) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @param string $key |
||
| 142 | * @param mixed $value |
||
| 143 | */ |
||
| 144 | 1 | protected function setInternalFileValueCache($key, $value) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $key |
||
| 151 | */ |
||
| 152 | 1 | protected function removeInternalFileValueCache($key) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @inheritdoc |
||
| 159 | */ |
||
| 160 | 1 | public function exists($key) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @inheritdoc |
||
| 171 | */ |
||
| 172 | 5 | public function get($key) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @inheritdoc |
||
| 224 | */ |
||
| 225 | public function installed() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @inheritdoc |
||
| 232 | */ |
||
| 233 | 1 | public function remove($key) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @inheritdoc |
||
| 244 | */ |
||
| 245 | 1 | public function removeAll() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @inheritdoc |
||
| 265 | */ |
||
| 266 | 3 | public function set($key, $value) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @inheritdoc |
||
| 273 | */ |
||
| 274 | 4 | public function setExpired($key, $value, $ttl = 0) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $key |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | 6 | protected function getFileName($key) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Set the file-mode for new cache-files. |
||
| 311 | * |
||
| 312 | * e.g. '0777', or '0755' ... |
||
| 313 | * |
||
| 314 | * @param $fileMode |
||
| 315 | */ |
||
| 316 | public function setFileMode($fileMode) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param $ttl |
||
| 323 | * |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | 5 | protected function ttlHasExpired($ttl) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @param $data |
||
| 337 | * |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | 5 | protected function validateDataFromCache($data) |
|
| 354 | } |
||
| 355 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: