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 |
||
32 | class ZIP extends Archive{ |
||
33 | /** |
||
34 | * @var \ZipArchive zip |
||
35 | */ |
||
36 | private $zip=null; |
||
37 | private $path; |
||
38 | |||
39 | /** |
||
40 | * @param string $source |
||
41 | */ |
||
42 | function __construct($source) { |
||
50 | /** |
||
51 | * add an empty folder to the archive |
||
52 | * @param string $path |
||
53 | * @return bool |
||
54 | */ |
||
55 | function addFolder($path) { |
||
58 | /** |
||
59 | * add a file to the archive |
||
60 | * @param string $path |
||
61 | * @param string $source either a local file or string data |
||
62 | * @return bool |
||
63 | */ |
||
64 | function addFile($path, $source='') { |
||
76 | /** |
||
77 | * rename a file or folder in the archive |
||
78 | * @param string $source |
||
79 | * @param string $dest |
||
80 | * @return boolean|null |
||
81 | */ |
||
82 | function rename($source, $dest) { |
||
87 | /** |
||
88 | * get the uncompressed size of a file in the archive |
||
89 | * @param string $path |
||
90 | * @return int |
||
91 | */ |
||
92 | function filesize($path) { |
||
96 | /** |
||
97 | * get the last modified time of a file in the archive |
||
98 | * @param string $path |
||
99 | * @return int |
||
100 | */ |
||
101 | function mtime($path) { |
||
104 | /** |
||
105 | * get the files in a folder |
||
106 | * @param string $path |
||
107 | * @return array |
||
108 | */ |
||
109 | function getFolder($path) { |
||
122 | /** |
||
123 | * get all files in the archive |
||
124 | * @return array |
||
125 | */ |
||
126 | function getFiles() { |
||
134 | /** |
||
135 | * get the content of a file |
||
136 | * @param string $path |
||
137 | * @return string |
||
138 | */ |
||
139 | function getFile($path) { |
||
142 | /** |
||
143 | * extract a single file from the archive |
||
144 | * @param string $path |
||
145 | * @param string $dest |
||
146 | * @return boolean|null |
||
147 | */ |
||
148 | function extractFile($path, $dest) { |
||
152 | /** |
||
153 | * extract the archive |
||
154 | * @param string $dest |
||
155 | * @return bool |
||
156 | */ |
||
157 | function extract($dest) { |
||
160 | /** |
||
161 | * check if a file or folder exists in the archive |
||
162 | * @param string $path |
||
163 | * @return bool |
||
164 | */ |
||
165 | function fileExists($path) { |
||
168 | /** |
||
169 | * remove a file or folder from the archive |
||
170 | * @param string $path |
||
171 | * @return bool |
||
172 | */ |
||
173 | function remove($path) { |
||
180 | /** |
||
181 | * get a file handler |
||
182 | * @param string $path |
||
183 | * @param string $mode |
||
184 | * @return resource |
||
185 | */ |
||
186 | function getStream($path, $mode) { |
||
207 | |||
208 | private static $tempFiles=array(); |
||
209 | /** |
||
210 | * write back temporary files |
||
211 | */ |
||
212 | function writeBack($tmpFile) { |
||
218 | |||
219 | /** |
||
220 | * @param string $path |
||
221 | * @return string |
||
222 | */ |
||
223 | private function stripPath($path) { |
||
230 | } |
||
231 |
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.