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) |
|
209 | |||
210 | /** |
||
211 | * @param string $path |
||
212 | * @param string $newpath |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | 1 | public function copy($path, $newpath) |
|
226 | |||
227 | /** |
||
228 | * @param string $path |
||
229 | * |
||
230 | * @return bool |
||
231 | */ |
||
232 | 1 | public function delete($path) |
|
239 | |||
240 | /** |
||
241 | * @param string $dirname |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | 1 | public function deleteDir($dirname) |
|
262 | |||
263 | /** |
||
264 | * @param string $dirname |
||
265 | * @param Config $config |
||
266 | * |
||
267 | * @return array|bool |
||
268 | */ |
||
269 | 1 | public function createDir($dirname, Config $config) |
|
277 | |||
278 | /** |
||
279 | * @param string $path |
||
280 | * @param string $visibility |
||
281 | * |
||
282 | * @return bool |
||
283 | */ |
||
284 | 1 | public function setVisibility($path, $visibility) |
|
295 | |||
296 | /** |
||
297 | * @param string $path |
||
298 | * |
||
299 | * @return bool |
||
300 | */ |
||
301 | 1 | public function has($path) |
|
309 | |||
310 | /** |
||
311 | * @param string $path |
||
312 | * |
||
313 | * @return array|bool |
||
314 | */ |
||
315 | 1 | public function read($path) |
|
328 | |||
329 | /** |
||
330 | * @param string $path |
||
331 | * |
||
332 | * @return array|bool |
||
333 | */ |
||
334 | 1 | public function readStream($path) |
|
342 | |||
343 | /** |
||
344 | * @param string $directory |
||
345 | * @param bool $recursive |
||
346 | * |
||
347 | * @return array|bool |
||
348 | */ |
||
349 | 1 | public function listContents($directory = '', $recursive = false) |
|
361 | |||
362 | /** |
||
363 | * @param string $path |
||
364 | * |
||
365 | * @return array|bool |
||
366 | */ |
||
367 | 5 | public function getMetadata($path) |
|
374 | |||
375 | /** |
||
376 | * @param string $path |
||
377 | * |
||
378 | * @return array|bool |
||
379 | */ |
||
380 | 1 | public function getSize($path) |
|
387 | |||
388 | /** |
||
389 | * @param string $path |
||
390 | * |
||
391 | * @return array|bool |
||
392 | */ |
||
393 | 1 | public function getMimetype($path) |
|
400 | |||
401 | /** |
||
402 | * @param string $path |
||
403 | * |
||
404 | * @return array|bool |
||
405 | */ |
||
406 | 1 | public function getTimestamp($path) |
|
413 | |||
414 | /** |
||
415 | * @param string $path |
||
416 | * |
||
417 | * @return array|bool |
||
418 | */ |
||
419 | 1 | public function getVisibility($path) |
|
437 | |||
438 | /** |
||
439 | * @param array $content |
||
440 | * |
||
441 | * @return array |
||
442 | */ |
||
443 | private function normalizeFileInfo(array $content) |
||
458 | |||
459 | /** |
||
460 | * @param string $directory |
||
461 | * @param bool $recursive |
||
462 | * |
||
463 | * @return mixed |
||
464 | */ |
||
465 | 2 | private function listObjects($directory = '', $recursive = false) |
|
473 | } |
||
474 |