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 |
||
| 17 | class WebDAVAdapter extends AbstractAdapter |
||
| 18 | { |
||
| 19 | use StreamedReadingTrait; |
||
| 20 | use StreamedCopyTrait { |
||
| 21 | StreamedCopyTrait::copy as streamedCopy; |
||
| 22 | } |
||
| 23 | use NotSupportingVisibilityTrait; |
||
| 24 | |||
| 25 | private static $metadataFields = [ |
||
| 26 | '{DAV:}displayname', |
||
| 27 | '{DAV:}getcontentlength', |
||
| 28 | '{DAV:}getcontenttype', |
||
| 29 | '{DAV:}getlastmodified', |
||
| 30 | '{DAV:}iscollection', |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected static $resultMap = [ |
||
| 37 | '{DAV:}getcontentlength' => 'size', |
||
| 38 | '{DAV:}getcontenttype' => 'mimetype', |
||
| 39 | 'content-length' => 'size', |
||
| 40 | 'content-type' => 'mimetype', |
||
| 41 | ]; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Client |
||
| 45 | */ |
||
| 46 | protected $client; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var bool |
||
| 50 | */ |
||
| 51 | protected $useStreamedCopy = true; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Constructor. |
||
| 55 | * |
||
| 56 | * @param Client $client |
||
| 57 | * @param string $prefix |
||
| 58 | * @param bool $useStreamedCopy |
||
| 59 | */ |
||
| 60 | public function __construct(Client $client, $prefix = null, $useStreamedCopy = true) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * url encode a path |
||
| 69 | * |
||
| 70 | * @param string $path |
||
| 71 | * |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | protected function encodePath($path) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | public function getMetadata($path) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * {@inheritdoc} |
||
| 103 | */ |
||
| 104 | public function has($path) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function read($path) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritdoc} |
||
| 137 | */ |
||
| 138 | public function write($path, $contents, Config $config) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | public function writeStream($path, $resource, Config $config) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | public function update($path, $contents, Config $config) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritdoc} |
||
| 178 | */ |
||
| 179 | public function updateStream($path, $resource, Config $config) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritdoc} |
||
| 186 | */ |
||
| 187 | public function rename($path, $newpath) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * {@inheritdoc} |
||
| 209 | */ |
||
| 210 | public function copy($path, $newpath) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritdoc} |
||
| 221 | */ |
||
| 222 | public function delete($path) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * {@inheritdoc} |
||
| 237 | */ |
||
| 238 | public function createDir($path, Config $config) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * {@inheritdoc} |
||
| 269 | */ |
||
| 270 | public function deleteDir($dirname) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * {@inheritdoc} |
||
| 277 | */ |
||
| 278 | public function listContents($directory = '', $recursive = false) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * {@inheritdoc} |
||
| 301 | */ |
||
| 302 | public function getSize($path) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * {@inheritdoc} |
||
| 309 | */ |
||
| 310 | public function getTimestamp($path) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * {@inheritdoc} |
||
| 317 | */ |
||
| 318 | public function getMimetype($path) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return boolean |
||
| 325 | */ |
||
| 326 | public function getUseStreamedCopy() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param boolean $useStreamedCopy |
||
| 333 | */ |
||
| 334 | public function setUseStreamedCopy($useStreamedCopy) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Copy a file through WebDav COPY method. |
||
| 341 | * |
||
| 342 | * @param string $path |
||
| 343 | * @param string $newPath |
||
| 344 | * |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | protected function nativeCopy($path, $newPath) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Normalise a WebDAV repsonse object. |
||
| 374 | * |
||
| 375 | * @param array $object |
||
| 376 | * @param string $path |
||
| 377 | * |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | protected function normalizeObject(array $object, $path) |
||
| 397 | |||
| 398 | private function isDirectory(array $object) |
||
| 402 | } |
||
| 403 |
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: