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 AsyncAwsS3 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 AsyncAwsS3, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class AsyncAwsS3 implements Adapter, MetadataSupporter, ListKeysAware, SizeCalculator, MimeTypeProvider |
||
16 | { |
||
17 | /** @var SimpleS3Client */ |
||
18 | protected $service; |
||
19 | /** @var string */ |
||
20 | protected $bucket; |
||
21 | /** @var array */ |
||
22 | protected $options; |
||
23 | /** @var bool */ |
||
24 | protected $bucketExists; |
||
25 | /** @var array */ |
||
26 | protected $metadata = []; |
||
27 | /** @var bool */ |
||
28 | protected $detectContentType; |
||
29 | |||
30 | /** |
||
31 | * @param SimpleS3Client $service |
||
32 | * @param string $bucket |
||
33 | * @param array $options |
||
34 | * @param bool $detectContentType |
||
35 | */ |
||
36 | View Code Duplication | public function __construct(SimpleS3Client $service, $bucket, array $options = [], $detectContentType = false) |
|
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | View Code Duplication | public function setMetadata($key, $content) |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | public function getMetadata($key) |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | View Code Duplication | public function read($key) |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | View Code Duplication | public function rename($sourceKey, $targetKey) |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | * @param string|resource $content |
||
124 | */ |
||
125 | 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 | public function size($key) |
||
183 | |||
184 | public function mimeType($key) |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | public function keys() |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | View Code Duplication | public function listKeys($prefix = '') |
|
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | public function delete($key) |
||
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | public function isDirectory($key) |
||
253 | |||
254 | /** |
||
255 | * Ensures the specified bucket exists. If the bucket does not exists |
||
256 | * and the create option is set to true, it will try to create the |
||
257 | * bucket. The bucket is created using the same region as the supplied |
||
258 | * client object. |
||
259 | * |
||
260 | * @throws \RuntimeException if the bucket does not exists or could not be |
||
261 | * created |
||
262 | */ |
||
263 | View Code Duplication | protected function ensureBucketExists() |
|
287 | |||
288 | View Code Duplication | protected function getOptions($key, array $options = []) |
|
302 | |||
303 | protected function computePath($key) |
||
311 | |||
312 | /** |
||
313 | * Computes the key from the specified path. |
||
314 | * |
||
315 | * @param string $path |
||
316 | * |
||
317 | * return string |
||
318 | */ |
||
319 | protected function computeKey($path) |
||
323 | |||
324 | /** |
||
325 | * @param string|resource $content |
||
326 | * |
||
327 | * @return string |
||
328 | */ |
||
329 | View Code Duplication | private function guessContentType($content) |
|
339 | } |
||
340 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.