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 |
||
15 | class Adapter extends AbstractAdapter |
||
16 | { |
||
17 | /** |
||
18 | * @var Client |
||
19 | */ |
||
20 | protected $client; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $config = []; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $regionMap = [ |
||
31 | 'cn-east' => 'ap-shanghai', |
||
32 | 'cn-sorth' => 'ap-guangzhou', |
||
33 | 'cn-north' => 'ap-beijing-1', |
||
34 | 'cn-south-2' => 'ap-guangzhou-2', |
||
35 | 'cn-southwest' => 'ap-chengdu', |
||
36 | 'sg' => 'ap-singapore', |
||
37 | 'tj' => 'ap-beijing-1', |
||
38 | 'bj' => 'ap-beijing', |
||
39 | 'sh' => 'ap-shanghai', |
||
40 | 'gz' => 'ap-guangzhou', |
||
41 | 'cd' => 'ap-chengdu', |
||
42 | 'sgp' => 'ap-singapore', |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * Adapter constructor. |
||
47 | * |
||
48 | * @param Client $client |
||
49 | * @param array $config |
||
50 | */ |
||
51 | public function __construct(Client $client, array $config) |
||
58 | |||
59 | /** |
||
60 | * @return string |
||
61 | */ |
||
62 | 18 | public function getBucket() |
|
66 | |||
67 | /** |
||
68 | * @return string |
||
69 | */ |
||
70 | 2 | public function getAppId() |
|
74 | |||
75 | /** |
||
76 | * @return string |
||
77 | */ |
||
78 | 2 | public function getRegion() |
|
82 | |||
83 | /** |
||
84 | * @param $path |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 2 | public function getSourcePath($path) |
|
94 | |||
95 | /** |
||
96 | * @param string $path |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | 4 | public function getUrl($path) |
|
110 | |||
111 | /** |
||
112 | * @param string $path |
||
113 | * @param \DateTimeInterface $expiration |
||
114 | * @param array $options |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | public function getTemporaryUrl($path, DateTimeInterface $expiration, array $options = []) |
||
126 | |||
127 | /** |
||
128 | * @param string $path |
||
129 | * @param string $contents |
||
130 | * @param Config $config |
||
131 | * |
||
132 | * @return array|bool |
||
133 | */ |
||
134 | 2 | public function write($path, $contents, Config $config) |
|
138 | |||
139 | /** |
||
140 | * @param string $path |
||
141 | * @param resource $resource |
||
142 | * @param Config $config |
||
143 | * |
||
144 | * @return array|bool |
||
145 | */ |
||
146 | 2 | public function writeStream($path, $resource, Config $config) |
|
150 | |||
151 | /** |
||
152 | * @param string $path |
||
153 | * @param string $contents |
||
154 | * @param Config $config |
||
155 | * |
||
156 | * @return array|bool |
||
157 | */ |
||
158 | 1 | public function update($path, $contents, Config $config) |
|
162 | |||
163 | /** |
||
164 | * @param string $path |
||
165 | * @param resource $resource |
||
166 | * @param Config $config |
||
167 | * |
||
168 | * @return array|bool |
||
169 | */ |
||
170 | 1 | public function updateStream($path, $resource, Config $config) |
|
174 | |||
175 | /** |
||
176 | * @param string $path |
||
177 | * @param string $newpath |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | 1 | public function rename($path, $newpath) |
|
195 | |||
196 | /** |
||
197 | * @param string $path |
||
198 | * @param string $newpath |
||
199 | * |
||
200 | * @return bool |
||
201 | */ |
||
202 | 1 | public function copy($path, $newpath) |
|
212 | |||
213 | /** |
||
214 | * @param string $path |
||
215 | * |
||
216 | * @return bool |
||
217 | */ |
||
218 | 1 | public function delete($path) |
|
225 | |||
226 | /** |
||
227 | * @param string $dirname |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | 1 | public function deleteDir($dirname) |
|
244 | |||
245 | /** |
||
246 | * @param string $dirname |
||
247 | * @param Config $config |
||
248 | * |
||
249 | * @return array|bool |
||
250 | */ |
||
251 | 1 | public function createDir($dirname, Config $config) |
|
259 | |||
260 | /** |
||
261 | * @param string $path |
||
262 | * @param string $visibility |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | 1 | public function setVisibility($path, $visibility) |
|
277 | |||
278 | /** |
||
279 | * @param string $path |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | 1 | public function has($path) |
|
291 | |||
292 | /** |
||
293 | * @param string $path |
||
294 | * |
||
295 | * @return array|bool |
||
296 | */ |
||
297 | 1 | public function read($path) |
|
310 | |||
311 | /** |
||
312 | * @param string $path |
||
313 | * |
||
314 | * @return array|bool |
||
315 | */ |
||
316 | 1 | public function readStream($path) |
|
324 | |||
325 | /** |
||
326 | * @param string $directory |
||
327 | * @param bool $recursive |
||
328 | * |
||
329 | * @return array|bool |
||
330 | */ |
||
331 | 2 | public function listContents($directory = '', $recursive = false) |
|
348 | |||
349 | /** |
||
350 | * @param string $path |
||
351 | * |
||
352 | * @return array|bool |
||
353 | */ |
||
354 | 5 | public function getMetadata($path) |
|
361 | |||
362 | /** |
||
363 | * @param string $path |
||
364 | * |
||
365 | * @return array|bool |
||
366 | */ |
||
367 | 1 | public function getSize($path) |
|
374 | |||
375 | /** |
||
376 | * @param string $path |
||
377 | * |
||
378 | * @return array|bool |
||
379 | */ |
||
380 | 1 | public function getMimetype($path) |
|
387 | |||
388 | /** |
||
389 | * @param string $path |
||
390 | * |
||
391 | * @return array|bool |
||
392 | */ |
||
393 | 1 | public function getTimestamp($path) |
|
400 | |||
401 | /** |
||
402 | * @param string $path |
||
403 | * |
||
404 | * @return array|bool |
||
405 | */ |
||
406 | 1 | public function getVisibility($path) |
|
424 | |||
425 | private function normalizeFileInfo(array $content) |
||
434 | } |
||
435 |