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 |
||
| 24 | class QiniuAdapter extends AbstractAdapter |
||
| 25 | { |
||
| 26 | use NotSupportingVisibilityTrait; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $accessKey; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $secretKey; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $bucket; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $domain; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var \Qiniu\Auth |
||
| 50 | */ |
||
| 51 | protected $authManager; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \Qiniu\Storage\UploadManager |
||
| 55 | */ |
||
| 56 | protected $uploadManager; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var \Qiniu\Storage\BucketManager |
||
| 60 | */ |
||
| 61 | protected $bucketManager; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * QiniuAdapter constructor. |
||
| 65 | * |
||
| 66 | * @param string $accessKey |
||
| 67 | * @param string $secretKey |
||
| 68 | * @param string $bucket |
||
| 69 | * @param string $domain |
||
| 70 | */ |
||
| 71 | 1 | public function __construct($accessKey, $secretKey, $bucket, $domain) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Write a new file. |
||
| 81 | * |
||
| 82 | * @param string $path |
||
| 83 | * @param string $contents |
||
| 84 | * @param Config $config Config object |
||
| 85 | * |
||
| 86 | * @return array|false false on failure file meta data on success |
||
| 87 | */ |
||
| 88 | 2 | public function write($path, $contents, Config $config) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Write a new file using a stream. |
||
| 113 | * |
||
| 114 | * @param string $path |
||
| 115 | * @param resource $resource |
||
| 116 | * @param Config $config Config object |
||
| 117 | * |
||
| 118 | * @return array|false false on failure file meta data on success |
||
| 119 | */ |
||
| 120 | 1 | public function writeStream($path, $resource, Config $config) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Update a file. |
||
| 139 | * |
||
| 140 | * @param string $path |
||
| 141 | * @param string $contents |
||
| 142 | * @param Config $config Config object |
||
| 143 | * |
||
| 144 | * @return array|false false on failure file meta data on success |
||
| 145 | */ |
||
| 146 | 1 | public function update($path, $contents, Config $config) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Update a file using a stream. |
||
| 155 | * |
||
| 156 | * @param string $path |
||
| 157 | * @param resource $resource |
||
| 158 | * @param Config $config Config object |
||
| 159 | * |
||
| 160 | * @return array|false false on failure file meta data on success |
||
| 161 | */ |
||
| 162 | 1 | public function updateStream($path, $resource, Config $config) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Rename a file. |
||
| 171 | * |
||
| 172 | * @param string $path |
||
| 173 | * @param string $newPath |
||
| 174 | * |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | 1 | public function rename($path, $newPath) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Copy a file. |
||
| 186 | * |
||
| 187 | * @param string $path |
||
| 188 | * @param string $newPath |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | 1 | public function copy($path, $newPath) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Delete a file. |
||
| 201 | * |
||
| 202 | * @param string $path |
||
| 203 | * |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | 1 | public function delete($path) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Delete a directory. |
||
| 215 | * |
||
| 216 | * @param string $directory |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | 1 | public function deleteDir($directory) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Create a directory. |
||
| 227 | * |
||
| 228 | * @param string $directory directory name |
||
| 229 | * @param Config $config |
||
| 230 | * |
||
| 231 | * @return array|false |
||
| 232 | */ |
||
| 233 | 1 | public function createDir($directory, Config $config) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Check whether a file exists. |
||
| 240 | * |
||
| 241 | * @param string $path |
||
| 242 | * |
||
| 243 | * @return array|bool|null |
||
| 244 | */ |
||
| 245 | 1 | public function has($path) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Get resource url. |
||
| 254 | * |
||
| 255 | * @param string $path |
||
| 256 | * |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | 1 | public function getUrl($path) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Read a file. |
||
| 266 | * |
||
| 267 | * @param string $path |
||
| 268 | * |
||
| 269 | * @return array|false |
||
| 270 | */ |
||
| 271 | 1 | public function read($path) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Read a file as a stream. |
||
| 280 | * |
||
| 281 | * @param string $path |
||
| 282 | * |
||
| 283 | * @return array|false |
||
| 284 | */ |
||
| 285 | 1 | public function readStream($path) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * List contents of a directory. |
||
| 298 | * |
||
| 299 | * @param string $directory |
||
| 300 | * @param bool $recursive |
||
| 301 | * |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | 1 | public function listContents($directory = '', $recursive = false) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Get all the meta data of a file or directory. |
||
| 319 | * |
||
| 320 | * @param string $path |
||
| 321 | * |
||
| 322 | * @return array|false |
||
| 323 | */ |
||
| 324 | 1 | public function getMetadata($path) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Get the size of a file. |
||
| 334 | * |
||
| 335 | * @param string $path |
||
| 336 | * |
||
| 337 | * @return array|false |
||
| 338 | */ |
||
| 339 | 1 | public function getSize($path) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Fetch url to bucket. |
||
| 346 | * |
||
| 347 | * @param string $path |
||
| 348 | * @param string $url |
||
| 349 | * |
||
| 350 | * @return array|false |
||
| 351 | */ |
||
| 352 | public function fetch($path, $url) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get the mime-type of a file. |
||
| 365 | * |
||
| 366 | * @param string $path |
||
| 367 | * |
||
| 368 | * @return array|false |
||
| 369 | */ |
||
| 370 | 1 | public function getMimeType($path) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Get the timestamp of a file. |
||
| 383 | * |
||
| 384 | * @param string $path |
||
| 385 | * |
||
| 386 | * @return array|false |
||
| 387 | */ |
||
| 388 | 1 | public function getTimestamp($path) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * @param \Qiniu\Storage\BucketManager $manager |
||
| 395 | * |
||
| 396 | * @return $this |
||
| 397 | */ |
||
| 398 | 1 | public function setBucketManager(BucketManager $manager) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * @param \Qiniu\Storage\UploadManager $manager |
||
| 407 | * |
||
| 408 | * @return $this |
||
| 409 | */ |
||
| 410 | 1 | public function setUploadManager(UploadManager $manager) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * @param \Qiniu\Auth $manager |
||
| 419 | * |
||
| 420 | * @return $this |
||
| 421 | */ |
||
| 422 | 1 | public function setAuthManager(Auth $manager) |
|
| 428 | |||
| 429 | /** |
||
| 430 | * @return \Qiniu\Storage\BucketManager |
||
| 431 | */ |
||
| 432 | 1 | public function getBucketManager() |
|
| 436 | |||
| 437 | /** |
||
| 438 | * @return \Qiniu\Auth |
||
| 439 | */ |
||
| 440 | 1 | public function getAuthManager() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * @return \Qiniu\Storage\UploadManager |
||
| 447 | */ |
||
| 448 | 1 | public function getUploadManager() |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Get the upload token. |
||
| 455 | * |
||
| 456 | * @param string|null $key |
||
| 457 | * @param int $expires |
||
| 458 | * @param bool $policy |
||
| 459 | * |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | 1 | public function getUploadToken($key = null, $expires = 3600, $policy = true) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * @param array $stats |
||
| 469 | * |
||
| 470 | * @return array |
||
| 471 | */ |
||
| 472 | 2 | protected function normalizeFileInfo(array $stats) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * @param string $domain |
||
| 484 | * |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | 2 | protected function normalizeHost($domain) |
|
| 495 | } |
||
| 496 |
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: