Complex classes like AdapterFileAbstract 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 AdapterFileAbstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | abstract class AdapterFileAbstract implements iAdapter |
||
| 11 | { |
||
| 12 | const CACHE_FILE_PREFIX = '__'; |
||
| 13 | |||
| 14 | const CACHE_FILE_SUBFIX = '.php.cache'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var bool |
||
| 18 | */ |
||
| 19 | public $installed = false; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $cacheDir; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var iSerializer |
||
| 28 | */ |
||
| 29 | protected $serializer; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $fileMode = '0755'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param callable|string|null $cacheDir |
||
| 38 | */ |
||
| 39 | 34 | public function __construct($cacheDir = null) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Recursively creates & chmod directories. |
||
| 60 | * |
||
| 61 | * @param string $path |
||
| 62 | * |
||
| 63 | * @return bool |
||
| 64 | */ |
||
| 65 | 34 | protected function createCacheDirectory($path): bool |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $cacheFileWithPath |
||
| 114 | * |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | 12 | protected function deleteFile($cacheFileWithPath): bool |
|
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | 6 | public function exists(string $key): bool |
|
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | */ |
||
| 139 | abstract public function get(string $key); |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | 1 | public function installed(): bool |
|
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | 12 | public function remove(string $key): bool |
|
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritdoc} |
||
| 161 | */ |
||
| 162 | 3 | public function removeAll(): bool |
|
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | 9 | public function set(string $key, $value): bool |
|
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritdoc} |
||
| 188 | */ |
||
| 189 | abstract public function setExpired(string $key, $value, int $ttl = 0): bool; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $key |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | 16 | protected function getFileName(string $key): string |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Set the file-mode for new cache-files. |
||
| 203 | * |
||
| 204 | * e.g. '0777', or '0755' ... |
||
| 205 | * |
||
| 206 | * @param string $fileMode |
||
| 207 | */ |
||
| 208 | public function setFileMode($fileMode) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param int $ttl |
||
| 215 | * |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | 22 | protected function ttlHasExpired(int $ttl): bool |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @param mixed $data |
||
| 229 | * |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | 22 | protected function validateDataFromCache($data): bool |
|
| 246 | |||
| 247 | /** |
||
| 248 | * copy&past from https://github.com/webimpress/safe-writer (thx @michalbundyra) |
||
| 249 | * |
||
| 250 | * @param string $file |
||
| 251 | * @param string $content |
||
| 252 | * @param int|null $chmod |
||
| 253 | * |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | 21 | protected function writeFile($file, $content, $chmod = null): bool |
|
| 298 | } |
||
| 299 |