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 | * Copy a file through WebDav COPY method. |
||
312 | * |
||
313 | * @param string $path |
||
314 | * @param string $newPath |
||
315 | * |
||
316 | * @return bool |
||
317 | */ |
||
318 | protected function nativeCopy($path, $newPath) |
||
338 | |||
339 | /** |
||
340 | * Normalise a WebDAV repsonse object. |
||
341 | * |
||
342 | * @param array $object |
||
343 | * @param string $path |
||
344 | * |
||
345 | * @return array |
||
346 | */ |
||
347 | protected function normalizeObject(array $object, $path) |
||
364 | } |
||
365 |
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: