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 | 18 | public function getBucket() |
|
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 | 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) |
|
155 | |||
156 | /** |
||
157 | * @param string $path |
||
158 | * @param resource $resource |
||
159 | * @param Config $config |
||
160 | * |
||
161 | * @return array|bool |
||
162 | */ |
||
163 | 2 | public function writeStream($path, $resource, Config $config) |
|
167 | |||
168 | /** |
||
169 | * @param string $path |
||
170 | * @param string $contents |
||
171 | * @param Config $config |
||
172 | * |
||
173 | * @return array|bool |
||
174 | */ |
||
175 | 2 | public function update($path, $contents, Config $config) |
|
179 | |||
180 | /** |
||
181 | * @param string $path |
||
182 | * @param resource $resource |
||
183 | * @param Config $config |
||
184 | * |
||
185 | * @return array|bool |
||
186 | */ |
||
187 | 1 | public function updateStream($path, $resource, Config $config) |
|
191 | |||
192 | /** |
||
193 | * @param string $path |
||
194 | * @param string $newpath |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | 1 | public function rename($path, $newpath) |
|
206 | |||
207 | /** |
||
208 | * @param string $path |
||
209 | * @param string $newpath |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | 2 | public function copy($path, $newpath) |
|
219 | |||
220 | /** |
||
221 | * @param string $path |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | 2 | public function delete($path) |
|
232 | |||
233 | /** |
||
234 | * @param string $dirname |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | 1 | public function deleteDir($dirname) |
|
255 | |||
256 | /** |
||
257 | * @param string $dirname |
||
258 | * @param Config $config |
||
259 | * |
||
260 | * @return array|bool |
||
261 | */ |
||
262 | 1 | public function createDir($dirname, Config $config) |
|
270 | |||
271 | /** |
||
272 | * @param string $path |
||
273 | * @param string $visibility |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | 1 | public function setVisibility($path, $visibility) |
|
288 | |||
289 | /** |
||
290 | * @param string $path |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | 1 | public function has($path) |
|
302 | |||
303 | /** |
||
304 | * @param string $path |
||
305 | * |
||
306 | * @return array|bool |
||
307 | */ |
||
308 | 1 | public function read($path) |
|
321 | |||
322 | /** |
||
323 | * @param string $path |
||
324 | * |
||
325 | * @return array|bool |
||
326 | */ |
||
327 | 1 | public function readStream($path) |
|
335 | |||
336 | /** |
||
337 | * @param string $directory |
||
338 | * @param bool $recursive |
||
339 | * |
||
340 | * @return array|bool |
||
341 | */ |
||
342 | 1 | public function listContents($directory = '', $recursive = false) |
|
354 | |||
355 | /** |
||
356 | * @param string $path |
||
357 | * |
||
358 | * @return array|bool |
||
359 | */ |
||
360 | 5 | public function getMetadata($path) |
|
367 | |||
368 | /** |
||
369 | * @param string $path |
||
370 | * |
||
371 | * @return array|bool |
||
372 | */ |
||
373 | 1 | public function getSize($path) |
|
380 | |||
381 | /** |
||
382 | * @param string $path |
||
383 | * |
||
384 | * @return array|bool |
||
385 | */ |
||
386 | 1 | public function getMimetype($path) |
|
393 | |||
394 | /** |
||
395 | * @param string $path |
||
396 | * |
||
397 | * @return array|bool |
||
398 | */ |
||
399 | 1 | public function getTimestamp($path) |
|
406 | |||
407 | /** |
||
408 | * @param string $path |
||
409 | * |
||
410 | * @return array|bool |
||
411 | */ |
||
412 | 1 | public function getVisibility($path) |
|
430 | |||
431 | /** |
||
432 | * @param array $content |
||
433 | * |
||
434 | * @return array |
||
435 | */ |
||
436 | 1 | private function normalizeFileInfo(array $content) |
|
451 | |||
452 | /** |
||
453 | * @param string $directory |
||
454 | * @param bool $recursive |
||
455 | * |
||
456 | * @return mixed |
||
457 | */ |
||
458 | 2 | private function listObjects($directory = '', $recursive = false) |
|
466 | } |
||
467 |