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) |
|
117 | |||
118 | /** |
||
119 | * Write a new file using a stream. |
||
120 | * |
||
121 | * @param string $path |
||
122 | * @param resource $resource |
||
123 | * @param Config $config Config object |
||
124 | * |
||
125 | * @return array|false false on failure file meta data on success |
||
126 | */ |
||
127 | 1 | public function writeStream($path, $resource, Config $config) |
|
143 | |||
144 | /** |
||
145 | * Update a file. |
||
146 | * |
||
147 | * @param string $path |
||
148 | * @param string $contents |
||
149 | * @param Config $config Config object |
||
150 | * |
||
151 | * @return array|false false on failure file meta data on success |
||
152 | */ |
||
153 | 1 | public function update($path, $contents, Config $config) |
|
159 | |||
160 | /** |
||
161 | * Update a file using a stream. |
||
162 | * |
||
163 | * @param string $path |
||
164 | * @param resource $resource |
||
165 | * @param Config $config Config object |
||
166 | * |
||
167 | * @return array|false false on failure file meta data on success |
||
168 | */ |
||
169 | 1 | public function updateStream($path, $resource, Config $config) |
|
175 | |||
176 | /** |
||
177 | * Rename a file. |
||
178 | * |
||
179 | * @param string $path |
||
180 | * @param string $newPath |
||
181 | * |
||
182 | * @return bool |
||
183 | */ |
||
184 | 1 | public function rename($path, $newPath) |
|
190 | |||
191 | /** |
||
192 | * Copy a file. |
||
193 | * |
||
194 | * @param string $path |
||
195 | * @param string $newPath |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | 1 | public function copy($path, $newPath) |
|
205 | |||
206 | /** |
||
207 | * Delete a file. |
||
208 | * |
||
209 | * @param string $path |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | 1 | public function delete($path) |
|
219 | |||
220 | /** |
||
221 | * Delete a directory. |
||
222 | * |
||
223 | * @param string $directory |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | 1 | public function deleteDir($directory) |
|
231 | |||
232 | /** |
||
233 | * Create a directory. |
||
234 | * |
||
235 | * @param string $directory directory name |
||
236 | * @param Config $config |
||
237 | * |
||
238 | * @return array|false |
||
239 | */ |
||
240 | 1 | public function createDir($directory, Config $config) |
|
244 | |||
245 | /** |
||
246 | * Check whether a file exists. |
||
247 | * |
||
248 | * @param string $path |
||
249 | * |
||
250 | * @return array|bool|null |
||
251 | */ |
||
252 | 1 | public function has($path) |
|
258 | |||
259 | /** |
||
260 | * Get resource url. |
||
261 | * |
||
262 | * @param string $path |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | 3 | public function getUrl($path) |
|
270 | |||
271 | /** |
||
272 | * Read a file. |
||
273 | * |
||
274 | * @param string $path |
||
275 | * |
||
276 | * @return array|false |
||
277 | */ |
||
278 | 1 | public function read($path) |
|
284 | |||
285 | /** |
||
286 | * Read a file as a stream. |
||
287 | * |
||
288 | * @param string $path |
||
289 | * |
||
290 | * @return array|false |
||
291 | */ |
||
292 | 1 | public function readStream($path) |
|
302 | |||
303 | /** |
||
304 | * List contents of a directory. |
||
305 | * |
||
306 | * @param string $directory |
||
307 | * @param bool $recursive |
||
308 | * |
||
309 | * @return array |
||
310 | */ |
||
311 | 1 | public function listContents($directory = '', $recursive = false) |
|
323 | |||
324 | /** |
||
325 | * Get all the meta data of a file or directory. |
||
326 | * |
||
327 | * @param string $path |
||
328 | * |
||
329 | * @return array|false |
||
330 | */ |
||
331 | 1 | public function getMetadata($path) |
|
338 | |||
339 | /** |
||
340 | * Get the size of a file. |
||
341 | * |
||
342 | * @param string $path |
||
343 | * |
||
344 | * @return array|false |
||
345 | */ |
||
346 | 1 | public function getSize($path) |
|
350 | |||
351 | /** |
||
352 | * Fetch url to bucket. |
||
353 | * |
||
354 | * @param string $path |
||
355 | * @param string $url |
||
356 | * |
||
357 | * @return array|false |
||
358 | */ |
||
359 | public function fetch($path, $url) |
||
369 | |||
370 | /** |
||
371 | * Get private file download url. |
||
372 | * |
||
373 | * @param string $path |
||
374 | * @param int $expires |
||
375 | * |
||
376 | * @return string |
||
377 | */ |
||
378 | 1 | public function privateDownloadUrl($path, $expires = 3600) |
|
384 | |||
385 | /** |
||
386 | * Refresh file cache. |
||
387 | * |
||
388 | * @param string|array $path |
||
389 | * |
||
390 | * @return array |
||
391 | */ |
||
392 | 1 | public function refresh($path) |
|
403 | |||
404 | /** |
||
405 | * Get the mime-type of a file. |
||
406 | * |
||
407 | * @param string $path |
||
408 | * |
||
409 | * @return array|false |
||
410 | */ |
||
411 | 1 | public function getMimeType($path) |
|
421 | |||
422 | /** |
||
423 | * Get the timestamp of a file. |
||
424 | * |
||
425 | * @param string $path |
||
426 | * |
||
427 | * @return array|false |
||
428 | */ |
||
429 | 1 | public function getTimestamp($path) |
|
433 | |||
434 | /** |
||
435 | * @param \Qiniu\Storage\BucketManager $manager |
||
436 | * |
||
437 | * @return $this |
||
438 | */ |
||
439 | 1 | public function setBucketManager(BucketManager $manager) |
|
445 | |||
446 | /** |
||
447 | * @param \Qiniu\Storage\UploadManager $manager |
||
448 | * |
||
449 | * @return $this |
||
450 | */ |
||
451 | 1 | public function setUploadManager(UploadManager $manager) |
|
457 | |||
458 | /** |
||
459 | * @param \Qiniu\Auth $manager |
||
460 | * |
||
461 | * @return $this |
||
462 | */ |
||
463 | 1 | public function setAuthManager(Auth $manager) |
|
469 | |||
470 | /** |
||
471 | * @param CdnManager $manager |
||
472 | * |
||
473 | * @return $this |
||
474 | */ |
||
475 | public function setCdnManager(CdnManager $manager) |
||
481 | |||
482 | /** |
||
483 | * @return \Qiniu\Storage\BucketManager |
||
484 | */ |
||
485 | 1 | public function getBucketManager() |
|
489 | |||
490 | /** |
||
491 | * @return \Qiniu\Auth |
||
492 | */ |
||
493 | 1 | public function getAuthManager() |
|
497 | |||
498 | /** |
||
499 | * @return \Qiniu\Storage\UploadManager |
||
500 | */ |
||
501 | 1 | public function getUploadManager() |
|
505 | |||
506 | /** |
||
507 | * @return \Qiniu\Cdn\CdnManager |
||
508 | */ |
||
509 | public function getCdnManager() |
||
513 | |||
514 | /** |
||
515 | * Get the upload token. |
||
516 | * |
||
517 | * @param string|null $key |
||
518 | * @param int $expires |
||
519 | * @param string|null $policy |
||
520 | * @param string|null $strictPolice |
||
521 | * |
||
522 | * @return string |
||
523 | */ |
||
524 | 1 | public function getUploadToken($key = null, $expires = 3600, $policy = null, $strictPolice = null) |
|
528 | |||
529 | /** |
||
530 | * @param array $stats |
||
531 | * |
||
532 | * @return array |
||
533 | */ |
||
534 | 2 | protected function normalizeFileInfo(array $stats) |
|
543 | |||
544 | /** |
||
545 | * @param string $domain |
||
546 | * |
||
547 | * @return string |
||
548 | */ |
||
549 | 4 | protected function normalizeHost($domain) |
|
557 | } |
||
558 |
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: