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:
1 | <?php |
||
15 | View Code Duplication | class S3 implements Adapter, Adapter\MetadataSupporter, Adapter\ListKeysAware, Adapter\SizeCalculator |
|
|
|||
16 | { |
||
17 | protected $service; |
||
18 | protected $bucket; |
||
19 | protected $options; |
||
20 | protected $bucketExists; |
||
21 | protected $metadata = array(); |
||
22 | protected $detectContentType; |
||
23 | |||
24 | public function __construct(S3Client $service, $bucket, array $options = array(), $detectContentType = false) |
||
39 | |||
40 | /** |
||
41 | * Gets the publicly accessible URL of an Amazon S3 object. |
||
42 | * |
||
43 | * @param string $key Object key |
||
44 | * @param array $options Associative array of options used to buld the URL |
||
45 | * - expires: The time at which the URL should expire |
||
46 | * represented as a UNIX timestamp |
||
47 | * - Any options available in the Amazon S3 GetObject |
||
48 | * operation may be specified. |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | public function getUrl($key, array $options = array()) |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function setMetadata($key, $metadata) |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function getMetadata($key) |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function read($key) |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function rename($sourceKey, $targetKey) |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function write($key, $content) |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function exists($key) |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function mtime($key) |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function size($key) |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | public function keys() |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | public function listKeys($prefix = '') |
||
219 | |||
220 | /** |
||
221 | * {@inheritdoc} |
||
222 | */ |
||
223 | public function delete($key) |
||
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | public function isDirectory($key) |
||
247 | |||
248 | /** |
||
249 | * Ensures the specified bucket exists. If the bucket does not exists |
||
250 | * and the create option is set to true, it will try to create the |
||
251 | * bucket. The bucket is created using the same region as the supplied |
||
252 | * client object. |
||
253 | * |
||
254 | * @throws \RuntimeException if the bucket does not exists or could not be |
||
255 | * created |
||
256 | */ |
||
257 | protected function ensureBucketExists() |
||
284 | |||
285 | protected function getOptions($key, array $options = array()) |
||
299 | |||
300 | protected function computePath($key) |
||
308 | |||
309 | /** |
||
310 | * Computes the key from the specified path. |
||
311 | * |
||
312 | * @param string $path |
||
313 | * |
||
314 | * return string |
||
315 | */ |
||
316 | protected function computeKey($path) |
||
320 | } |
||
321 |
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.