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 /** MicroFileHelper */ |
||
19 | class FileHelper |
||
20 | { |
||
21 | /** |
||
22 | * Directory size |
||
23 | * |
||
24 | * @access public |
||
25 | * |
||
26 | * @param string $dirName directory name |
||
27 | * |
||
28 | * @return integer |
||
29 | * @static |
||
30 | */ |
||
31 | public static function dirSize($dirName) |
||
52 | |||
53 | /** |
||
54 | * Recursive remove dir |
||
55 | * |
||
56 | * @access public |
||
57 | * |
||
58 | * @param string $path path to remove |
||
59 | * |
||
60 | * @return void |
||
61 | * @static |
||
62 | */ |
||
63 | public static function removeDir($path) |
||
79 | |||
80 | /** |
||
81 | * Recursive copy files |
||
82 | * |
||
83 | * @access public |
||
84 | * |
||
85 | * @param string $src source path |
||
86 | * @param string $dst destination path |
||
87 | * |
||
88 | * @return void |
||
89 | * @throws Exception |
||
90 | * @static |
||
91 | */ |
||
92 | public static function recurseCopy($src, $dst) |
||
111 | |||
112 | /** |
||
113 | * Recursive copy files if edited |
||
114 | * |
||
115 | * @access public |
||
116 | * |
||
117 | * @param string $src source path |
||
118 | * @param string $dst destination path |
||
119 | * @param array $excludes excludes extensions |
||
120 | * |
||
121 | * @return void |
||
122 | * @throws Exception |
||
123 | * @static |
||
124 | */ |
||
125 | public static function recurseCopyIfEdited($src = '', $dst = '', array $excludes = ['php']) |
||
160 | } |
||
161 |
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.