Complex classes like WebDAVAdapter 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 WebDAVAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class WebDAVAdapter extends AbstractAdapter |
||
| 19 | { |
||
| 20 | use StreamedReadingTrait; |
||
| 21 | use StreamedCopyTrait { |
||
| 22 | StreamedCopyTrait::copy as streamedCopy; |
||
| 23 | } |
||
| 24 | use NotSupportingVisibilityTrait; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected static $resultMap = [ |
||
| 30 | '{DAV:}getcontentlength' => 'size', |
||
| 31 | '{DAV:}getcontenttype' => 'mimetype', |
||
| 32 | 'content-length' => 'size', |
||
| 33 | 'content-type' => 'mimetype', |
||
| 34 | ]; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Client |
||
| 38 | */ |
||
| 39 | protected $client; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | protected $useStreamedCopy = true; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructor. |
||
| 48 | * |
||
| 49 | * @param Client $client |
||
| 50 | * @param string $prefix |
||
| 51 | * @param bool $useStreamedCopy |
||
| 52 | */ |
||
| 53 | public function __construct(Client $client, $prefix = null, $useStreamedCopy = true) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * url encode a path |
||
| 62 | * |
||
| 63 | * @param string $path |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | protected function encodePath($path) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | public function getMetadata($path) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | public function has($path) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | public function read($path) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | public function write($path, $contents, Config $config) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | public function writeStream($path, $resource, Config $config) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | public function update($path, $contents, Config $config) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | public function updateStream($path, $resource, Config $config) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritdoc} |
||
| 184 | */ |
||
| 185 | public function rename($path, $newpath) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritdoc} |
||
| 207 | */ |
||
| 208 | public function copy($path, $newpath) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | public function delete($path) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | public function createDir($path, Config $config) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function deleteDir($dirname) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | public function listContents($directory = '', $recursive = false) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | */ |
||
| 305 | public function getSize($path) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * {@inheritdoc} |
||
| 312 | */ |
||
| 313 | public function getTimestamp($path) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * {@inheritdoc} |
||
| 320 | */ |
||
| 321 | public function getMimetype($path) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return boolean |
||
| 328 | */ |
||
| 329 | public function getUseStreamedCopy() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param boolean $useStreamedCopy |
||
| 336 | */ |
||
| 337 | public function setUseStreamedCopy($useStreamedCopy) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Copy a file through WebDav COPY method. |
||
| 344 | * |
||
| 345 | * @param string $path |
||
| 346 | * @param string $newPath |
||
| 347 | * |
||
| 348 | * @return bool |
||
| 349 | */ |
||
| 350 | protected function nativeCopy($path, $newPath) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Normalise a WebDAV repsonse object. |
||
| 377 | * |
||
| 378 | * @param array $object |
||
| 379 | * @param string $path |
||
| 380 | * |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | protected function normalizeObject(array $object, $path) |
||
| 400 | } |
||
| 401 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: