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 |
||
17 | class AwsS3 implements Adapter, |
||
|
|||
18 | MetadataSupporter, |
||
19 | ListKeysAware, |
||
20 | SizeCalculator, |
||
21 | MimeTypeProvider |
||
22 | { |
||
23 | /** @var S3Client */ |
||
24 | protected $service; |
||
25 | /** @var string */ |
||
26 | protected $bucket; |
||
27 | /** @var array */ |
||
28 | protected $options; |
||
29 | /** @var bool */ |
||
30 | protected $bucketExists; |
||
31 | /** @var array */ |
||
32 | protected $metadata = []; |
||
33 | /** @var bool */ |
||
34 | protected $detectContentType; |
||
35 | |||
36 | /** |
||
37 | * @param S3Client $service |
||
38 | * @param string $bucket |
||
39 | * @param array $options |
||
40 | * @param bool $detectContentType |
||
41 | */ |
||
42 | public function __construct(S3Client $service, $bucket, array $options = [], $detectContentType = false) |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function setMetadata($key, $metadata) |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function getMetadata($key) |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function read($key) |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function rename($sourceKey, $targetKey) |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | public function write($key, $content) |
||
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | public function exists($key) |
||
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | View Code Duplication | public function mtime($key) |
|
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | View Code Duplication | public function size($key) |
|
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | public function keys() |
||
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | */ |
||
233 | public function listKeys($prefix = '') |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | View Code Duplication | public function delete($key) |
|
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | public function isDirectory($key) |
||
292 | |||
293 | /** |
||
294 | * Ensures the specified bucket exists. If the bucket does not exists |
||
295 | * and the create option is set to true, it will try to create the |
||
296 | * bucket. The bucket is created using the same region as the supplied |
||
297 | * client object. |
||
298 | * |
||
299 | * @throws \RuntimeException if the bucket does not exists or could not be |
||
300 | * created |
||
301 | */ |
||
302 | protected function ensureBucketExists() |
||
327 | |||
328 | protected function getOptions($key, array $options = []) |
||
342 | |||
343 | protected function computePath($key) |
||
351 | |||
352 | /** |
||
353 | * Computes the key from the specified path. |
||
354 | * |
||
355 | * @param string $path |
||
356 | * |
||
357 | * return string |
||
358 | */ |
||
359 | protected function computeKey($path) |
||
363 | |||
364 | /** |
||
365 | * @param string $content |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | View Code Duplication | private function guessContentType($content) |
|
379 | |||
380 | View Code Duplication | public function mimeType($key) |
|
393 | } |
||
394 |