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 |
||
| 12 | class AdapterFile implements iAdapter |
||
| 13 | { |
||
| 14 | const CACHE_FILE_PREFIX = '__'; |
||
| 15 | const CACHE_FILE_SUBFIX = '.php.cache'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var bool |
||
| 19 | */ |
||
| 20 | public $installed = false; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $cacheDir; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var iSerializer |
||
| 29 | */ |
||
| 30 | protected $serializer; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $fileMode = '0755'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param string $cacheDir |
||
| 39 | */ |
||
| 40 | 11 | public function __construct($cacheDir = null) |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Recursively creates & chmod directories. |
||
| 57 | * |
||
| 58 | * @param string $path |
||
| 59 | * |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | 11 | protected function createCacheDirectory($path): bool |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param $cacheFile |
||
| 111 | * |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | 3 | protected function deleteFile($cacheFile): bool |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @inheritdoc |
||
| 125 | */ |
||
| 126 | 1 | public function exists(string $key): bool |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @inheritdoc |
||
| 135 | */ |
||
| 136 | 7 | public function get(string $key) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @inheritdoc |
||
| 185 | */ |
||
| 186 | public function installed(): bool |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @inheritdoc |
||
| 193 | */ |
||
| 194 | 3 | public function remove(string $key): bool |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @inheritdoc |
||
| 203 | */ |
||
| 204 | 1 | public function removeAll(): bool |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @inheritdoc |
||
| 222 | */ |
||
| 223 | 3 | public function set(string $key, $value): bool |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @inheritdoc |
||
| 230 | */ |
||
| 231 | 6 | public function setExpired(string $key, $value, int $ttl = 0): bool |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $key |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | 8 | protected function getFileName(string $key): string |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Set the file-mode for new cache-files. |
||
| 268 | * |
||
| 269 | * e.g. '0777', or '0755' ... |
||
| 270 | * |
||
| 271 | * @param $fileMode |
||
| 272 | */ |
||
| 273 | public function setFileMode($fileMode) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param $ttl |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | 7 | protected function ttlHasExpired(int $ttl): bool |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @param mixed $data |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | 7 | protected function validateDataFromCache($data): bool |
|
| 311 | } |
||
| 312 |
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: