Complex classes like FallbackAdapter 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 FallbackAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class FallbackAdapter implements AdapterInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var AdapterInterface |
||
13 | */ |
||
14 | protected $mainAdapter; |
||
15 | |||
16 | /** |
||
17 | * @var AdapterInterface |
||
18 | */ |
||
19 | protected $fallback; |
||
20 | |||
21 | /** |
||
22 | * @var bool |
||
23 | */ |
||
24 | protected $forceCopyOnMain; |
||
25 | |||
26 | /** |
||
27 | * Constructor. |
||
28 | * |
||
29 | * @param AdapterInterface $mainAdapter |
||
30 | * @param AdapterInterface $fallback |
||
31 | * @param boolean $forceCopyOnMain |
||
32 | */ |
||
33 | 25 | public function __construct(AdapterInterface $mainAdapter, AdapterInterface $fallback, $forceCopyOnMain = false) |
|
39 | |||
40 | /** |
||
41 | * Returns the main adapter. |
||
42 | * |
||
43 | * @return AdapterInterface |
||
44 | */ |
||
45 | 1 | public function getMainAdapter() |
|
49 | |||
50 | /** |
||
51 | * Returns the fallback adapter. |
||
52 | * |
||
53 | * @return AdapterInterface |
||
54 | */ |
||
55 | 1 | public function getFallbackAdapter() |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 1 | public function write($path, $contents, Config $config) |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 1 | public function writeStream($path, $resource, Config $config) |
|
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 2 | public function update($path, $contents, Config $config) |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 2 | public function updateStream($path, $resource, Config $config) |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 2 | public function rename($path, $newpath) |
|
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 3 | public function copy($path, $newpath) |
|
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | 3 | public function delete($path) |
|
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | public function deleteDir($dirname) |
||
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | public function createDir($dirname, Config $config) |
||
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | public function setVisibility($path, $visibility) |
||
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | 3 | public function has($path) |
|
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | 2 | public function read($path) |
|
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | public function readStream($path) |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | 1 | public function listContents($directory = '', $recursive = false) |
|
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | public function getMetadata($path) |
||
285 | |||
286 | /** |
||
287 | * {@inheritdoc} |
||
288 | */ |
||
289 | public function getSize($path) |
||
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | public function getMimetype($path) |
||
309 | |||
310 | /** |
||
311 | * {@inheritdoc} |
||
312 | */ |
||
313 | public function getTimestamp($path) |
||
321 | |||
322 | /** |
||
323 | * {@inheritdoc} |
||
324 | */ |
||
325 | 2 | public function getVisibility($path) |
|
333 | |||
334 | /** |
||
335 | * Copies a resource accessible through the fallback adapter to the filesystem abstracted with the main adapter. |
||
336 | * |
||
337 | * @param $path |
||
338 | * @return boolean |
||
339 | */ |
||
340 | 3 | private function portFromFallback($path, $newpath) |
|
356 | } |
||
357 |