Complex classes like Adapter 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 Adapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Adapter extends AbstractAdapter |
||
17 | { |
||
18 | /** |
||
19 | * @var Client |
||
20 | */ |
||
21 | protected $client; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $config = []; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $regionMap = [ |
||
32 | 'cn-east' => 'ap-shanghai', |
||
33 | 'cn-sorth' => 'ap-guangzhou', |
||
34 | 'cn-north' => 'ap-beijing-1', |
||
35 | 'cn-south-2' => 'ap-guangzhou-2', |
||
36 | 'cn-southwest' => 'ap-chengdu', |
||
37 | 'sg' => 'ap-singapore', |
||
38 | 'tj' => 'ap-beijing-1', |
||
39 | 'bj' => 'ap-beijing', |
||
40 | 'sh' => 'ap-shanghai', |
||
41 | 'gz' => 'ap-guangzhou', |
||
42 | 'cd' => 'ap-chengdu', |
||
43 | 'sgp' => 'ap-singapore', |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * Adapter constructor. |
||
48 | * |
||
49 | * @param Client $client |
||
50 | * @param array $config |
||
51 | */ |
||
52 | public function __construct(Client $client, array $config) |
||
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | */ |
||
63 | 18 | public function getBucket() |
|
67 | |||
68 | /** |
||
69 | * @return string |
||
70 | */ |
||
71 | 2 | public function getAppId() |
|
75 | |||
76 | /** |
||
77 | * @return string |
||
78 | */ |
||
79 | 2 | public function getRegion() |
|
84 | |||
85 | /** |
||
86 | * @param $path |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | 2 | public function getSourcePath($path) |
|
96 | |||
97 | /** |
||
98 | * @param string $path |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | 6 | public function getUrl($path) |
|
112 | |||
113 | /** |
||
114 | * @param string $path |
||
115 | * @param \DateTimeInterface $expiration |
||
116 | * @param array $options |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getTemporaryUrl($path, DateTimeInterface $expiration, array $options = []) |
||
128 | |||
129 | /** |
||
130 | * @param string $path |
||
131 | * @param string $contents |
||
132 | * @param Config $config |
||
133 | * |
||
134 | * @return array|bool |
||
135 | */ |
||
136 | 2 | public function write($path, $contents, Config $config) |
|
140 | |||
141 | /** |
||
142 | * @param string $path |
||
143 | * @param resource $resource |
||
144 | * @param Config $config |
||
145 | * |
||
146 | * @return array|bool |
||
147 | */ |
||
148 | 2 | public function writeStream($path, $resource, Config $config) |
|
152 | |||
153 | /** |
||
154 | * @param string $path |
||
155 | * @param string $contents |
||
156 | * @param Config $config |
||
157 | * |
||
158 | * @return array|bool |
||
159 | */ |
||
160 | 1 | public function update($path, $contents, Config $config) |
|
164 | |||
165 | /** |
||
166 | * @param string $path |
||
167 | * @param resource $resource |
||
168 | * @param Config $config |
||
169 | * |
||
170 | * @return array|bool |
||
171 | */ |
||
172 | 1 | public function updateStream($path, $resource, Config $config) |
|
176 | |||
177 | /** |
||
178 | * @param string $path |
||
179 | * @param string $newpath |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | 1 | public function rename($path, $newpath) |
|
197 | |||
198 | /** |
||
199 | * @param string $path |
||
200 | * @param string $newpath |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | 1 | public function copy($path, $newpath) |
|
214 | |||
215 | /** |
||
216 | * @param string $path |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | 1 | public function delete($path) |
|
227 | |||
228 | /** |
||
229 | * @param string $dirname |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | 1 | public function deleteDir($dirname) |
|
250 | |||
251 | /** |
||
252 | * @param string $dirname |
||
253 | * @param Config $config |
||
254 | * |
||
255 | * @return array|bool |
||
256 | */ |
||
257 | 1 | public function createDir($dirname, Config $config) |
|
265 | |||
266 | /** |
||
267 | * @param string $path |
||
268 | * @param string $visibility |
||
269 | * |
||
270 | * @return bool |
||
271 | */ |
||
272 | 1 | public function setVisibility($path, $visibility) |
|
283 | |||
284 | /** |
||
285 | * @param string $path |
||
286 | * |
||
287 | * @return bool |
||
288 | */ |
||
289 | 1 | public function has($path) |
|
297 | |||
298 | /** |
||
299 | * @param string $path |
||
300 | * |
||
301 | * @return array|bool |
||
302 | */ |
||
303 | 1 | public function read($path) |
|
316 | |||
317 | /** |
||
318 | * @param string $path |
||
319 | * |
||
320 | * @return array|bool |
||
321 | */ |
||
322 | public function readStream($path) |
||
323 | { |
||
324 | try { |
||
325 | return ['stream' => fopen($this->getUrl($path), 'rb', false)]; |
||
326 | } catch (NoSuchKeyException $e) { |
||
327 | return false; |
||
328 | } |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @param string $directory |
||
333 | * @param bool $recursive |
||
334 | * |
||
335 | * @return array|bool |
||
336 | */ |
||
337 | 1 | public function listContents($directory = '', $recursive = false) |
|
349 | |||
350 | /** |
||
351 | * @param string $path |
||
352 | * |
||
353 | * @return array|bool |
||
354 | */ |
||
355 | 5 | public function getMetadata($path) |
|
362 | |||
363 | /** |
||
364 | * @param string $path |
||
365 | * |
||
366 | * @return array|bool |
||
367 | */ |
||
368 | 1 | public function getSize($path) |
|
375 | |||
376 | /** |
||
377 | * @param string $path |
||
378 | * |
||
379 | * @return array|bool |
||
380 | */ |
||
381 | 1 | public function getMimetype($path) |
|
388 | |||
389 | /** |
||
390 | * @param string $path |
||
391 | * |
||
392 | * @return array|bool |
||
393 | */ |
||
394 | 1 | public function getTimestamp($path) |
|
401 | |||
402 | /** |
||
403 | * @param string $path |
||
404 | * |
||
405 | * @return array|bool |
||
406 | */ |
||
407 | 1 | public function getVisibility($path) |
|
425 | |||
426 | /** |
||
427 | * @param array $content |
||
428 | * |
||
429 | * @return array |
||
430 | */ |
||
431 | private function normalizeFileInfo(array $content) |
||
446 | |||
447 | /** |
||
448 | * @param string $directory |
||
449 | * @param bool $recursive |
||
450 | * |
||
451 | * @return mixed |
||
452 | */ |
||
453 | 2 | private function listObjects($directory = '', $recursive = false) |
|
461 | } |
||
462 |