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) |
|
220 | { |
||
221 | 2 | $result = $this->mainAdapter->read($path); |
|
222 | |||
223 | 2 | if (false !== $result) { |
|
224 | 1 | return $result; |
|
225 | } |
||
226 | |||
227 | 1 | $result = $this->fallback->read($path); |
|
228 | |||
229 | 1 | if (false !== $result && $this->forceCopyOnMain) { |
|
230 | $this->mainAdapter->write($path, $result['contents'], new Config()); |
||
231 | } |
||
232 | |||
233 | 1 | return $result; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | public function readStream($path) |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | 1 | public function listContents($directory = '', $recursive = false) |
|
277 | |||
278 | /** |
||
279 | * {@inheritdoc} |
||
280 | */ |
||
281 | public function getMetadata($path) |
||
289 | |||
290 | /** |
||
291 | * {@inheritdoc} |
||
292 | */ |
||
293 | public function getSize($path) |
||
301 | |||
302 | /** |
||
303 | * {@inheritdoc} |
||
304 | */ |
||
305 | public function getMimetype($path) |
||
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | */ |
||
317 | public function getTimestamp($path) |
||
325 | |||
326 | /** |
||
327 | * {@inheritdoc} |
||
328 | */ |
||
329 | 2 | public function getVisibility($path) |
|
337 | |||
338 | /** |
||
339 | * Copies a resource accessible through the fallback adapter to the filesystem abstracted with the main adapter. |
||
340 | * |
||
341 | * @param $path |
||
342 | * @return boolean |
||
343 | */ |
||
344 | 3 | private function portFromFallback($path, $newpath) |
|
360 | } |
||
361 |