Complex classes like AwsS3Adapter 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 AwsS3Adapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class AwsS3Adapter extends AbstractAdapter implements CanOverwriteFiles |
||
| 20 | { |
||
| 21 | const PUBLIC_GRANT_URI = 'http://acs.amazonaws.com/groups/global/AllUsers'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected static $resultMap = [ |
||
| 27 | 'Body' => 'contents', |
||
| 28 | 'ContentLength' => 'size', |
||
| 29 | 'ContentType' => 'mimetype', |
||
| 30 | 'Size' => 'size', |
||
| 31 | 'Metadata' => 'metadata', |
||
| 32 | 'StorageClass' => 'storageclass', |
||
| 33 | 'ETag' => 'etag', |
||
| 34 | 'VersionId' => 'versionid' |
||
| 35 | ]; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected static $metaOptions = [ |
||
| 41 | 'ACL', |
||
| 42 | 'CacheControl', |
||
| 43 | 'ContentDisposition', |
||
| 44 | 'ContentEncoding', |
||
| 45 | 'ContentLength', |
||
| 46 | 'ContentType', |
||
| 47 | 'Expires', |
||
| 48 | 'GrantFullControl', |
||
| 49 | 'GrantRead', |
||
| 50 | 'GrantReadACP', |
||
| 51 | 'GrantWriteACP', |
||
| 52 | 'Metadata', |
||
| 53 | 'RequestPayer', |
||
| 54 | 'SSECustomerAlgorithm', |
||
| 55 | 'SSECustomerKey', |
||
| 56 | 'SSECustomerKeyMD5', |
||
| 57 | 'SSEKMSKeyId', |
||
| 58 | 'ServerSideEncryption', |
||
| 59 | 'StorageClass', |
||
| 60 | 'Tagging', |
||
| 61 | 'WebsiteRedirectLocation', |
||
| 62 | ]; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var S3ClientInterface |
||
| 66 | */ |
||
| 67 | protected $s3Client; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $bucket; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $options = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Constructor. |
||
| 81 | * |
||
| 82 | * @param S3ClientInterface $client |
||
| 83 | * @param string $bucket |
||
| 84 | * @param string $prefix |
||
| 85 | 76 | * @param array $options |
|
| 86 | */ |
||
| 87 | 76 | public function __construct(S3ClientInterface $client, $bucket, $prefix = '', array $options = []) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Get the S3Client bucket. |
||
| 97 | * |
||
| 98 | 4 | * @return string |
|
| 99 | */ |
||
| 100 | 4 | public function getBucket() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Set the S3Client bucket. |
||
| 107 | * |
||
| 108 | 2 | * @return string |
|
| 109 | */ |
||
| 110 | 2 | public function setBucket($bucket) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Get the S3Client instance. |
||
| 117 | * |
||
| 118 | 2 | * @return S3ClientInterface |
|
| 119 | */ |
||
| 120 | 2 | public function getClient() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Write a new file. |
||
| 127 | * |
||
| 128 | * @param string $path |
||
| 129 | * @param string $contents |
||
| 130 | * @param Config $config Config object |
||
| 131 | * |
||
| 132 | 6 | * @return false|array false on failure file meta data on success |
|
| 133 | */ |
||
| 134 | 6 | public function write($path, $contents, Config $config) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Update a file. |
||
| 141 | * |
||
| 142 | * @param string $path |
||
| 143 | * @param string $contents |
||
| 144 | * @param Config $config Config object |
||
| 145 | * |
||
| 146 | 4 | * @return false|array false on failure file meta data on success |
|
| 147 | */ |
||
| 148 | 4 | public function update($path, $contents, Config $config) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Rename a file. |
||
| 155 | * |
||
| 156 | * @param string $path |
||
| 157 | * @param string $newpath |
||
| 158 | * |
||
| 159 | 4 | * @return bool |
|
| 160 | */ |
||
| 161 | 4 | public function rename($path, $newpath) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Delete a file. |
||
| 172 | * |
||
| 173 | * @param string $path |
||
| 174 | * |
||
| 175 | 4 | * @return bool |
|
| 176 | */ |
||
| 177 | 4 | public function delete($path) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Delete a directory. |
||
| 196 | * |
||
| 197 | * @param string $dirname |
||
| 198 | * |
||
| 199 | 4 | * @return bool |
|
| 200 | */ |
||
| 201 | public function deleteDir($dirname) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Create a directory. |
||
| 215 | * |
||
| 216 | * @param string $dirname directory name |
||
| 217 | * @param Config $config |
||
| 218 | * |
||
| 219 | 4 | * @return bool|array |
|
| 220 | */ |
||
| 221 | 4 | public function createDir($dirname, Config $config) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Check whether a file exists. |
||
| 228 | * |
||
| 229 | * @param string $path |
||
| 230 | * |
||
| 231 | 12 | * @return bool |
|
| 232 | */ |
||
| 233 | 12 | public function has($path) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Read a file. |
||
| 246 | * |
||
| 247 | * @param string $path |
||
| 248 | * |
||
| 249 | 6 | * @return false|array |
|
| 250 | */ |
||
| 251 | 6 | public function read($path) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * List contents of a directory. |
||
| 264 | * |
||
| 265 | * @param string $directory |
||
| 266 | * @param bool $recursive |
||
| 267 | * |
||
| 268 | 4 | * @return array |
|
| 269 | */ |
||
| 270 | 4 | public function listContents($directory = '', $recursive = false) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param array $options |
||
| 288 | * |
||
| 289 | 4 | * @return array |
|
| 290 | */ |
||
| 291 | 4 | protected function retrievePaginatedListing(array $options) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Get all the meta data of a file or directory. |
||
| 305 | * |
||
| 306 | * @param string $path |
||
| 307 | * |
||
| 308 | 14 | * @return false|array |
|
| 309 | */ |
||
| 310 | 14 | public function getMetadata($path) |
|
| 333 | |||
| 334 | /** |
||
| 335 | 4 | * @return bool |
|
| 336 | */ |
||
| 337 | 4 | private function is404Exception(S3Exception $exception) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Get all the meta data of a file or directory. |
||
| 350 | * |
||
| 351 | * @param string $path |
||
| 352 | * |
||
| 353 | 2 | * @return false|array |
|
| 354 | */ |
||
| 355 | 2 | public function getSize($path) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Get the mimetype of a file. |
||
| 362 | * |
||
| 363 | * @param string $path |
||
| 364 | * |
||
| 365 | 2 | * @return false|array |
|
| 366 | */ |
||
| 367 | 2 | public function getMimetype($path) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Get the timestamp of a file. |
||
| 374 | * |
||
| 375 | * @param string $path |
||
| 376 | * |
||
| 377 | 2 | * @return false|array |
|
| 378 | */ |
||
| 379 | 2 | public function getTimestamp($path) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Write a new file using a stream. |
||
| 386 | * |
||
| 387 | * @param string $path |
||
| 388 | * @param resource $resource |
||
| 389 | * @param Config $config Config object |
||
| 390 | * |
||
| 391 | 4 | * @return array|false false on failure file meta data on success |
|
| 392 | */ |
||
| 393 | 4 | public function writeStream($path, $resource, Config $config) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Update a file using a stream. |
||
| 400 | * |
||
| 401 | * @param string $path |
||
| 402 | * @param resource $resource |
||
| 403 | * @param Config $config Config object |
||
| 404 | * |
||
| 405 | 4 | * @return array|false false on failure file meta data on success |
|
| 406 | */ |
||
| 407 | 4 | public function updateStream($path, $resource, Config $config) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Copy a file. |
||
| 414 | * |
||
| 415 | * @param string $path |
||
| 416 | * @param string $newpath |
||
| 417 | * |
||
| 418 | 8 | * @return bool |
|
| 419 | */ |
||
| 420 | 8 | public function copy($path, $newpath) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Get a CloudFront URL for the file at the given path. |
||
| 444 | * |
||
| 445 | * @param string $path |
||
| 446 | * @param \DateTimeInterface $expiration |
||
| 447 | 6 | * @param array $options |
|
| 448 | * @return string |
||
| 449 | 6 | */ |
|
| 450 | public function getCloudFrontUrl($path, $expiration, array $options = []) |
||
| 471 | 5 | ||
| 472 | /** |
||
| 473 | 10 | * Get a temporary URL for the file at the given path. |
|
| 474 | 2 | * |
|
| 475 | 1 | * @param string $path |
|
| 476 | * @param \DateTimeInterface $expiration |
||
| 477 | 10 | * @param array $options |
|
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | public function getTemporaryUrl($path, $expiration, array $options = []) |
||
| 497 | 6 | ||
| 498 | /** |
||
| 499 | 6 | * Read a file as a stream. |
|
| 500 | 6 | * |
|
| 501 | * @param string $path |
||
| 502 | 6 | * |
|
| 503 | 6 | * @return array|false |
|
| 504 | 6 | */ |
|
| 505 | public function readStream($path) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Read an object and normalize the response. |
||
| 519 | * |
||
| 520 | * @param string $path |
||
| 521 | * |
||
| 522 | * @return array|bool |
||
| 523 | */ |
||
| 524 | 4 | protected function readObject($path) |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Set the visibility for a file. |
||
| 549 | * |
||
| 550 | * @param string $path |
||
| 551 | * @param string $visibility |
||
| 552 | * |
||
| 553 | * @return array|false file meta data |
||
| 554 | 12 | */ |
|
| 555 | public function setVisibility($path, $visibility) |
||
| 574 | 2 | ||
| 575 | /** |
||
| 576 | 6 | * Get the visibility of a file. |
|
| 577 | * |
||
| 578 | 12 | * @param string $path |
|
| 579 | * |
||
| 580 | * @return array|false |
||
| 581 | */ |
||
| 582 | public function getVisibility($path) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * {@inheritdoc} |
||
| 589 | */ |
||
| 590 | 14 | public function applyPathPrefix($path) |
|
| 594 | 14 | ||
| 595 | /** |
||
| 596 | 14 | * {@inheritdoc} |
|
| 597 | 12 | */ |
|
| 598 | 2 | public function setPathPrefix($prefix) |
|
| 604 | |||
| 605 | 12 | /** |
|
| 606 | * Get the object acl presented as a visibility. |
||
| 607 | * |
||
| 608 | 6 | * @param string $path |
|
| 609 | * |
||
| 610 | * @return string |
||
| 611 | 14 | */ |
|
| 612 | 8 | protected function getRawVisibility($path) |
|
| 638 | 14 | ||
| 639 | /** |
||
| 640 | 14 | * Upload an object. |
|
| 641 | * |
||
| 642 | 14 | * @param string $path |
|
| 643 | * @param string|resource $body |
||
| 644 | 10 | * @param Config $config |
|
| 645 | * |
||
| 646 | 10 | * @return array|bool |
|
| 647 | 5 | */ |
|
| 648 | protected function upload($path, $body, Config $config) |
||
| 676 | |||
| 677 | 28 | /** |
|
| 678 | 16 | * Check if the path contains only directories |
|
| 679 | 14 | * |
|
| 680 | 14 | * @param string $path |
|
| 681 | 28 | * |
|
| 682 | * @return bool |
||
| 683 | 28 | */ |
|
| 684 | 16 | private function isOnlyDir($path) |
|
| 688 | 6 | ||
| 689 | 6 | /** |
|
| 690 | * Get options from the config. |
||
| 691 | 6 | * |
|
| 692 | * @param Config $config |
||
| 693 | * |
||
| 694 | 26 | * @return array |
|
| 695 | */ |
||
| 696 | protected function getOptionsFromConfig(Config $config) |
||
| 723 | |||
| 724 | 2 | /** |
|
| 725 | * Normalize the object result array. |
||
| 726 | * |
||
| 727 | * @param array $response |
||
| 728 | * @param string $path |
||
| 729 | * |
||
| 730 | * @return array |
||
| 731 | */ |
||
| 732 | protected function normalizeResponse(array $response, $path = null) |
||
| 754 | |||
| 755 | /** |
||
| 756 | * @param string $location |
||
| 757 | * |
||
| 758 | * @return bool |
||
| 759 | */ |
||
| 760 | protected function doesDirectoryExist($location) |
||
| 785 | } |
||
| 786 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: