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 |
||
25 | class QiniuAdapter extends AbstractAdapter |
||
26 | { |
||
27 | use NotSupportingVisibilityTrait; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $accessKey; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $secretKey; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $bucket; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $domain; |
||
48 | |||
49 | /** |
||
50 | * @var \Qiniu\Auth |
||
51 | */ |
||
52 | protected $authManager; |
||
53 | |||
54 | /** |
||
55 | * @var \Qiniu\Storage\UploadManager |
||
56 | */ |
||
57 | protected $uploadManager; |
||
58 | |||
59 | /** |
||
60 | * @var \Qiniu\Storage\BucketManager |
||
61 | */ |
||
62 | protected $bucketManager; |
||
63 | |||
64 | /** |
||
65 | * @var \Qiniu\Cdn\CdnManager |
||
66 | */ |
||
67 | protected $cdnManager; |
||
68 | |||
69 | /** |
||
70 | * QiniuAdapter constructor. |
||
71 | * |
||
72 | * @param string $accessKey |
||
73 | * @param string $secretKey |
||
74 | * @param string $bucket |
||
75 | * @param string $domain |
||
76 | */ |
||
77 | 1 | public function __construct($accessKey, $secretKey, $bucket, $domain) |
|
84 | |||
85 | /** |
||
86 | * Write a new file. |
||
87 | * |
||
88 | * @param string $path |
||
89 | * @param string $contents |
||
90 | * @param Config $config Config object |
||
91 | * |
||
92 | * @return array|false false on failure file meta data on success |
||
93 | */ |
||
94 | 2 | public function write($path, $contents, Config $config) |
|
116 | |||
117 | /** |
||
118 | * Write a new file using a stream. |
||
119 | * |
||
120 | * @param string $path |
||
121 | * @param resource $resource |
||
122 | * @param Config $config Config object |
||
123 | * |
||
124 | * @return array|false false on failure file meta data on success |
||
125 | */ |
||
126 | 1 | public function writeStream($path, $resource, Config $config) |
|
142 | |||
143 | /** |
||
144 | * Update a file. |
||
145 | * |
||
146 | * @param string $path |
||
147 | * @param string $contents |
||
148 | * @param Config $config Config object |
||
149 | * |
||
150 | * @return array|false false on failure file meta data on success |
||
151 | */ |
||
152 | 1 | public function update($path, $contents, Config $config) |
|
158 | |||
159 | /** |
||
160 | * Update a file using a stream. |
||
161 | * |
||
162 | * @param string $path |
||
163 | * @param resource $resource |
||
164 | * @param Config $config Config object |
||
165 | * |
||
166 | * @return array|false false on failure file meta data on success |
||
167 | */ |
||
168 | 1 | public function updateStream($path, $resource, Config $config) |
|
174 | |||
175 | /** |
||
176 | * Rename a file. |
||
177 | * |
||
178 | * @param string $path |
||
179 | * @param string $newPath |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | 1 | public function rename($path, $newPath) |
|
189 | |||
190 | /** |
||
191 | * Copy a file. |
||
192 | * |
||
193 | * @param string $path |
||
194 | * @param string $newPath |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | 1 | public function copy($path, $newPath) |
|
204 | |||
205 | /** |
||
206 | * Delete a file. |
||
207 | * |
||
208 | * @param string $path |
||
209 | * |
||
210 | * @return bool |
||
211 | */ |
||
212 | 1 | public function delete($path) |
|
218 | |||
219 | /** |
||
220 | * Delete a directory. |
||
221 | * |
||
222 | * @param string $directory |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | 1 | public function deleteDir($directory) |
|
230 | |||
231 | /** |
||
232 | * Create a directory. |
||
233 | * |
||
234 | * @param string $directory directory name |
||
235 | * @param Config $config |
||
236 | * |
||
237 | * @return array|false |
||
238 | */ |
||
239 | 1 | public function createDir($directory, Config $config) |
|
243 | |||
244 | /** |
||
245 | * Check whether a file exists. |
||
246 | * |
||
247 | * @param string $path |
||
248 | * |
||
249 | * @return array|bool|null |
||
250 | */ |
||
251 | 1 | public function has($path) |
|
257 | |||
258 | /** |
||
259 | * Get resource url. |
||
260 | * |
||
261 | * @param string $path |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | 3 | public function getUrl($path) |
|
269 | |||
270 | /** |
||
271 | * Read a file. |
||
272 | * |
||
273 | * @param string $path |
||
274 | * |
||
275 | * @return array|false |
||
276 | */ |
||
277 | 1 | public function read($path) |
|
283 | |||
284 | /** |
||
285 | * Read a file as a stream. |
||
286 | * |
||
287 | * @param string $path |
||
288 | * |
||
289 | * @return array|false |
||
290 | */ |
||
291 | 1 | public function readStream($path) |
|
301 | |||
302 | /** |
||
303 | * List contents of a directory. |
||
304 | * |
||
305 | * @param string $directory |
||
306 | * @param bool $recursive |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | 1 | public function listContents($directory = '', $recursive = false) |
|
322 | |||
323 | /** |
||
324 | * Get all the meta data of a file or directory. |
||
325 | * |
||
326 | * @param string $path |
||
327 | * |
||
328 | * @return array|false |
||
329 | */ |
||
330 | 1 | public function getMetadata($path) |
|
337 | |||
338 | /** |
||
339 | * Get the size of a file. |
||
340 | * |
||
341 | * @param string $path |
||
342 | * |
||
343 | * @return array|false |
||
344 | */ |
||
345 | 1 | public function getSize($path) |
|
349 | |||
350 | /** |
||
351 | * Fetch url to bucket. |
||
352 | * |
||
353 | * @param string $path |
||
354 | * @param string $url |
||
355 | * |
||
356 | * @return array|false |
||
357 | */ |
||
358 | public function fetch($path, $url) |
||
368 | |||
369 | |||
370 | /** |
||
371 | * Get private file download url. |
||
372 | * |
||
373 | * @param $path |
||
374 | * @param int $expires |
||
375 | * |
||
376 | * @return string |
||
377 | */ |
||
378 | 1 | public function privateDownloadUrl($path, $expires = 3600) |
|
384 | |||
385 | |||
386 | /** |
||
387 | * Refresh file cache. |
||
388 | * |
||
389 | * @param $path |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | 1 | public function refresh($path) |
|
404 | |||
405 | /** |
||
406 | * Get the mime-type of a file. |
||
407 | * |
||
408 | * @param string $path |
||
409 | * |
||
410 | * @return array|false |
||
411 | */ |
||
412 | 1 | public function getMimeType($path) |
|
422 | |||
423 | /** |
||
424 | * Get the timestamp of a file. |
||
425 | * |
||
426 | * @param string $path |
||
427 | * |
||
428 | * @return array|false |
||
429 | */ |
||
430 | 1 | public function getTimestamp($path) |
|
434 | |||
435 | /** |
||
436 | * @param \Qiniu\Storage\BucketManager $manager |
||
437 | * |
||
438 | * @return $this |
||
439 | */ |
||
440 | 1 | public function setBucketManager(BucketManager $manager) |
|
446 | |||
447 | /** |
||
448 | * @param \Qiniu\Storage\UploadManager $manager |
||
449 | * |
||
450 | * @return $this |
||
451 | */ |
||
452 | 1 | public function setUploadManager(UploadManager $manager) |
|
458 | |||
459 | /** |
||
460 | * @param \Qiniu\Auth $manager |
||
461 | * |
||
462 | * @return $this |
||
463 | */ |
||
464 | 1 | public function setAuthManager(Auth $manager) |
|
470 | |||
471 | /** |
||
472 | * @param CdnManager $manager |
||
473 | * |
||
474 | * @return $this |
||
475 | */ |
||
476 | public function setCdnManager(CdnManager $manager) |
||
482 | |||
483 | /** |
||
484 | * @return \Qiniu\Storage\BucketManager |
||
485 | */ |
||
486 | 1 | public function getBucketManager() |
|
490 | |||
491 | /** |
||
492 | * @return \Qiniu\Auth |
||
493 | */ |
||
494 | 1 | public function getAuthManager() |
|
498 | |||
499 | /** |
||
500 | * @return \Qiniu\Storage\UploadManager |
||
501 | */ |
||
502 | 1 | public function getUploadManager() |
|
506 | |||
507 | /** |
||
508 | * @return \Qiniu\Cdn\CdnManager |
||
509 | */ |
||
510 | public function getCdnManager() |
||
514 | |||
515 | /** |
||
516 | * Get the upload token. |
||
517 | * |
||
518 | * @param string|null $key |
||
519 | * @param int $expires |
||
520 | * @param string|null $policy |
||
521 | * @param string|null $strictPolice |
||
522 | * |
||
523 | * @return string |
||
524 | */ |
||
525 | 1 | public function getUploadToken($key = null, $expires = 3600, $policy = null, $strictPolice = null) |
|
529 | |||
530 | /** |
||
531 | * @param array $stats |
||
532 | * |
||
533 | * @return array |
||
534 | */ |
||
535 | 2 | protected function normalizeFileInfo(array $stats) |
|
544 | |||
545 | /** |
||
546 | * @param string $domain |
||
547 | * |
||
548 | * @return string |
||
549 | */ |
||
550 | 4 | protected function normalizeHost($domain) |
|
558 | } |
||
559 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: