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 | * @param string $cacheDir |
||
| 37 | */ |
||
| 38 | 7 | public function __construct($cacheDir = null) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * remove on cache-file |
||
| 55 | * |
||
| 56 | * @param string $key |
||
| 57 | * |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | 1 | public function remove($key) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * remove all cache-files |
||
| 69 | * |
||
| 70 | * @return bool |
||
| 71 | */ |
||
| 72 | 1 | public function removeAll() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $key |
||
| 90 | * |
||
| 91 | * @return mixed |
||
| 92 | */ |
||
| 93 | 5 | public function get($key) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param $data |
||
| 118 | * |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | 5 | protected function validateDataFromCache($data) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @param string $key |
||
| 138 | * |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | 1 | public function exists($key) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $key |
||
| 148 | * @param mixed $value |
||
| 149 | * |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | 3 | public function set($key, $value) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $key |
||
| 159 | * @param mixed $value |
||
| 160 | * @param int $ttl |
||
| 161 | * |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | 4 | public function setExpired($key, $value, $ttl = 0) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * recursively creates & chmod directories |
||
| 182 | * |
||
| 183 | * @param string $path |
||
| 184 | * |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | 7 | protected function createCacheDirectory($path) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @param $cacheFile |
||
| 236 | * |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | 1 | protected function deleteFile($cacheFile) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @param string $key |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | 6 | protected function getFileName($key) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @param $ttl |
||
| 260 | * |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | 5 | protected function ttlHasExpired($ttl) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * e.g. '0777', or '0755' ... |
||
| 274 | * |
||
| 275 | * @param $fileMode |
||
| 276 | */ |
||
| 277 | public function setFileMode($fileMode) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * check if cache is installed |
||
| 284 | * |
||
| 285 | * @return boolean |
||
| 286 | */ |
||
| 287 | public function installed() |
||
| 291 | } |
||
| 292 |
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: