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 | { |
||
19 | protected $service; |
||
20 | protected $bucket; |
||
21 | protected $options; |
||
22 | protected $bucketExists; |
||
23 | protected $metadata = array(); |
||
24 | protected $detectContentType; |
||
25 | |||
26 | View Code Duplication | public function __construct(S3Client $service, $bucket, array $options = array(), $detectContentType = false) |
|
41 | |||
42 | /** |
||
43 | * Gets the publicly accessible URL of an Amazon S3 object. |
||
44 | * |
||
45 | * @param string $key Object key |
||
46 | * @param array $options Associative array of options used to buld the URL |
||
47 | * - expires: The time at which the URL should expire |
||
48 | * represented as a UNIX timestamp |
||
49 | * - Any options available in the Amazon S3 GetObject |
||
50 | * operation may be specified. |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | public function getUrl($key, array $options = array()) |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | public function setMetadata($key, $metadata) |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | public function getMetadata($key) |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function read($key) |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | public function rename($sourceKey, $targetKey) |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function write($key, $content) |
||
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | public function exists($key) |
||
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | View Code Duplication | public function mtime($key) |
|
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | View Code Duplication | public function size($key) |
|
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | public function keys() |
||
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | public function listKeys($prefix = '') |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | public function delete($key) |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function isDirectory($key) |
||
244 | |||
245 | /** |
||
246 | * Ensures the specified bucket exists. If the bucket does not exists |
||
247 | * and the create option is set to true, it will try to create the |
||
248 | * bucket. The bucket is created using the same region as the supplied |
||
249 | * client object. |
||
250 | * |
||
251 | * @throws \RuntimeException if the bucket does not exists or could not be |
||
252 | * created |
||
253 | */ |
||
254 | protected function ensureBucketExists() |
||
281 | |||
282 | protected function getOptions($key, array $options = array()) |
||
296 | |||
297 | View Code Duplication | protected function computePath($key) |
|
305 | |||
306 | /** |
||
307 | * Computes the key from the specified path. |
||
308 | * |
||
309 | * @param string $path |
||
310 | * |
||
311 | * return string |
||
312 | */ |
||
313 | protected function computeKey($path) |
||
317 | |||
318 | /** |
||
319 | * @param string $content |
||
320 | * |
||
321 | * @return string |
||
322 | */ |
||
323 | View Code Duplication | private function guessContentType($content) |
|
333 | } |
||
334 |