Complex classes like CachedAdapter 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 CachedAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class CachedAdapter implements AdapterInterface |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var AdapterInterface |
||
| 12 | */ |
||
| 13 | private $adapter; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var CacheInterface |
||
| 17 | */ |
||
| 18 | private $cache; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Constructor. |
||
| 22 | * |
||
| 23 | * @param AdapterInterface $adapter |
||
| 24 | * @param CacheInterface $cache |
||
| 25 | */ |
||
| 26 | 144 | public function __construct(AdapterInterface $adapter, CacheInterface $cache) |
|
| 27 | { |
||
| 28 | 144 | $this->adapter = $adapter; |
|
| 29 | 144 | $this->cache = $cache; |
|
| 30 | 144 | $this->cache->load(); |
|
| 31 | 144 | } |
|
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | 3 | public function getAdapter() |
|
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | 6 | public function write($path, $contents, Config $config) |
|
| 45 | { |
||
| 46 | 6 | $result = $this->adapter->write($path, $contents, $config); |
|
| 47 | |||
| 48 | 6 | if ($result !== false) { |
|
| 49 | 3 | $this->cache->updateObject($path, $result + compact('path', 'contents'), true); |
|
| 50 | 3 | } |
|
| 51 | |||
| 52 | 6 | return $result; |
|
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | 6 | public function writeStream($path, $resource, Config $config) |
|
| 59 | { |
||
| 60 | 6 | $result = $this->adapter->writeStream($path, $resource, $config); |
|
| 61 | |||
| 62 | 6 | if ($result !== false) { |
|
| 63 | 3 | $contents = false; |
|
| 64 | 3 | $this->cache->updateObject($path, $result + compact('path', 'contents'), true); |
|
| 65 | 3 | } |
|
| 66 | |||
| 67 | 6 | return $result; |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | */ |
||
| 73 | 6 | public function update($path, $contents, Config $config) |
|
| 74 | { |
||
| 75 | 6 | $result = $this->adapter->update($path, $contents, $config); |
|
| 76 | |||
| 77 | 6 | if ($result !== false) { |
|
| 78 | 3 | $this->cache->updateObject($path, $result + compact('path', 'contents'), true); |
|
| 79 | 3 | } |
|
| 80 | |||
| 81 | 6 | return $result; |
|
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | 6 | public function updateStream($path, $resource, Config $config) |
|
| 88 | { |
||
| 89 | 6 | $result = $this->adapter->updateStream($path, $resource, $config); |
|
| 90 | |||
| 91 | 6 | if ($result !== false) { |
|
| 92 | 3 | $contents = false; |
|
| 93 | 3 | $this->cache->updateObject($path, $result + compact('path', 'contents'), true); |
|
| 94 | 3 | } |
|
| 95 | |||
| 96 | 6 | return $result; |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | 6 | public function rename($path, $newPath) |
|
| 103 | { |
||
| 104 | 6 | $result = $this->adapter->rename($path, $newPath); |
|
| 105 | |||
| 106 | 6 | if ($result !== false) { |
|
| 107 | 3 | $this->cache->rename($path, $newPath); |
|
| 108 | 3 | } |
|
| 109 | |||
| 110 | 6 | return $result; |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | 6 | public function copy($path, $newpath) |
|
| 117 | { |
||
| 118 | 6 | $result = $this->adapter->copy($path, $newpath); |
|
| 119 | |||
| 120 | 6 | if ($result !== false) { |
|
| 121 | 3 | $this->cache->copy($path, $newpath); |
|
| 122 | 3 | } |
|
| 123 | |||
| 124 | 6 | return $result; |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | 6 | public function delete($path) |
|
| 131 | { |
||
| 132 | 6 | $result = $this->adapter->delete($path); |
|
| 133 | |||
| 134 | 6 | if ($result !== false) { |
|
| 135 | 3 | $this->cache->delete($path); |
|
| 136 | 3 | } |
|
| 137 | |||
| 138 | 6 | return $result; |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | 6 | public function deleteDir($dirname) |
|
| 145 | { |
||
| 146 | 6 | $result = $this->adapter->deleteDir($dirname); |
|
| 147 | |||
| 148 | 6 | if ($result !== false) { |
|
| 149 | 3 | $this->cache->deleteDir($dirname); |
|
| 150 | 3 | } |
|
| 151 | |||
| 152 | 6 | return $result; |
|
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | 6 | public function createDir($dirname, Config $config) |
|
| 159 | { |
||
| 160 | 6 | $result = $this->adapter->createDir($dirname, $config); |
|
| 161 | |||
| 162 | 6 | if ($result !== false) { |
|
| 163 | 3 | $type = 'dir'; |
|
| 164 | 3 | $path = $dirname; |
|
| 165 | 3 | $this->cache->updateObject($dirname, compact('path', 'type'), true); |
|
| 166 | 3 | } |
|
| 167 | |||
| 168 | 6 | return $result; |
|
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritdoc} |
||
| 173 | */ |
||
| 174 | 6 | public function setVisibility($path, $visibility) |
|
| 175 | { |
||
| 176 | 6 | $result = $this->adapter->setVisibility($path, $visibility); |
|
| 177 | |||
| 178 | 6 | if ($result !== false) { |
|
| 179 | 3 | $this->cache->updateObject($path, compact('path', 'visibility'), true); |
|
| 180 | 3 | } |
|
| 181 | |||
| 182 | 6 | return $result; |
|
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | 12 | public function has($path) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * {@inheritdoc} |
||
| 210 | */ |
||
| 211 | 9 | public function read($path) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritdoc} |
||
| 218 | */ |
||
| 219 | 3 | public function readStream($path) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | 9 | public function listContents($directory = '', $recursive = false) |
|
| 228 | { |
||
| 229 | 9 | if ($this->cache->isComplete($directory, $recursive)) { |
|
| 230 | 3 | return $this->cache->listContents($directory, $recursive); |
|
| 231 | } |
||
| 232 | |||
| 233 | 6 | $result = $this->adapter->listContents($directory, $recursive); |
|
| 234 | |||
| 235 | 6 | if ($result) { |
|
|
|
|||
| 236 | 3 | $this->cache->storeContents($directory, $result, $recursive); |
|
| 237 | 3 | } |
|
| 238 | |||
| 239 | 6 | return $result; |
|
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * {@inheritdoc} |
||
| 244 | */ |
||
| 245 | 9 | public function getMetadata($path) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | 9 | public function getSize($path) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | 9 | public function getMimetype($path) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritdoc} |
||
| 268 | */ |
||
| 269 | 9 | public function getTimestamp($path) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * {@inheritdoc} |
||
| 276 | */ |
||
| 277 | 9 | public function getVisibility($path) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Call a method and cache the response. |
||
| 284 | * |
||
| 285 | * @param string $method |
||
| 286 | * @param string $path |
||
| 287 | * |
||
| 288 | * @return mixed |
||
| 289 | */ |
||
| 290 | 54 | protected function callWithFallback($method, $path) |
|
| 291 | { |
||
| 292 | 54 | $result = $this->cache->{$method}($path); |
|
| 293 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * Call any method for plugin features. |
||
| 310 | * |
||
| 311 | * @param array $arguments [ $path ] |
||
| 312 | * |
||
| 313 | * @return array|false |
||
| 314 | */ |
||
| 315 | public function __call($method, $arguments) |
||
| 323 | } |
||
| 324 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.