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 |
||
19 | class QiniuAdapter extends AbstractAdapter |
||
20 | { |
||
21 | use NotSupportingVisibilityTrait; |
||
22 | |||
23 | protected $accessKey; |
||
24 | protected $secretKey; |
||
25 | protected $bucket; |
||
26 | protected $domain; |
||
27 | |||
28 | protected $authManager; |
||
29 | protected $uploadManager; |
||
30 | protected $bucketManager; |
||
31 | |||
32 | /** |
||
33 | * QiniuAdapter constructor. |
||
34 | * |
||
35 | * @param string $accessKey |
||
36 | * @param string $secretKey |
||
37 | * @param string $bucket |
||
38 | * @param string $domain |
||
39 | */ |
||
40 | 1 | public function __construct($accessKey, $secretKey, $bucket, $domain) |
|
47 | |||
48 | /** |
||
49 | * Write a new file. |
||
50 | * |
||
51 | * @param string $path |
||
52 | * @param string $contents |
||
53 | * @param Config $config Config object |
||
54 | * |
||
55 | * @return array|false false on failure file meta data on success |
||
56 | */ |
||
57 | 2 | public function write($path, $contents, Config $config) |
|
79 | |||
80 | /** |
||
81 | * Write a new file using a stream. |
||
82 | * |
||
83 | * @param string $path |
||
84 | * @param resource $resource |
||
85 | * @param Config $config Config object |
||
86 | * |
||
87 | * @return array|false false on failure file meta data on success |
||
88 | */ |
||
89 | 1 | public function writeStream($path, $resource, Config $config) |
|
105 | |||
106 | /** |
||
107 | * Update a file. |
||
108 | * |
||
109 | * @param string $path |
||
110 | * @param string $contents |
||
111 | * @param Config $config Config object |
||
112 | * |
||
113 | * @return array|false false on failure file meta data on success |
||
114 | */ |
||
115 | 1 | public function update($path, $contents, Config $config) |
|
121 | |||
122 | /** |
||
123 | * Update a file using a stream. |
||
124 | * |
||
125 | * @param string $path |
||
126 | * @param resource $resource |
||
127 | * @param Config $config Config object |
||
128 | * |
||
129 | * @return array|false false on failure file meta data on success |
||
130 | */ |
||
131 | 1 | public function updateStream($path, $resource, Config $config) |
|
137 | |||
138 | /** |
||
139 | * Rename a file. |
||
140 | * |
||
141 | * @param string $path |
||
142 | * @param string $newpath |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | 1 | public function rename($path, $newpath) |
|
152 | |||
153 | /** |
||
154 | * Copy a file. |
||
155 | * |
||
156 | * @param string $path |
||
157 | * @param string $newpath |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | 1 | public function copy($path, $newpath) |
|
167 | |||
168 | /** |
||
169 | * Delete a file. |
||
170 | * |
||
171 | * @param string $path |
||
172 | * |
||
173 | * @return bool |
||
174 | */ |
||
175 | 1 | public function delete($path) |
|
181 | |||
182 | /** |
||
183 | * Delete a directory. |
||
184 | * |
||
185 | * @param string $dirname |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | 1 | public function deleteDir($dirname) |
|
193 | |||
194 | /** |
||
195 | * Create a directory. |
||
196 | * |
||
197 | * @param string $dirname directory name |
||
198 | * @param Config $config |
||
199 | * |
||
200 | * @return array|false |
||
201 | */ |
||
202 | 1 | public function createDir($dirname, Config $config) |
|
206 | |||
207 | /** |
||
208 | * Check whether a file exists. |
||
209 | * |
||
210 | * @param string $path |
||
211 | * |
||
212 | * @return array|bool|null |
||
213 | */ |
||
214 | 1 | public function has($path) |
|
220 | |||
221 | /** |
||
222 | * Get resource url. |
||
223 | * |
||
224 | * @param string $path |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | 1 | public function getUrl($path) |
|
232 | |||
233 | /** |
||
234 | * Read a file. |
||
235 | * |
||
236 | * @param string $path |
||
237 | * |
||
238 | * @return array|false |
||
239 | */ |
||
240 | 1 | public function read($path) |
|
246 | |||
247 | /** |
||
248 | * Read a file as a stream. |
||
249 | * |
||
250 | * @param string $path |
||
251 | * |
||
252 | * @return array|false |
||
253 | */ |
||
254 | 1 | public function readStream($path) |
|
264 | |||
265 | /** |
||
266 | * List contents of a directory. |
||
267 | * |
||
268 | * @param string $directory |
||
269 | * @param bool $recursive |
||
270 | * |
||
271 | * @return array |
||
272 | */ |
||
273 | 1 | public function listContents($directory = '', $recursive = false) |
|
285 | |||
286 | /** |
||
287 | * Get all the meta data of a file or directory. |
||
288 | * |
||
289 | * @param string $path |
||
290 | * |
||
291 | * @return array|false |
||
292 | */ |
||
293 | 1 | public function getMetadata($path) |
|
300 | |||
301 | /** |
||
302 | * Get the size of a file. |
||
303 | * |
||
304 | * @param string $path |
||
305 | * |
||
306 | * @return array|false |
||
307 | */ |
||
308 | 1 | public function getSize($path) |
|
312 | |||
313 | /** |
||
314 | * Fetch url to bucket. |
||
315 | * |
||
316 | * @param string $path |
||
317 | * @param string $url |
||
318 | * |
||
319 | * @return array|false |
||
320 | */ |
||
321 | public function fetch($path, $url) |
||
331 | |||
332 | /** |
||
333 | * Get the mimetype of a file. |
||
334 | * |
||
335 | * @param string $path |
||
336 | * |
||
337 | * @return array|false |
||
338 | */ |
||
339 | 1 | public function getMimetype($path) |
|
349 | |||
350 | /** |
||
351 | * Get the timestamp of a file. |
||
352 | * |
||
353 | * @param string $path |
||
354 | * |
||
355 | * @return array|false |
||
356 | */ |
||
357 | 1 | public function getTimestamp($path) |
|
361 | |||
362 | /** |
||
363 | * @param \Qiniu\Storage\BucketManager $manager |
||
364 | * |
||
365 | * @return $this |
||
366 | */ |
||
367 | 1 | public function setBucketManager(BucketManager $manager) |
|
373 | |||
374 | /** |
||
375 | * @param \Qiniu\Storage\UploadManager $manager |
||
376 | * |
||
377 | * @return $this |
||
378 | */ |
||
379 | 1 | public function setUploadManager(UploadManager $manager) |
|
385 | |||
386 | /** |
||
387 | * @param \Qiniu\Auth $manager |
||
388 | * |
||
389 | * @return $this |
||
390 | */ |
||
391 | 1 | public function setAuthManager(Auth $manager) |
|
397 | |||
398 | /** |
||
399 | * @return \Qiniu\Storage\BucketManager |
||
400 | */ |
||
401 | 1 | public function getBucketManager() |
|
405 | |||
406 | /** |
||
407 | * @return \Qiniu\Auth |
||
408 | */ |
||
409 | 1 | public function getAuthManager() |
|
413 | |||
414 | /** |
||
415 | * @return \Qiniu\Storage\UploadManager |
||
416 | */ |
||
417 | 1 | public function getUploadManager() |
|
421 | |||
422 | /** |
||
423 | * @param array $stats |
||
424 | * |
||
425 | * @return array |
||
426 | */ |
||
427 | 2 | protected function normalizeFileInfo(array $stats) |
|
436 | |||
437 | /** |
||
438 | * @param string $domain |
||
439 | * |
||
440 | * @return string |
||
441 | */ |
||
442 | 2 | protected function normalizeHost($domain) |
|
450 | } |
||
451 |
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: