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 |
||
10 | class CopyFilter extends Filter |
||
11 | { |
||
12 | /** |
||
13 | * Copy the sources files to the destinations. |
||
14 | * |
||
15 | * @param SplFileInfo[] $sources |
||
16 | * @param string[] $destinations |
||
17 | * |
||
18 | * @return SplFileInfo[] |
||
19 | * |
||
20 | * @throws Exception |
||
21 | */ |
||
22 | public function run(array $sources, array $destinations) |
||
63 | |||
64 | /** |
||
65 | * No specific requirements for the copy filter. |
||
66 | */ |
||
67 | public function checkRequirements() |
||
70 | |||
71 | /** |
||
72 | * Return the file extensions supported by this filter. All the files can be process by the copy filter. |
||
73 | * |
||
74 | * @return string[] |
||
75 | */ |
||
76 | public function getSupportedExtensions() |
||
82 | |||
83 | /** |
||
84 | * Remove the last slash of the string. |
||
85 | * |
||
86 | * @param string $string |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | View Code Duplication | private function removeLastSlash($string) |
|
98 | } |
||
99 |
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.