Complex classes like AliyunOssAdapter 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 AliyunOssAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class AliyunOssAdapter extends AbstractAdapter implements CanOverwriteFiles |
||
20 | { |
||
21 | /** |
||
22 | * @var OssClient |
||
23 | */ |
||
24 | protected $ossClient; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $bucket; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $options = []; |
||
35 | |||
36 | /** |
||
37 | * Constructor. |
||
38 | * |
||
39 | * @param OssClient $ossClient |
||
40 | * @param string $bucket |
||
41 | * @param string $prefix |
||
42 | * @param array $options |
||
43 | */ |
||
44 | 86 | public function __construct(OssClient $ossClient, $bucket, $prefix = '', array $options = []) |
|
51 | |||
52 | /** |
||
53 | * Get the OssClient bucket. |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | 4 | public function getBucket() |
|
61 | |||
62 | /** |
||
63 | * Set the OssClient bucket. |
||
64 | * |
||
65 | * @param string $bucket |
||
66 | */ |
||
67 | 2 | public function setBucket($bucket) |
|
71 | |||
72 | /** |
||
73 | * Get the OssClient instance. |
||
74 | * |
||
75 | * @return OssClient |
||
76 | */ |
||
77 | 2 | public function getClient() |
|
81 | |||
82 | /** |
||
83 | * Write a new file. |
||
84 | * |
||
85 | * @param string $path |
||
86 | * @param string $contents |
||
87 | * @param Config $config Config object |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | 4 | public function write($path, $contents, Config $config) |
|
95 | |||
96 | /** |
||
97 | * Update a file. |
||
98 | * |
||
99 | * @param string $path |
||
100 | * @param string $contents |
||
101 | * @param Config $config Config object |
||
102 | * |
||
103 | * @return array |
||
104 | */ |
||
105 | 2 | public function update($path, $contents, Config $config) |
|
109 | |||
110 | /** |
||
111 | * Write a new file using a stream. |
||
112 | * |
||
113 | * @param string $path |
||
114 | * @param resource $resource |
||
115 | * @param Config $config Config object |
||
116 | * |
||
117 | * @return array|false false on failure file meta data on success |
||
118 | */ |
||
119 | 2 | public function writeStream($path, $resource, Config $config) |
|
123 | |||
124 | /** |
||
125 | * Update a file using a stream. |
||
126 | * |
||
127 | * @param string $path |
||
128 | * @param resource $resource |
||
129 | * @param Config $config Config object |
||
130 | * |
||
131 | * @return array|false false on failure file meta data on success |
||
132 | */ |
||
133 | 2 | 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 | 4 | public function rename($path, $newpath) |
|
154 | |||
155 | /** |
||
156 | * Copy a file. |
||
157 | * |
||
158 | * @param string $path |
||
159 | * @param string $newpath |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | 8 | public function copy($path, $newpath) |
|
179 | |||
180 | /** |
||
181 | * Delete a file. |
||
182 | * |
||
183 | * @param string $path |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | 4 | public function delete($path) |
|
195 | |||
196 | /** |
||
197 | * Delete a directory. |
||
198 | * |
||
199 | * @param string $dirname |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | 2 | public function deleteDir($dirname) |
|
207 | |||
208 | /** |
||
209 | * Create a directory. |
||
210 | * |
||
211 | * @param string $dirname directory name |
||
212 | * @param Config $config |
||
213 | * |
||
214 | * @return array|false |
||
215 | */ |
||
216 | 4 | public function createDir($dirname, Config $config) |
|
229 | |||
230 | /** |
||
231 | * Set the visibility for a file. |
||
232 | * |
||
233 | * @param string $path |
||
234 | * @param string $visibility |
||
235 | * |
||
236 | * @return array|false file meta data |
||
237 | */ |
||
238 | 6 | public function setVisibility($path, $visibility) |
|
252 | |||
253 | /** |
||
254 | * Check whether a file exists. |
||
255 | * |
||
256 | * @param string $path |
||
257 | * |
||
258 | * @return bool |
||
259 | */ |
||
260 | 12 | public function has($path) |
|
270 | |||
271 | /** |
||
272 | * Read a file. |
||
273 | * |
||
274 | * @param string $path |
||
275 | * |
||
276 | * @return array|false |
||
277 | */ |
||
278 | 6 | public function read($path) |
|
290 | |||
291 | /** |
||
292 | * Read a file as a stream. |
||
293 | * |
||
294 | * @param string $path |
||
295 | * |
||
296 | * @return array|false |
||
297 | */ |
||
298 | 4 | public function readStream($path) |
|
312 | |||
313 | /** |
||
314 | * List contents of a directory. |
||
315 | * |
||
316 | * @param string $directory |
||
317 | * @param bool $recursive |
||
318 | * |
||
319 | * @return array|false |
||
320 | */ |
||
321 | 6 | public function listContents($directory = '', $recursive = false) |
|
348 | |||
349 | /** |
||
350 | * @param array $options |
||
351 | * |
||
352 | * @return array |
||
353 | * @throws OssException |
||
354 | */ |
||
355 | 6 | protected function retrieveListing(array $options) |
|
379 | |||
380 | /** |
||
381 | * Get all the meta data of a file or directory. |
||
382 | * |
||
383 | * @param string $path |
||
384 | * |
||
385 | * @return array|false |
||
386 | */ |
||
387 | 12 | public function getMetadata($path) |
|
397 | |||
398 | /** |
||
399 | * Get the size of a file. |
||
400 | * |
||
401 | * @param string $path |
||
402 | * |
||
403 | * @return array|false |
||
404 | */ |
||
405 | 2 | public function getSize($path) |
|
415 | |||
416 | /** |
||
417 | * Get the mimetype of a file. |
||
418 | * |
||
419 | * @param string $path |
||
420 | * |
||
421 | * @return array|false |
||
422 | */ |
||
423 | 2 | public function getMimetype($path) |
|
436 | |||
437 | /** |
||
438 | * Get the last modified time of a file as a timestamp. |
||
439 | * |
||
440 | * @param string $path |
||
441 | * |
||
442 | * @return array|false |
||
443 | */ |
||
444 | 2 | public function getTimestamp($path) |
|
454 | |||
455 | /** |
||
456 | * Get the visibility of a file. |
||
457 | * |
||
458 | * @param string $path |
||
459 | * |
||
460 | * @return array|false |
||
461 | */ |
||
462 | 6 | public function getVisibility($path) |
|
472 | |||
473 | /** |
||
474 | * {@inheritdoc} |
||
475 | */ |
||
476 | 74 | public function applyPathPrefix($path) |
|
480 | |||
481 | /** |
||
482 | * {@inheritdoc} |
||
483 | */ |
||
484 | 86 | public function setPathPrefix($prefix) |
|
490 | |||
491 | /** |
||
492 | * Get the object meta. |
||
493 | * |
||
494 | * @param $path |
||
495 | * |
||
496 | * @return array|bool |
||
497 | */ |
||
498 | 12 | protected function getObjectMeta($path) |
|
510 | |||
511 | /** |
||
512 | * Upload an object. |
||
513 | * |
||
514 | * @param string $path |
||
515 | * @param string $body |
||
516 | * @param Config $config |
||
517 | * |
||
518 | * @return array|false |
||
519 | * @throws \InvalidArgumentException |
||
520 | */ |
||
521 | 10 | protected function upload($path, $body, Config $config) |
|
539 | |||
540 | /** |
||
541 | * Get options from the config. |
||
542 | * |
||
543 | * @param Config $config |
||
544 | * @param array $keys |
||
545 | * |
||
546 | * @return array |
||
547 | */ |
||
548 | 14 | protected function getOptionsFromConfig(Config $config, $keys = []) |
|
560 | |||
561 | /** |
||
562 | * Normalize the object result array. |
||
563 | * |
||
564 | * @param array $response |
||
565 | * @param null $path |
||
566 | * |
||
567 | * @return array |
||
568 | */ |
||
569 | 26 | protected function normalizeResponse(array $response, $path = null) |
|
590 | |||
591 | /** |
||
592 | * @param $location |
||
593 | * |
||
594 | * @return bool |
||
595 | */ |
||
596 | 10 | protected function doesDirectoryExist($location) |
|
615 | } |
||
616 |
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: