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 |
||
17 | View Code Duplication | class Gzip implements CompressorInterface, DeCompressorInterface, LoggerAwareInterface, ProcessFactoryAwareInterface |
|
|
|||
18 | { |
||
19 | use OptionalLoggerTrait; |
||
20 | use FileProcessTrait; |
||
21 | use GetOptionTrait; |
||
22 | |||
23 | /** |
||
24 | * Compress a file and return the new file |
||
25 | * |
||
26 | * @param FileNodeInterface $node |
||
27 | * @param array $options |
||
28 | * |
||
29 | * @return FileNodeInterface |
||
30 | */ |
||
31 | 1 | public function compress(FileNodeInterface $node, array $options = []) |
|
38 | |||
39 | /** |
||
40 | * @param LocalFile $file |
||
41 | * @param array $options -keepOldFile <bool> (Default: true) |
||
42 | * |
||
43 | * @return LocalFile |
||
44 | * @throws ProcessFailedException |
||
45 | */ |
||
46 | 9 | public function gzip(LocalFile $file, array $options = []) |
|
69 | |||
70 | /** |
||
71 | * Decompress a file and return the decompressed file |
||
72 | * |
||
73 | * @param FileNodeInterface $node |
||
74 | * @param array $options |
||
75 | * |
||
76 | * @return FileNodeInterface |
||
77 | */ |
||
78 | 1 | public function decompress(FileNodeInterface $node, array $options = []) |
|
85 | |||
86 | /** |
||
87 | * @extend Graze\DataFile\Node\File\LocalFile |
||
88 | * |
||
89 | * @param LocalFile $file |
||
90 | * @param array $options |
||
91 | * |
||
92 | * @return LocalFile |
||
93 | */ |
||
94 | 5 | public function gunzip(LocalFile $file, array $options = []) |
|
118 | } |
||
119 |
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.