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 namespace Comodojo\Zip; |
||
26 | class ZipManager implements Countable { |
||
27 | |||
28 | /** |
||
29 | * Array of managed zip files |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | private $zip_archives = []; |
||
34 | |||
35 | /** |
||
36 | * Count the number of Zip objects registered to the manager |
||
37 | * |
||
38 | * @return int |
||
39 | */ |
||
40 | public function count(): int { |
||
43 | |||
44 | /** |
||
45 | * Add a Zip object to manager and return its id |
||
46 | * |
||
47 | * @param Zip $zip |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | public function addZip(Zip $zip): string { |
||
58 | |||
59 | /** |
||
60 | * Remove a Zip object from manager |
||
61 | * |
||
62 | * @param Zip $zip |
||
63 | * |
||
64 | * @return bool |
||
65 | * @throws ZipException |
||
66 | */ |
||
67 | public function removeZip(Zip $zip): bool { |
||
78 | |||
79 | /** |
||
80 | * Remove a Zip object from manager by Zip id |
||
81 | * |
||
82 | * @param string $id |
||
83 | * |
||
84 | * @return bool |
||
85 | * @throws ZipException |
||
86 | */ |
||
87 | public function removeZipById(string $id): bool { |
||
96 | |||
97 | /** |
||
98 | * Get a list of all registered Zips filenames as an array |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | View Code Duplication | public function listZips(): array { |
|
111 | |||
112 | /** |
||
113 | * Get a Zip object by Id |
||
114 | * |
||
115 | * @param string $id The zip id |
||
116 | * |
||
117 | * @return Zip |
||
118 | * @throws ZipException |
||
119 | */ |
||
120 | public function getZip(string $id): Zip { |
||
128 | |||
129 | /** |
||
130 | * Set current base path (to add relative files to zip archive) |
||
131 | * for all Zips |
||
132 | * |
||
133 | * @param string $path |
||
134 | * |
||
135 | * @return ZipManager |
||
136 | * @throws ZipException |
||
137 | */ |
||
138 | public function setPath(string $path): ZipManager { |
||
150 | |||
151 | /** |
||
152 | * Get a list of paths used by Zips |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | View Code Duplication | public function getPath(): array { |
|
165 | |||
166 | /** |
||
167 | * Set default file mask for all Zips |
||
168 | * |
||
169 | * @param int $mask |
||
170 | * |
||
171 | * @return ZipManager |
||
172 | * @throws ZipException |
||
173 | */ |
||
174 | public function setMask(int $mask): ZipManager { |
||
186 | |||
187 | /** |
||
188 | * Get a list of masks from Zips |
||
189 | * |
||
190 | * @return array |
||
191 | */ |
||
192 | View Code Duplication | public function getMask(): array { |
|
201 | |||
202 | /** |
||
203 | * Get a list of files in Zips |
||
204 | * |
||
205 | * @return array |
||
206 | * @throws ZipException |
||
207 | */ |
||
208 | View Code Duplication | public function listFiles(): array { |
|
221 | |||
222 | /** |
||
223 | * Extract Zips to common destination |
||
224 | * |
||
225 | * @param string $destination Destination path |
||
226 | * @param bool $separate (optional) If true (default), files will be placed in different directories |
||
227 | * @param mixed $files (optional) a filename or an array of filenames |
||
228 | * |
||
229 | * @return bool |
||
230 | * @throws ZipException |
||
231 | */ |
||
232 | public function extract( |
||
254 | |||
255 | /** |
||
256 | * Merge multiple Zips into one |
||
257 | * |
||
258 | * @param string $output_zip_file |
||
259 | * Destination zip |
||
260 | * @param bool $separate (optional) |
||
261 | * If true (default), files will be placed in different directories |
||
262 | * |
||
263 | * @return bool |
||
264 | * @throws ZipException |
||
265 | */ |
||
266 | public function merge(string $output_zip_file, bool $separate = true): bool { |
||
286 | |||
287 | /** |
||
288 | * Add a file to all registered Zips |
||
289 | * |
||
290 | * @param mixed $file_name_or_array |
||
291 | * The filename to add or an array of filenames |
||
292 | * @param bool $flatten_root_folder |
||
293 | * (optional) If true, the Zip root folder will be flattened (default: false) |
||
294 | * |
||
295 | * @return ZipManager |
||
296 | * @throws ZipException |
||
297 | */ |
||
298 | public function add($file_name_or_array, bool $flatten_root_folder = false): ZipManager { |
||
312 | |||
313 | /** |
||
314 | * Delete a file from any registered Zip |
||
315 | * |
||
316 | * @param mixed $file_name_or_array |
||
317 | * The filename to add or an array of filenames |
||
318 | * |
||
319 | * @return ZipManager |
||
320 | * @throws ZipException |
||
321 | */ |
||
322 | public function delete($file_name_or_array): ZipManager { |
||
336 | |||
337 | /** |
||
338 | * Close all Zips |
||
339 | * |
||
340 | * @return bool |
||
341 | * @throws ZipException |
||
342 | */ |
||
343 | public function close(): bool { |
||
355 | |||
356 | } |
||
357 |
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.