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 |
||
15 | class AwsS3 implements Adapter, |
||
|
|||
16 | MetadataSupporter, |
||
17 | ListKeysAware, |
||
18 | ListFilesAware, |
||
19 | SizeCalculator |
||
20 | { |
||
21 | protected $service; |
||
22 | protected $bucket; |
||
23 | protected $options; |
||
24 | protected $bucketExists; |
||
25 | protected $metadata = array(); |
||
26 | protected $detectContentType; |
||
27 | |||
28 | View Code Duplication | public function __construct(S3Client $service, $bucket, array $options = array(), $detectContentType = false) |
|
43 | |||
44 | /** |
||
45 | * Gets the publicly accessible URL of an Amazon S3 object. |
||
46 | * |
||
47 | * @param string $key Object key |
||
48 | * @param array $options Associative array of options used to buld the URL |
||
49 | * - expires: The time at which the URL should expire |
||
50 | * represented as a UNIX timestamp |
||
51 | * - Any options available in the Amazon S3 GetObject |
||
52 | * operation may be specified. |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | public function getUrl($key, array $options = array()) |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | public function setMetadata($key, $metadata) |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function getMetadata($key) |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | public function read($key) |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | public function rename($sourceKey, $targetKey) |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function write($key, $content) |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | public function exists($key) |
||
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | View Code Duplication | public function mtime($key) |
|
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | View Code Duplication | public function size($key) |
|
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | public function keys() |
||
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | public function listKeys($prefix = '') |
||
223 | |||
224 | /** |
||
225 | * List files beginning with given prefix. |
||
226 | * Similar to listKeys but this will also return file mtime, size, and etag. |
||
227 | * @param string $prefix |
||
228 | * @return mixed |
||
229 | */ |
||
230 | public function listFiles($prefix = '') |
||
258 | |||
259 | /** |
||
260 | * {@inheritdoc} |
||
261 | */ |
||
262 | public function delete($key) |
||
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | public function isDirectory($key) |
||
286 | |||
287 | /** |
||
288 | * Ensures the specified bucket exists. If the bucket does not exists |
||
289 | * and the create option is set to true, it will try to create the |
||
290 | * bucket. The bucket is created using the same region as the supplied |
||
291 | * client object. |
||
292 | * |
||
293 | * @throws \RuntimeException if the bucket does not exists or could not be |
||
294 | * created |
||
295 | */ |
||
296 | protected function ensureBucketExists() |
||
323 | |||
324 | protected function getOptions($key, array $options = array()) |
||
338 | |||
339 | View Code Duplication | protected function computePath($key) |
|
347 | |||
348 | /** |
||
349 | * Computes the key from the specified path. |
||
350 | * |
||
351 | * @param string $path |
||
352 | * |
||
353 | * return string |
||
354 | */ |
||
355 | protected function computeKey($path) |
||
359 | } |
||
360 |