Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AwsS3 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 AwsS3, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class AwsS3 implements Adapter, |
||
|
|
|||
| 15 | MetadataSupporter, |
||
| 16 | ListKeysAware, |
||
| 17 | SizeCalculator, |
||
| 18 | MimeTypeProvider |
||
| 19 | { |
||
| 20 | /** @var S3Client */ |
||
| 21 | protected $service; |
||
| 22 | /** @var string */ |
||
| 23 | protected $bucket; |
||
| 24 | /** @var array */ |
||
| 25 | protected $options; |
||
| 26 | /** @var bool */ |
||
| 27 | protected $bucketExists; |
||
| 28 | /** @var array */ |
||
| 29 | protected $metadata = []; |
||
| 30 | /** @var bool */ |
||
| 31 | protected $detectContentType; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param S3Client $service |
||
| 35 | * @param string $bucket |
||
| 36 | * @param array $options |
||
| 37 | * @param bool $detectContentType |
||
| 38 | */ |
||
| 39 | public function __construct(S3Client $service, $bucket, array $options = [], $detectContentType = false) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Gets the publicly accessible URL of an Amazon S3 object. |
||
| 57 | * |
||
| 58 | * @param string $key Object key |
||
| 59 | * @param array $options Associative array of options used to buld the URL |
||
| 60 | * - expires: The time at which the URL should expire |
||
| 61 | * represented as a UNIX timestamp |
||
| 62 | * - Any options available in the Amazon S3 GetObject |
||
| 63 | * operation may be specified. |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function getUrl($key, array $options = []) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * {@inheritdoc} |
||
| 79 | */ |
||
| 80 | public function setMetadata($key, $metadata) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | public function getMetadata($key) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | public function read($key) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | public function rename($sourceKey, $targetKey) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | public function write($key, $content) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | public function exists($key) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function mtime($key) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | View Code Duplication | public function size($key) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * {@inheritdoc} |
||
| 210 | */ |
||
| 211 | public function keys() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritdoc} |
||
| 218 | */ |
||
| 219 | public function listKeys($prefix = '') |
||
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | */ |
||
| 240 | public function delete($key) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * {@inheritdoc} |
||
| 253 | */ |
||
| 254 | public function isDirectory($key) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Ensures the specified bucket exists. If the bucket does not exists |
||
| 267 | * and the create option is set to true, it will try to create the |
||
| 268 | * bucket. The bucket is created using the same region as the supplied |
||
| 269 | * client object. |
||
| 270 | * |
||
| 271 | * @throws \RuntimeException if the bucket does not exists or could not be |
||
| 272 | * created |
||
| 273 | */ |
||
| 274 | protected function ensureBucketExists() |
||
| 299 | |||
| 300 | protected function getOptions($key, array $options = []) |
||
| 314 | |||
| 315 | View Code Duplication | protected function computePath($key) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Computes the key from the specified path. |
||
| 326 | * |
||
| 327 | * @param string $path |
||
| 328 | * |
||
| 329 | * return string |
||
| 330 | */ |
||
| 331 | protected function computeKey($path) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $content |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | View Code Duplication | private function guessContentType($content) |
|
| 351 | |||
| 352 | View Code Duplication | public function mimeType($key) |
|
| 361 | } |
||
| 362 |