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 | protected static $metadataFields = [ |
||
27 | '{DAV:}displayname', |
||
28 | '{DAV:}getcontentlength', |
||
29 | '{DAV:}getcontenttype', |
||
30 | '{DAV:}getlastmodified', |
||
31 | '{DAV:}iscollection', |
||
32 | '{DAV:}resourcetype', |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected static $resultMap = [ |
||
39 | '{DAV:}getcontentlength' => 'size', |
||
40 | '{DAV:}getcontenttype' => 'mimetype', |
||
41 | 'content-length' => 'size', |
||
42 | 'content-type' => 'mimetype', |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * @var Client |
||
47 | */ |
||
48 | protected $client; |
||
49 | |||
50 | /** |
||
51 | * @var bool |
||
52 | */ |
||
53 | protected $useStreamedCopy = true; |
||
54 | |||
55 | /** |
||
56 | * Constructor. |
||
57 | * |
||
58 | * @param Client $client |
||
59 | * @param string $prefix |
||
60 | * @param bool $useStreamedCopy |
||
61 | */ |
||
62 | public function __construct(Client $client, $prefix = null, $useStreamedCopy = true) |
||
68 | |||
69 | /** |
||
70 | * url encode a path |
||
71 | * |
||
72 | * @param string $path |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | protected function encodePath($path) |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function getMetadata($path) |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function has($path) |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function read($path) |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function write($path, $contents, Config $config) |
||
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | public function writeStream($path, $resource, Config $config) |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | public function update($path, $contents, Config $config) |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function updateStream($path, $resource, Config $config) |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | public function rename($path, $newpath) |
||
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | public function copy($path, $newpath) |
||
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | public function delete($path) |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | public function createDir($path, Config $config) |
||
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | public function deleteDir($dirname) |
||
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | */ |
||
285 | public function listContents($directory = '', $recursive = false) |
||
305 | |||
306 | /** |
||
307 | * {@inheritdoc} |
||
308 | */ |
||
309 | public function getSize($path) |
||
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | */ |
||
317 | public function getTimestamp($path) |
||
321 | |||
322 | /** |
||
323 | * {@inheritdoc} |
||
324 | */ |
||
325 | public function getMimetype($path) |
||
329 | |||
330 | /** |
||
331 | * @return boolean |
||
332 | */ |
||
333 | public function getUseStreamedCopy() |
||
337 | |||
338 | /** |
||
339 | * @param boolean $useStreamedCopy |
||
340 | */ |
||
341 | public function setUseStreamedCopy($useStreamedCopy) |
||
345 | |||
346 | /** |
||
347 | * Copy a file through WebDav COPY method. |
||
348 | * |
||
349 | * @param string $path |
||
350 | * @param string $newPath |
||
351 | * |
||
352 | * @return bool |
||
353 | */ |
||
354 | protected function nativeCopy($path, $newPath) |
||
378 | |||
379 | /** |
||
380 | * Normalise a WebDAV repsonse object. |
||
381 | * |
||
382 | * @param array $object |
||
383 | * @param string $path |
||
384 | * |
||
385 | * @return array |
||
386 | */ |
||
387 | protected function normalizeObject(array $object, $path) |
||
404 | |||
405 | /** |
||
406 | * @param array $object |
||
407 | * @return bool |
||
408 | */ |
||
409 | protected function isDirectory(array $object) |
||
419 | } |
||
420 |
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: