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 | const METADATA_FIELDS = [ |
||
21 | '{DAV:}displayname', |
||
22 | '{DAV:}getcontentlength', |
||
23 | '{DAV:}getcontenttype', |
||
24 | '{DAV:}getlastmodified', |
||
25 | '{DAV:}iscollection', |
||
26 | ]; |
||
27 | use StreamedTrait; |
||
28 | use StreamedCopyTrait { |
||
29 | StreamedCopyTrait::copy as streamedCopy; |
||
30 | } |
||
31 | use NotSupportingVisibilityTrait; |
||
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 update($path, $contents, Config $config) |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | public function rename($path, $newpath) |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | public function copy($path, $newpath) |
||
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | public function delete($path) |
||
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | public function createDir($path, Config $config) |
||
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | */ |
||
254 | public function deleteDir($dirname) |
||
258 | |||
259 | /** |
||
260 | * {@inheritdoc} |
||
261 | */ |
||
262 | public function listContents($directory = '', $recursive = false) |
||
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | public function getSize($path) |
||
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | public function getTimestamp($path) |
||
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | */ |
||
302 | public function getMimetype($path) |
||
306 | |||
307 | /** |
||
308 | * @return boolean |
||
309 | */ |
||
310 | public function getUseStreamedCopy() |
||
314 | |||
315 | /** |
||
316 | * @param boolean $useStreamedCopy |
||
317 | */ |
||
318 | public function setUseStreamedCopy($useStreamedCopy) |
||
322 | |||
323 | /** |
||
324 | * Copy a file through WebDav COPY method. |
||
325 | * |
||
326 | * @param string $path |
||
327 | * @param string $newPath |
||
328 | * |
||
329 | * @return bool |
||
330 | */ |
||
331 | protected function nativeCopy($path, $newPath) |
||
355 | |||
356 | /** |
||
357 | * Normalise a WebDAV repsonse object. |
||
358 | * |
||
359 | * @param array $object |
||
360 | * @param string $path |
||
361 | * |
||
362 | * @return array |
||
363 | */ |
||
364 | protected function normalizeObject(array $object, $path) |
||
381 | |||
382 | private function isDirectory(array $object) |
||
386 | } |
||
387 |
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: