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 |
||
12 | class Directory implements ArrayAccess |
||
13 | { |
||
14 | protected $path; |
||
15 | protected $format; |
||
16 | protected $filesystem; |
||
17 | protected $documents = []; |
||
18 | protected $directories = []; |
||
19 | |||
20 | /** |
||
21 | * Creates a new directory instance. |
||
22 | */ |
||
23 | public static function make(string $path, FormatInterface $format): Directory |
||
27 | |||
28 | public function __construct(FilesystemInterface $filesystem, string $path, FormatInterface $format) |
||
34 | |||
35 | /** |
||
36 | * Read and return a document. |
||
37 | */ |
||
38 | public function getDocument(string $id): Document |
||
57 | |||
58 | /** |
||
59 | * Read and return a directory. |
||
60 | */ |
||
61 | public function getDirectory(string $id): Directory |
||
73 | |||
74 | /** |
||
75 | * Check whether a document exists. |
||
76 | */ |
||
77 | View Code Duplication | public function hasDocument(string $id): bool |
|
93 | |||
94 | /** |
||
95 | * Check whether a document or directory exists. |
||
96 | */ |
||
97 | View Code Duplication | public function hasDirectory(string $id): bool |
|
113 | |||
114 | /** |
||
115 | * Saves a document. |
||
116 | */ |
||
117 | public function saveDocument(string $id, Document $document): self |
||
124 | |||
125 | /** |
||
126 | * Creates a new directory. |
||
127 | */ |
||
128 | public function createDirectory(string $id): Directory |
||
135 | |||
136 | /** |
||
137 | * Deletes a document. |
||
138 | */ |
||
139 | public function deleteDocument(string $id): self |
||
146 | |||
147 | /** |
||
148 | * Deletes a directory. |
||
149 | */ |
||
150 | public function deleteDirectory(string $id): self |
||
157 | |||
158 | /** |
||
159 | * Returns all documents. |
||
160 | */ |
||
161 | View Code Duplication | public function getAllDocuments(): array |
|
175 | |||
176 | /** |
||
177 | * Returns all directories. |
||
178 | */ |
||
179 | View Code Duplication | public function getAllDirectories(): array |
|
193 | |||
194 | /** |
||
195 | * Returns a file path. |
||
196 | */ |
||
197 | private function getDocumentPath(string $id): string |
||
201 | |||
202 | /** |
||
203 | * Returns a directory path. |
||
204 | */ |
||
205 | private function getDirectoryPath(string $id): string |
||
213 | |||
214 | /** |
||
215 | * ArrayAccess used to documents. |
||
216 | * |
||
217 | * @param string $id |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function offsetExists($id) |
||
225 | |||
226 | /** |
||
227 | * ArrayAccess used to documents. |
||
228 | * |
||
229 | * @param string $id |
||
230 | * |
||
231 | * @return Document |
||
232 | */ |
||
233 | public function offsetGet($id) |
||
237 | |||
238 | /** |
||
239 | * ArrayAccess used to documents. |
||
240 | * |
||
241 | * @param string $id |
||
242 | * @param Document $document |
||
243 | */ |
||
244 | public function offsetSet($id, $document) |
||
248 | |||
249 | /** |
||
250 | * ArrayAccess used to documents. |
||
251 | * |
||
252 | * @param string $id |
||
253 | */ |
||
254 | public function offsetUnset($id) |
||
258 | |||
259 | /** |
||
260 | * Property magic method used to directories. |
||
261 | */ |
||
262 | public function __get(string $id): Directory |
||
266 | |||
267 | /** |
||
268 | * Property magic method used to directories. |
||
269 | */ |
||
270 | public function __isset(string $id): bool |
||
274 | |||
275 | /** |
||
276 | * Property magic method used to directories. |
||
277 | */ |
||
278 | public function __unset(string $id) |
||
282 | } |
||
283 |
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.