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) |
|
116 | |||
117 | /** |
||
118 | * @param string $path |
||
119 | * @param \DateTimeInterface $expiration |
||
120 | * @param array $options |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | public function getTemporaryUrl($path, DateTimeInterface $expiration, array $options = []) |
||
140 | |||
141 | /** |
||
142 | * @param string $path |
||
143 | * @param string $contents |
||
144 | * @param Config $config |
||
145 | * |
||
146 | * @return array|bool |
||
147 | */ |
||
148 | 2 | public function write($path, $contents, Config $config) |
|
152 | |||
153 | /** |
||
154 | * @param string $path |
||
155 | * @param resource $resource |
||
156 | * @param Config $config |
||
157 | * |
||
158 | * @return array|bool |
||
159 | */ |
||
160 | 2 | public function writeStream($path, $resource, Config $config) |
|
164 | |||
165 | /** |
||
166 | * @param string $path |
||
167 | * @param string $contents |
||
168 | * @param Config $config |
||
169 | * |
||
170 | * @return array|bool |
||
171 | */ |
||
172 | 1 | public function update($path, $contents, Config $config) |
|
176 | |||
177 | /** |
||
178 | * @param string $path |
||
179 | * @param resource $resource |
||
180 | * @param Config $config |
||
181 | * |
||
182 | * @return array|bool |
||
183 | */ |
||
184 | 1 | public function updateStream($path, $resource, Config $config) |
|
188 | |||
189 | /** |
||
190 | * @param string $path |
||
191 | * @param string $newpath |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | 1 | public function rename($path, $newpath) |
|
203 | |||
204 | /** |
||
205 | * @param string $path |
||
206 | * @param string $newpath |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | 2 | public function copy($path, $newpath) |
|
216 | |||
217 | /** |
||
218 | * @param string $path |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | 1 | public function delete($path) |
|
229 | |||
230 | /** |
||
231 | * @param string $dirname |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | 1 | public function deleteDir($dirname) |
|
252 | |||
253 | /** |
||
254 | * @param string $dirname |
||
255 | * @param Config $config |
||
256 | * |
||
257 | * @return array|bool |
||
258 | */ |
||
259 | 1 | public function createDir($dirname, Config $config) |
|
267 | |||
268 | /** |
||
269 | * @param string $path |
||
270 | * @param string $visibility |
||
271 | * |
||
272 | * @return bool |
||
273 | */ |
||
274 | 1 | public function setVisibility($path, $visibility) |
|
285 | |||
286 | /** |
||
287 | * @param string $path |
||
288 | * |
||
289 | * @return bool |
||
290 | */ |
||
291 | 1 | public function has($path) |
|
299 | |||
300 | /** |
||
301 | * @param string $path |
||
302 | * |
||
303 | * @return array|bool |
||
304 | */ |
||
305 | 1 | public function read($path) |
|
318 | |||
319 | /** |
||
320 | * @param string $path |
||
321 | * |
||
322 | * @return array|bool |
||
323 | */ |
||
324 | public function readStream($path) |
||
332 | |||
333 | /** |
||
334 | * @param string $directory |
||
335 | * @param bool $recursive |
||
336 | * |
||
337 | * @return array|bool |
||
338 | */ |
||
339 | 1 | public function listContents($directory = '', $recursive = false) |
|
351 | |||
352 | /** |
||
353 | * @param string $path |
||
354 | * |
||
355 | * @return array|bool |
||
356 | */ |
||
357 | 5 | public function getMetadata($path) |
|
364 | |||
365 | /** |
||
366 | * @param string $path |
||
367 | * |
||
368 | * @return array|bool |
||
369 | */ |
||
370 | 1 | public function getSize($path) |
|
377 | |||
378 | /** |
||
379 | * @param string $path |
||
380 | * |
||
381 | * @return array|bool |
||
382 | */ |
||
383 | 1 | public function getMimetype($path) |
|
390 | |||
391 | /** |
||
392 | * @param string $path |
||
393 | * |
||
394 | * @return array|bool |
||
395 | */ |
||
396 | 1 | public function getTimestamp($path) |
|
403 | |||
404 | /** |
||
405 | * @param string $path |
||
406 | * |
||
407 | * @return array|bool |
||
408 | */ |
||
409 | 1 | public function getVisibility($path) |
|
427 | |||
428 | /** |
||
429 | * @param array $content |
||
430 | * |
||
431 | * @return array |
||
432 | */ |
||
433 | private function normalizeFileInfo(array $content) |
||
448 | |||
449 | /** |
||
450 | * @param string $directory |
||
451 | * @param bool $recursive |
||
452 | * |
||
453 | * @return mixed |
||
454 | */ |
||
455 | 2 | private function listObjects($directory = '', $recursive = false) |
|
463 | } |
||
464 |