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 |
||
27 | class AmazonServer extends StorageServer |
||
28 | { |
||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $options = [ |
||
33 | 'server' => 'https://s3.amazonaws.com', |
||
34 | 'timeout' => 0, |
||
35 | 'accessKey' => '', |
||
36 | 'secretKey' => '' |
||
37 | ]; |
||
38 | |||
39 | /** |
||
40 | * @todo DI in constructor |
||
41 | * @var ClientInterface |
||
42 | */ |
||
43 | protected $client = null; |
||
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | public function __construct(FilesInterface $files, array $options) |
||
55 | |||
56 | /** |
||
57 | * @param ClientInterface $client |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function setClient(ClientInterface $client) |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | * |
||
70 | * @return bool|ResponseInterface |
||
71 | */ |
||
72 | public function exists(BucketInterface $bucket, $name) |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | View Code Duplication | public function size(BucketInterface $bucket, $name) |
|
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function put(BucketInterface $bucket, $name, $source) |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function allocateStream(BucketInterface $bucket, $name) |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function delete(BucketInterface $bucket, $name) |
||
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | View Code Duplication | public function rename(BucketInterface $bucket, $oldname, $newname) |
|
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | View Code Duplication | public function copy(BucketInterface $bucket, BucketInterface $destination, $name) |
|
206 | |||
207 | /** |
||
208 | * Create instance of UriInterface based on provided bucket options and storage object name. |
||
209 | * |
||
210 | * @param BucketInterface $bucket |
||
211 | * @param string $name |
||
212 | * @return UriInterface |
||
213 | */ |
||
214 | protected function buildUri(BucketInterface $bucket, $name) |
||
220 | |||
221 | /** |
||
222 | * Helper to create configured PSR7 request with set of amazon commands. |
||
223 | * |
||
224 | * @param string $method |
||
225 | * @param BucketInterface $bucket |
||
226 | * @param string $name |
||
227 | * @param array $headers |
||
228 | * @param array $commands Amazon commands associated with values. |
||
229 | * @return RequestInterface |
||
230 | */ |
||
231 | protected function buildRequest( |
||
251 | |||
252 | /** |
||
253 | * Generate request headers based on provided set of amazon commands. |
||
254 | * |
||
255 | * @param array $commands |
||
256 | * @return array |
||
257 | */ |
||
258 | private function packCommands(array $commands) |
||
267 | |||
268 | /** |
||
269 | * Sign amazon request. |
||
270 | * |
||
271 | * @param RequestInterface $request |
||
272 | * @param array $packedCommands Headers generated based on request commands, see |
||
273 | * packCommands() method for more information. |
||
274 | * @return RequestInterface |
||
275 | */ |
||
276 | private function signRequest(RequestInterface $request, array $packedCommands = []) |
||
306 | |||
307 | /** |
||
308 | * Generate object headers. |
||
309 | * |
||
310 | * @param BucketInterface $bucket |
||
311 | * @param string $name |
||
312 | * @param mixed $source |
||
313 | * @return array |
||
314 | */ |
||
315 | private function createHeaders(BucketInterface $bucket, $name, $source) |
||
337 | } |
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.