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 | 1 | public function write($path, $contents, Config $config) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Write a new file using a stream. |
||
| 74 | * |
||
| 75 | * @param string $path |
||
| 76 | * @param resource $resource |
||
| 77 | * @param Config $config Config object |
||
| 78 | * |
||
| 79 | * @return array|false false on failure file meta data on success |
||
| 80 | */ |
||
| 81 | 1 | public function writeStream($path, $resource, Config $config) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Update a file. |
||
| 100 | * |
||
| 101 | * @param string $path |
||
| 102 | * @param string $contents |
||
| 103 | * @param Config $config Config object |
||
| 104 | * |
||
| 105 | * @return array|false false on failure file meta data on success |
||
| 106 | */ |
||
| 107 | 1 | public function update($path, $contents, Config $config) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Update a file using a stream. |
||
| 116 | * |
||
| 117 | * @param string $path |
||
| 118 | * @param resource $resource |
||
| 119 | * @param Config $config Config object |
||
| 120 | * |
||
| 121 | * @return array|false false on failure file meta data on success |
||
| 122 | */ |
||
| 123 | 1 | public function updateStream($path, $resource, Config $config) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Rename a file. |
||
| 132 | * |
||
| 133 | * @param string $path |
||
| 134 | * @param string $newpath |
||
| 135 | * |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | 1 | public function rename($path, $newpath) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Copy a file. |
||
| 147 | * |
||
| 148 | * @param string $path |
||
| 149 | * @param string $newpath |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | 1 | public function copy($path, $newpath) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Delete a file. |
||
| 162 | * |
||
| 163 | * @param string $path |
||
| 164 | * |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | 1 | public function delete($path) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Delete a directory. |
||
| 176 | * |
||
| 177 | * @param string $dirname |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | 1 | public function deleteDir($dirname) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Create a directory. |
||
| 188 | * |
||
| 189 | * @param string $dirname directory name |
||
| 190 | * @param Config $config |
||
| 191 | * |
||
| 192 | * @return array|false |
||
| 193 | */ |
||
| 194 | 1 | public function createDir($dirname, Config $config) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Check whether a file exists. |
||
| 201 | * |
||
| 202 | * @param string $path |
||
| 203 | * |
||
| 204 | * @return array|bool|null |
||
| 205 | */ |
||
| 206 | 1 | public function has($path) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Get resource url. |
||
| 215 | * |
||
| 216 | * @param string $path |
||
| 217 | * |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | 1 | public function getUrl($path) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Read a file. |
||
| 227 | * |
||
| 228 | * @param string $path |
||
| 229 | * |
||
| 230 | * @return array|false |
||
| 231 | */ |
||
| 232 | 1 | public function read($path) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Read a file as a stream. |
||
| 241 | * |
||
| 242 | * @param string $path |
||
| 243 | * |
||
| 244 | * @return array|false |
||
| 245 | */ |
||
| 246 | 1 | public function readStream($path) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * List contents of a directory. |
||
| 259 | * |
||
| 260 | * @param string $directory |
||
| 261 | * @param bool $recursive |
||
| 262 | * |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | 1 | public function listContents($directory = '', $recursive = false) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Get all the meta data of a file or directory. |
||
| 280 | * |
||
| 281 | * @param string $path |
||
| 282 | * |
||
| 283 | * @return array|false |
||
| 284 | */ |
||
| 285 | 1 | public function getMetadata($path) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Get the size of a file. |
||
| 295 | * |
||
| 296 | * @param string $path |
||
| 297 | * |
||
| 298 | * @return array|false |
||
| 299 | */ |
||
| 300 | 1 | public function getSize($path) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Get the mimetype of a file. |
||
| 307 | * |
||
| 308 | * @param string $path |
||
| 309 | * |
||
| 310 | * @return array|false |
||
| 311 | */ |
||
| 312 | 1 | public function getMimetype($path) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Get the timestamp of a file. |
||
| 325 | * |
||
| 326 | * @param string $path |
||
| 327 | * |
||
| 328 | * @return array|false |
||
| 329 | */ |
||
| 330 | 1 | public function getTimestamp($path) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @param \Qiniu\Storage\BucketManager $manager |
||
| 337 | * |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | 1 | public function setBucketManager(BucketManager $manager) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * @param \Qiniu\Storage\UploadManager $manager |
||
| 349 | * |
||
| 350 | * @return $this |
||
| 351 | */ |
||
| 352 | 1 | public function setUploadManager(UploadManager $manager) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @param \Qiniu\Auth $manager |
||
| 361 | * |
||
| 362 | * @return $this |
||
| 363 | */ |
||
| 364 | 1 | public function setAuthManager(Auth $manager) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * @return \Qiniu\Storage\BucketManager |
||
| 373 | */ |
||
| 374 | 1 | public function getBucketManager() |
|
| 378 | |||
| 379 | /** |
||
| 380 | * @return \Qiniu\Auth |
||
| 381 | */ |
||
| 382 | 1 | public function getAuthManager() |
|
| 386 | |||
| 387 | /** |
||
| 388 | * @return \Qiniu\Storage\UploadManager |
||
| 389 | */ |
||
| 390 | 1 | public function getUploadManager() |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Get the upload token. |
||
| 397 | * |
||
| 398 | * @param null $key |
||
| 399 | * |
||
| 400 | * @param int $expires |
||
| 401 | * |
||
| 402 | * @param null $policy |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | public function getUploadToken($key = null, $expires = 3600, $policy = null) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param array $stats |
||
| 413 | * |
||
| 414 | * @return array |
||
| 415 | */ |
||
| 416 | 2 | protected function normalizeFileInfo(array $stats) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * @param string $domain |
||
| 428 | * |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | 2 | protected function normalizeHost($domain) |
|
| 439 | } |
||
| 440 |
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.