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 Directory implements ArrayAccess |
||
11 | { |
||
12 | protected $path; |
||
13 | protected $format; |
||
14 | protected $filesystem; |
||
15 | protected $documents = []; |
||
16 | protected $directories = []; |
||
17 | |||
18 | /** |
||
19 | * Creates a new directory instance. |
||
20 | * |
||
21 | * @param string $path |
||
22 | * @param FormatInterface $format |
||
23 | * |
||
24 | * @return static |
||
25 | */ |
||
26 | public static function make($path, FormatInterface $format) |
||
30 | |||
31 | /** |
||
32 | * Init the directory. |
||
33 | * |
||
34 | * @param FilesystemInterface $filesystem |
||
35 | * @param string $path |
||
36 | * @param FormatInterface $format |
||
37 | */ |
||
38 | public function __construct(FilesystemInterface $filesystem, $path, FormatInterface $format) |
||
44 | |||
45 | /** |
||
46 | * Read and return a document. |
||
47 | * |
||
48 | * @param string $id |
||
49 | * |
||
50 | * @return Document |
||
51 | */ |
||
52 | public function getDocument($id) |
||
71 | |||
72 | /** |
||
73 | * Read and return a directory. |
||
74 | * |
||
75 | * @param string $id |
||
76 | * |
||
77 | * @return static |
||
78 | */ |
||
79 | public function getDirectory($id) |
||
91 | |||
92 | /** |
||
93 | * Check whether a document exists. |
||
94 | * |
||
95 | * @param string $id |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | View Code Duplication | public function hasDocument($id) |
|
115 | |||
116 | /** |
||
117 | * Check whether a document or directory exists. |
||
118 | * |
||
119 | * @param string $id |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | View Code Duplication | public function hasDirectory($id) |
|
139 | |||
140 | /** |
||
141 | * Saves a document. |
||
142 | * |
||
143 | * @param string $id |
||
144 | * @param Document $document |
||
145 | * |
||
146 | * @return self |
||
147 | */ |
||
148 | public function saveDocument($id, Document $document) |
||
155 | |||
156 | /** |
||
157 | * Creates a new directory. |
||
158 | * |
||
159 | * @param string $id |
||
160 | * |
||
161 | * @return static |
||
162 | */ |
||
163 | public function createDirectory($id) |
||
170 | |||
171 | /** |
||
172 | * Deletes a document. |
||
173 | * |
||
174 | * @param string $id |
||
175 | * |
||
176 | * @return self |
||
177 | */ |
||
178 | public function deleteDocument($id) |
||
185 | |||
186 | /** |
||
187 | * Deletes a directory. |
||
188 | * |
||
189 | * @param string $id |
||
190 | * |
||
191 | * @return self |
||
192 | */ |
||
193 | public function deleteDirectory($id) |
||
200 | |||
201 | /** |
||
202 | * Returns all documents. |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | View Code Duplication | public function getAllDocuments() |
|
220 | |||
221 | /** |
||
222 | * Returns all directories. |
||
223 | * |
||
224 | * @return array |
||
225 | */ |
||
226 | View Code Duplication | public function getAllDirectories() |
|
240 | |||
241 | /** |
||
242 | * Returns a file path. |
||
243 | * |
||
244 | * @param string $id |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | private function getDocumentPath($id) |
||
252 | |||
253 | /** |
||
254 | * Returns a directory path. |
||
255 | * |
||
256 | * @param string $id |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | private function getDirectoryPath($id) |
||
268 | |||
269 | /** |
||
270 | * ArrayAccess used to documents. |
||
271 | * |
||
272 | * @param string $id |
||
273 | * |
||
274 | * @return bool |
||
275 | */ |
||
276 | public function offsetExists($id) |
||
280 | |||
281 | /** |
||
282 | * ArrayAccess used to documents. |
||
283 | * |
||
284 | * @param string $id |
||
285 | * |
||
286 | * @return Document |
||
287 | */ |
||
288 | public function offsetGet($id) |
||
292 | |||
293 | /** |
||
294 | * ArrayAccess used to documents. |
||
295 | * |
||
296 | * @param string $id |
||
297 | * @param Document $document |
||
298 | */ |
||
299 | public function offsetSet($id, $document) |
||
303 | |||
304 | /** |
||
305 | * ArrayAccess used to documents. |
||
306 | * |
||
307 | * @param string $id |
||
308 | */ |
||
309 | public function offsetUnset($id) |
||
313 | |||
314 | /** |
||
315 | * Property magic method used to directories. |
||
316 | * |
||
317 | * @param string $id |
||
318 | * |
||
319 | * @return Directory |
||
320 | */ |
||
321 | public function __get($id) |
||
325 | |||
326 | /** |
||
327 | * Property magic method used to directories. |
||
328 | * |
||
329 | * @param string $id |
||
330 | */ |
||
331 | public function __isset($id) |
||
335 | |||
336 | /** |
||
337 | * Property magic method used to directories. |
||
338 | * |
||
339 | * @param string $id |
||
340 | */ |
||
341 | public function __unset($id) |
||
345 | } |
||
346 |
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.