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 | 11 | public function __construct($cacheDir = null) |
|
| 39 | { |
||
| 40 | 11 | $this->serializer = new SerializerIgbinary(); |
|
| 41 | |||
| 42 | 11 | if (!$cacheDir) { |
|
|
|
|||
| 43 | 11 | $cacheDir = realpath(sys_get_temp_dir()) . '/simple_php_cache'; |
|
| 44 | } |
||
| 45 | |||
| 46 | 11 | $this->cacheDir = (string)$cacheDir; |
|
| 47 | |||
| 48 | 11 | if ($this->createCacheDirectory($cacheDir) === true) { |
|
| 49 | 11 | $this->installed = true; |
|
| 50 | } |
||
| 51 | 11 | } |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Recursively creates & chmod directories. |
||
| 55 | * |
||
| 56 | * @param string $path |
||
| 57 | * |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | 11 | protected function createCacheDirectory($path) |
|
| 61 | { |
||
| 62 | if ( |
||
| 63 | 11 | !$path |
|
| 64 | || |
||
| 65 | 11 | $path === '/' |
|
| 66 | || |
||
| 67 | 11 | $path === '.' |
|
| 68 | || |
||
| 69 | 11 | $path === '\\' |
|
| 70 | ) { |
||
| 71 | return false; |
||
| 72 | } |
||
| 73 | |||
| 74 | // if the directory already exists, just return true |
||
| 75 | 11 | if (is_dir($path) && is_writable($path)) { |
|
| 76 | 11 | return true; |
|
| 77 | } |
||
| 78 | |||
| 79 | // if more than one level, try parent first |
||
| 80 | 1 | if (dirname($path) !== '.') { |
|
| 81 | 1 | $return = $this->createCacheDirectory(dirname($path)); |
|
| 82 | // if creating parent fails, we can abort immediately |
||
| 83 | 1 | if (!$return) { |
|
| 84 | return false; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | 1 | $mode_dec = intval($this->fileMode, 8); |
|
| 89 | 1 | $old_umask = umask(0); |
|
| 90 | |||
| 91 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
||
| 92 | 1 | if (!@mkdir($path, $mode_dec) && !is_dir($path)) { |
|
| 93 | $return = false; |
||
| 94 | } else { |
||
| 95 | 1 | $return = true; |
|
| 96 | } |
||
| 97 | |||
| 98 | 1 | if (is_dir($path) && !is_writable($path)) { |
|
| 99 | $return = chmod($path, $mode_dec); |
||
| 100 | } |
||
| 101 | |||
| 102 | 1 | umask($old_umask); |
|
| 103 | |||
| 104 | 1 | return $return; |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param $cacheFile |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | 3 | protected function deleteFile($cacheFile) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @inheritdoc |
||
| 123 | */ |
||
| 124 | 1 | public function exists($key) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @inheritdoc |
||
| 133 | */ |
||
| 134 | 7 | public function get($key) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @inheritdoc |
||
| 181 | */ |
||
| 182 | public function installed() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @inheritdoc |
||
| 189 | */ |
||
| 190 | 3 | public function remove($key) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @inheritdoc |
||
| 199 | */ |
||
| 200 | 1 | public function removeAll() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @inheritdoc |
||
| 218 | */ |
||
| 219 | 3 | public function set($key, $value) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @inheritdoc |
||
| 226 | */ |
||
| 227 | 6 | public function setExpired($key, $value, $ttl = 0) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $key |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | 8 | protected function getFileName($key) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Set the file-mode for new cache-files. |
||
| 264 | * |
||
| 265 | * e.g. '0777', or '0755' ... |
||
| 266 | * |
||
| 267 | * @param $fileMode |
||
| 268 | */ |
||
| 269 | public function setFileMode($fileMode) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param $ttl |
||
| 276 | * |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | 7 | protected function ttlHasExpired($ttl) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param $data |
||
| 290 | * |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | 7 | protected function validateDataFromCache($data) |
|
| 307 | } |
||
| 308 |
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: