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 |
||
5 | View Code Duplication | class Gzip extends AbstractCompressor |
|
|
|||
6 | { |
||
7 | /** |
||
8 | * Get the extension used by this compressor |
||
9 | * |
||
10 | * @return string |
||
11 | */ |
||
12 | 10 | protected function getExtension() |
|
16 | |||
17 | /** |
||
18 | * @return string |
||
19 | */ |
||
20 | 11 | protected function getCompression() |
|
24 | |||
25 | /** |
||
26 | * Get the command line to compress a file |
||
27 | * |
||
28 | * @param string $fromPath |
||
29 | * @param string $toPath |
||
30 | * |
||
31 | * @return string |
||
32 | */ |
||
33 | 10 | protected function getCompressCommand($fromPath, $toPath) |
|
37 | |||
38 | /** |
||
39 | * Get the command line to decompress a file |
||
40 | * |
||
41 | * @param string $fromPath |
||
42 | * @param string $toPath |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | 6 | protected function getDecompressCommand($fromPath, $toPath) |
|
50 | } |
||
51 |
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.