Complex classes like QiniuAdapter 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 QiniuAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class QiniuAdapter extends AbstractAdapter |
||
25 | { |
||
26 | use NotSupportingVisibilityTrait; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $accessKey; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $secretKey; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $bucket; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $domain; |
||
47 | |||
48 | /** |
||
49 | * @var \Qiniu\Auth |
||
50 | */ |
||
51 | protected $authManager; |
||
52 | |||
53 | /** |
||
54 | * @var \Qiniu\Storage\UploadManager |
||
55 | */ |
||
56 | protected $uploadManager; |
||
57 | |||
58 | /** |
||
59 | * @var \Qiniu\Storage\BucketManager |
||
60 | */ |
||
61 | protected $bucketManager; |
||
62 | |||
63 | /** |
||
64 | * QiniuAdapter constructor. |
||
65 | * |
||
66 | * @param string $accessKey |
||
67 | * @param string $secretKey |
||
68 | * @param string $bucket |
||
69 | * @param string $domain |
||
70 | */ |
||
71 | 1 | public function __construct($accessKey, $secretKey, $bucket, $domain) |
|
78 | |||
79 | /** |
||
80 | * Write a new file. |
||
81 | * |
||
82 | * @param string $path |
||
83 | * @param string $contents |
||
84 | * @param Config $config Config object |
||
85 | * |
||
86 | * @return array|false false on failure file meta data on success |
||
87 | */ |
||
88 | 1 | public function write($path, $contents, Config $config) |
|
102 | |||
103 | /** |
||
104 | * Write a new file using a stream. |
||
105 | * |
||
106 | * @param string $path |
||
107 | * @param resource $resource |
||
108 | * @param Config $config Config object |
||
109 | * |
||
110 | * @return array|false false on failure file meta data on success |
||
111 | */ |
||
112 | 1 | public function writeStream($path, $resource, Config $config) |
|
128 | |||
129 | /** |
||
130 | * Update a file. |
||
131 | * |
||
132 | * @param string $path |
||
133 | * @param string $contents |
||
134 | * @param Config $config Config object |
||
135 | * |
||
136 | * @return array|false false on failure file meta data on success |
||
137 | */ |
||
138 | 1 | public function update($path, $contents, Config $config) |
|
144 | |||
145 | /** |
||
146 | * Update a file using a stream. |
||
147 | * |
||
148 | * @param string $path |
||
149 | * @param resource $resource |
||
150 | * @param Config $config Config object |
||
151 | * |
||
152 | * @return array|false false on failure file meta data on success |
||
153 | */ |
||
154 | 1 | public function updateStream($path, $resource, Config $config) |
|
160 | |||
161 | /** |
||
162 | * Rename a file. |
||
163 | * |
||
164 | * @param string $path |
||
165 | * @param string $newPath |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | 1 | public function rename($path, $newPath) |
|
175 | |||
176 | /** |
||
177 | * Copy a file. |
||
178 | * |
||
179 | * @param string $path |
||
180 | * @param string $newPath |
||
181 | * |
||
182 | * @return bool |
||
183 | */ |
||
184 | 1 | public function copy($path, $newPath) |
|
190 | |||
191 | /** |
||
192 | * Delete a file. |
||
193 | * |
||
194 | * @param string $path |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | 1 | public function delete($path) |
|
204 | |||
205 | /** |
||
206 | * Delete a directory. |
||
207 | * |
||
208 | * @param string $directory |
||
209 | * |
||
210 | * @return bool |
||
211 | */ |
||
212 | 1 | public function deleteDir($directory) |
|
216 | |||
217 | /** |
||
218 | * Create a directory. |
||
219 | * |
||
220 | * @param string $directory directory name |
||
221 | * @param Config $config |
||
222 | * |
||
223 | * @return array|false |
||
224 | */ |
||
225 | 1 | public function createDir($directory, Config $config) |
|
229 | |||
230 | /** |
||
231 | * Check whether a file exists. |
||
232 | * |
||
233 | * @param string $path |
||
234 | * |
||
235 | * @return array|bool|null |
||
236 | */ |
||
237 | 1 | public function has($path) |
|
243 | |||
244 | /** |
||
245 | * Get resource url. |
||
246 | * |
||
247 | * @param string $path |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | 1 | public function getUrl($path) |
|
255 | |||
256 | /** |
||
257 | * Read a file. |
||
258 | * |
||
259 | * @param string $path |
||
260 | * |
||
261 | * @return array|false |
||
262 | */ |
||
263 | 1 | public function read($path) |
|
269 | |||
270 | /** |
||
271 | * Read a file as a stream. |
||
272 | * |
||
273 | * @param string $path |
||
274 | * |
||
275 | * @return array|false |
||
276 | */ |
||
277 | 1 | public function readStream($path) |
|
287 | |||
288 | /** |
||
289 | * List contents of a directory. |
||
290 | * |
||
291 | * @param string $directory |
||
292 | * @param bool $recursive |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | 1 | public function listContents($directory = '', $recursive = false) |
|
308 | |||
309 | /** |
||
310 | * Get all the meta data of a file or directory. |
||
311 | * |
||
312 | * @param string $path |
||
313 | * |
||
314 | * @return array|false |
||
315 | */ |
||
316 | 1 | public function getMetadata($path) |
|
323 | |||
324 | /** |
||
325 | * Get the size of a file. |
||
326 | * |
||
327 | * @param string $path |
||
328 | * |
||
329 | * @return array|false |
||
330 | */ |
||
331 | 1 | public function getSize($path) |
|
335 | |||
336 | /** |
||
337 | * Get the mime-type of a file. |
||
338 | * |
||
339 | * @param string $path |
||
340 | * |
||
341 | * @return array|false |
||
342 | */ |
||
343 | 1 | public function getMimeType($path) |
|
353 | |||
354 | /** |
||
355 | * Get the timestamp of a file. |
||
356 | * |
||
357 | * @param string $path |
||
358 | * |
||
359 | * @return array|false |
||
360 | */ |
||
361 | 1 | public function getTimestamp($path) |
|
365 | |||
366 | /** |
||
367 | * @param \Qiniu\Storage\BucketManager $manager |
||
368 | * |
||
369 | * @return $this |
||
370 | */ |
||
371 | 1 | public function setBucketManager(BucketManager $manager) |
|
377 | |||
378 | /** |
||
379 | * @param \Qiniu\Storage\UploadManager $manager |
||
380 | * |
||
381 | * @return $this |
||
382 | */ |
||
383 | 1 | public function setUploadManager(UploadManager $manager) |
|
389 | |||
390 | /** |
||
391 | * @param \Qiniu\Auth $manager |
||
392 | * |
||
393 | * @return $this |
||
394 | */ |
||
395 | 1 | public function setAuthManager(Auth $manager) |
|
401 | |||
402 | /** |
||
403 | * @return \Qiniu\Storage\BucketManager |
||
404 | */ |
||
405 | 1 | public function getBucketManager() |
|
409 | |||
410 | /** |
||
411 | * @return \Qiniu\Auth |
||
412 | */ |
||
413 | 1 | public function getAuthManager() |
|
417 | |||
418 | /** |
||
419 | * @return \Qiniu\Storage\UploadManager |
||
420 | */ |
||
421 | 1 | public function getUploadManager() |
|
425 | |||
426 | /** |
||
427 | * Get the upload token. |
||
428 | * |
||
429 | * @param string|null $key |
||
430 | * @param int $expires |
||
431 | * @param bool $policy |
||
432 | * |
||
433 | * @return string |
||
434 | */ |
||
435 | 1 | public function getUploadToken($key = null, $expires = 3600, $policy = true) |
|
439 | |||
440 | /** |
||
441 | * @param array $stats |
||
442 | * |
||
443 | * @return array |
||
444 | */ |
||
445 | 2 | protected function normalizeFileInfo(array $stats) |
|
454 | |||
455 | /** |
||
456 | * @param string $domain |
||
457 | * |
||
458 | * @return string |
||
459 | */ |
||
460 | 2 | protected function normalizeHost($domain) |
|
468 | } |
||
469 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.