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 |
||
17 | class Adapter extends AbstractAdapter implements CanOverwriteFiles |
||
18 | { |
||
19 | /** |
||
20 | * @var Client |
||
21 | */ |
||
22 | protected $client; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $config = []; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $regionMap = [ |
||
33 | 'cn-east' => 'ap-shanghai', |
||
34 | 'cn-sorth' => 'ap-guangzhou', |
||
35 | 'cn-north' => 'ap-beijing-1', |
||
36 | 'cn-south-2' => 'ap-guangzhou-2', |
||
37 | 'cn-southwest' => 'ap-chengdu', |
||
38 | 'sg' => 'ap-singapore', |
||
39 | 'tj' => 'ap-beijing-1', |
||
40 | 'bj' => 'ap-beijing', |
||
41 | 'sh' => 'ap-shanghai', |
||
42 | 'gz' => 'ap-guangzhou', |
||
43 | 'cd' => 'ap-chengdu', |
||
44 | 'sgp' => 'ap-singapore', |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Adapter constructor. |
||
49 | * |
||
50 | * @param Client $client |
||
51 | * @param array $config |
||
52 | */ |
||
53 | public function __construct(Client $client, array $config) |
||
60 | |||
61 | /** |
||
62 | * @return string |
||
63 | */ |
||
64 | 19 | public function getBucket() |
|
65 | 1 | { |
|
66 | 19 | return $this->config['bucket']; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * @return string |
||
71 | */ |
||
72 | 2 | public function getAppId() |
|
76 | |||
77 | /** |
||
78 | * @return string |
||
79 | */ |
||
80 | 2 | public function getRegion() |
|
85 | |||
86 | /** |
||
87 | * @param $path |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | 2 | public function getSourcePath($path) |
|
97 | |||
98 | /** |
||
99 | * @param string $path |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | 4 | public function getUrl($path) |
|
118 | |||
119 | /** |
||
120 | * @param string $path |
||
121 | * @param \DateTimeInterface $expiration |
||
122 | * @param array $options |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 1 | public function getTemporaryUrl($path, DateTimeInterface $expiration, array $options = []) |
|
143 | |||
144 | /** |
||
145 | * @param string $path |
||
146 | * @param string $contents |
||
147 | * @param Config $config |
||
148 | * |
||
149 | * @return array|bool |
||
150 | */ |
||
151 | 2 | public function write($path, $contents, Config $config) |
|
159 | |||
160 | /** |
||
161 | * @param string $path |
||
162 | * @param resource $resource |
||
163 | * @param Config $config |
||
164 | * |
||
165 | * @return array|bool |
||
166 | */ |
||
167 | 4 | public function writeStream($path, $resource, Config $config) |
|
175 | |||
176 | /** |
||
177 | * @param string $path |
||
178 | * @param string $contents |
||
179 | * @param Config $config |
||
180 | * |
||
181 | * @return array|bool |
||
182 | */ |
||
183 | 1 | public function update($path, $contents, Config $config) |
|
187 | |||
188 | /** |
||
189 | * @param string $path |
||
190 | * @param resource $resource |
||
191 | * @param Config $config |
||
192 | * |
||
193 | * @return array|bool |
||
194 | */ |
||
195 | 1 | public function updateStream($path, $resource, Config $config) |
|
199 | |||
200 | /** |
||
201 | * @param string $path |
||
202 | * @param string $newpath |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | 1 | public function rename($path, $newpath) |
|
214 | |||
215 | /** |
||
216 | * @param string $path |
||
217 | * @param string $newpath |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | 2 | public function copy($path, $newpath) |
|
227 | |||
228 | /** |
||
229 | * @param string $path |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | 2 | public function delete($path) |
|
240 | |||
241 | /** |
||
242 | * @param string $dirname |
||
243 | * |
||
244 | * @return bool |
||
245 | */ |
||
246 | 1 | public function deleteDir($dirname) |
|
263 | |||
264 | /** |
||
265 | * @param string $dirname |
||
266 | * @param Config $config |
||
267 | * |
||
268 | * @return array|bool |
||
269 | */ |
||
270 | 1 | public function createDir($dirname, Config $config) |
|
278 | |||
279 | /** |
||
280 | * @param string $path |
||
281 | * @param string $visibility |
||
282 | * |
||
283 | * @return bool |
||
284 | */ |
||
285 | 1 | public function setVisibility($path, $visibility) |
|
301 | |||
302 | /** |
||
303 | * @param string $path |
||
304 | * |
||
305 | * @return bool |
||
306 | */ |
||
307 | 1 | public function has($path) |
|
315 | |||
316 | /** |
||
317 | * @param string $path |
||
318 | * |
||
319 | * @return array|bool |
||
320 | */ |
||
321 | 1 | public function read($path) |
|
343 | |||
344 | /** |
||
345 | * @return \GuzzleHttp\Client |
||
346 | */ |
||
347 | 1 | public function getHttpClient() |
|
355 | |||
356 | /** |
||
357 | * @param string $path |
||
358 | * |
||
359 | * @return array|bool |
||
360 | */ |
||
361 | 1 | public function readStream($path) |
|
378 | |||
379 | /** |
||
380 | * @param string $directory |
||
381 | * @param bool $recursive |
||
382 | * |
||
383 | * @return array|bool |
||
384 | */ |
||
385 | 1 | public function listContents($directory = '', $recursive = false) |
|
397 | |||
398 | /** |
||
399 | * @param string $path |
||
400 | * |
||
401 | * @return array|bool |
||
402 | */ |
||
403 | 5 | public function getMetadata($path) |
|
410 | |||
411 | /** |
||
412 | * @param string $path |
||
413 | * |
||
414 | * @return array|bool |
||
415 | */ |
||
416 | 1 | public function getSize($path) |
|
423 | |||
424 | /** |
||
425 | * @param string $path |
||
426 | * |
||
427 | * @return array|bool |
||
428 | */ |
||
429 | 1 | public function getMimetype($path) |
|
436 | |||
437 | /** |
||
438 | * @param string $path |
||
439 | * |
||
440 | * @return array|bool |
||
441 | */ |
||
442 | 1 | public function getTimestamp($path) |
|
449 | |||
450 | /** |
||
451 | * @param string $path |
||
452 | * |
||
453 | * @return array|bool |
||
454 | */ |
||
455 | 1 | public function getVisibility($path) |
|
473 | |||
474 | /** |
||
475 | * @param array $content |
||
476 | * |
||
477 | * @return array |
||
478 | */ |
||
479 | 1 | private function normalizeFileInfo(array $content) |
|
494 | |||
495 | /** |
||
496 | * @param string $directory |
||
497 | * @param bool $recursive |
||
498 | * |
||
499 | * @return mixed |
||
500 | */ |
||
501 | 2 | private function listObjects($directory = '', $recursive = false) |
|
509 | } |
||
510 |