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 StreamedTrait; |
||
| 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 update($path, $contents, Config $config) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | public function rename($path, $newpath) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | public function copy($path, $newpath) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | */ |
||
| 204 | public function delete($path) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | public function createDir($path, Config $config) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritdoc} |
||
| 251 | */ |
||
| 252 | public function deleteDir($dirname) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritdoc} |
||
| 259 | */ |
||
| 260 | public function listContents($directory = '', $recursive = false) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritdoc} |
||
| 288 | */ |
||
| 289 | public function getSize($path) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * {@inheritdoc} |
||
| 296 | */ |
||
| 297 | public function getTimestamp($path) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | */ |
||
| 305 | public function getMimetype($path) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return boolean |
||
| 312 | */ |
||
| 313 | public function getUseStreamedCopy() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param boolean $useStreamedCopy |
||
| 320 | */ |
||
| 321 | public function setUseStreamedCopy($useStreamedCopy) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Copy a file through WebDav COPY method. |
||
| 328 | * |
||
| 329 | * @param string $path |
||
| 330 | * @param string $newPath |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | protected function nativeCopy($path, $newPath) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Normalise a WebDAV repsonse object. |
||
| 357 | * |
||
| 358 | * @param array $object |
||
| 359 | * @param string $path |
||
| 360 | * |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | protected function normalizeObject(array $object, $path) |
||
| 380 | } |
||
| 381 |
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: