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 | 46 | public function __construct($cacheDir = null) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Recursively creates & chmod directories. |
||
| 60 | * |
||
| 61 | * @param string $path |
||
| 62 | * |
||
| 63 | * @return bool |
||
| 64 | */ |
||
| 65 | 46 | protected function createCacheDirectory($path): bool |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $cacheFileWithPath |
||
| 114 | * |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | 15 | protected function deleteFile($cacheFileWithPath): bool |
|
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | 7 | 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 | 15 | public function remove(string $key): bool |
|
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritdoc} |
||
| 161 | */ |
||
| 162 | 4 | public function removeAll(): bool |
|
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | 13 | 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 | * @return void |
||
| 209 | */ |
||
| 210 | public function setFileMode($fileMode) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param int $ttl |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | 29 | protected function ttlHasExpired(int $ttl): bool |
|
| 228 | |||
| 229 | /** |
||
| 230 | * @param mixed $data |
||
| 231 | * |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | 29 | protected function validateDataFromCache($data): bool |
|
| 235 | { |
||
| 236 | 29 | if (!\is_array($data)) { |
|
| 237 | return false; |
||
| 238 | } |
||
| 239 | |||
| 240 | 29 | foreach (['value', 'ttl'] as $missing) { |
|
| 241 | 29 | if (!\array_key_exists($missing, $data)) { |
|
| 242 | 29 | return false; |
|
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | 29 | return true; |
|
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * copy&past from https://github.com/webimpress/safe-writer (thx @michalbundyra) |
||
| 251 | * |
||
| 252 | * @param string $file |
||
| 253 | * @param string $content |
||
| 254 | * @param int|null $chmod |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | 28 | protected function writeFile($file, $content, $chmod = null): bool |
|
| 300 | } |
||
| 301 |