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 | * @var string|null; |
||
48 | */ |
||
49 | private $urlPrefix = null; |
||
50 | |||
51 | /** |
||
52 | * Constructor. |
||
53 | * |
||
54 | * @param Client $client |
||
55 | * @param string $prefix |
||
56 | * @param bool $useStreamedCopy |
||
57 | */ |
||
58 | public function __construct(Client $client, $prefix = null, $useStreamedCopy = true) |
||
64 | |||
65 | /** |
||
66 | * url encode a path |
||
67 | * |
||
68 | * @param string $path |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | protected function encodePath($path) |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function getMetadata($path) |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function has($path) |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public function read($path) |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | public function write($path, $contents, Config $config) |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function update($path, $contents, Config $config) |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | public function rename($path, $newpath) |
||
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | public function copy($path, $newpath) |
||
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | */ |
||
209 | public function delete($path) |
||
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | public function createDir($path, Config $config) |
||
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | public function deleteDir($dirname) |
||
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | */ |
||
265 | public function listContents($directory = '', $recursive = false) |
||
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | public function getSize($path) |
||
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | */ |
||
302 | public function getTimestamp($path) |
||
306 | |||
307 | /** |
||
308 | * {@inheritdoc} |
||
309 | */ |
||
310 | public function getMimetype($path) |
||
314 | |||
315 | /** |
||
316 | * @return boolean |
||
317 | */ |
||
318 | public function getUseStreamedCopy() |
||
322 | |||
323 | /** |
||
324 | * @param boolean $useStreamedCopy |
||
325 | */ |
||
326 | public function setUseStreamedCopy($useStreamedCopy) |
||
330 | |||
331 | /** |
||
332 | * Copy a file through WebDav COPY method. |
||
333 | * |
||
334 | * @param string $path |
||
335 | * @param string $newPath |
||
336 | * |
||
337 | * @return bool |
||
338 | */ |
||
339 | protected function nativeCopy($path, $newPath) |
||
363 | |||
364 | /** |
||
365 | * Normalise a WebDAV repsonse object. |
||
366 | * |
||
367 | * @param array $object |
||
368 | * @param string $path |
||
369 | * |
||
370 | * @return array |
||
371 | */ |
||
372 | protected function normalizeObject(array $object, $path) |
||
389 | |||
390 | /** |
||
391 | * Get the URL for the file at the given path. |
||
392 | * |
||
393 | * @param string $path |
||
394 | * @return string |
||
395 | */ |
||
396 | public function getUrl($path) |
||
403 | |||
404 | /** |
||
405 | * Get the custom URL prefix for URL generation |
||
406 | * |
||
407 | * @return string|null |
||
408 | */ |
||
409 | public function getUrlPrefix() |
||
413 | |||
414 | /** |
||
415 | * Set the custom URL prefix for URL generation. |
||
416 | * |
||
417 | * If the prefix is not set, the baseUri of DAV client is used. |
||
418 | * |
||
419 | * @param string|null $prefix |
||
420 | */ |
||
421 | public function setUrlPrefix($prefix) |
||
425 | } |
||
426 |
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: