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 |
||
23 | class Storage |
||
24 | { |
||
25 | /** |
||
26 | * @var SitemapStorage |
||
27 | */ |
||
28 | protected $sitemap; |
||
29 | /** |
||
30 | * @var ImagesStorage |
||
31 | */ |
||
32 | protected $images; |
||
33 | /** |
||
34 | * @var ImageSetStorage |
||
35 | */ |
||
36 | protected $imageSet; |
||
37 | /** |
||
38 | * @var FilesStorage |
||
39 | */ |
||
40 | protected $files; |
||
41 | /** |
||
42 | * @var UsersStorage |
||
43 | */ |
||
44 | protected $users; |
||
45 | /** |
||
46 | * @var DocumentTypesStorage |
||
47 | */ |
||
48 | protected $documentTypes; |
||
49 | /** |
||
50 | * @var BricksStorage |
||
51 | */ |
||
52 | protected $bricks; |
||
53 | /** |
||
54 | * @var ApplicationComponentsStorage |
||
55 | */ |
||
56 | protected $applicationComponents; |
||
57 | |||
58 | /** |
||
59 | * @var ValuelistsStorage |
||
60 | */ |
||
61 | protected $valuelists; |
||
62 | /** |
||
63 | * @var DocumentStorage |
||
64 | */ |
||
65 | protected $documents; |
||
66 | /** |
||
67 | * @var RedirectsStorage |
||
68 | */ |
||
69 | protected $redirects; |
||
70 | |||
71 | /** |
||
72 | * @var ActivityLogStorage |
||
73 | */ |
||
74 | protected $activityLog; |
||
75 | /** |
||
76 | * @var String |
||
77 | */ |
||
78 | protected $imagesDir; |
||
79 | /** |
||
80 | * @var String |
||
81 | */ |
||
82 | protected $filesDir; |
||
83 | |||
84 | /** |
||
85 | * @var String |
||
86 | */ |
||
87 | private $storageDir; |
||
88 | /** |
||
89 | * @var Repository |
||
90 | */ |
||
91 | private $repository; |
||
92 | |||
93 | /** |
||
94 | * JsonStorage constructor. |
||
95 | * |
||
96 | * @param string $storageDir |
||
97 | * @param $imagesDir |
||
98 | * @param $filesDir |
||
99 | */ |
||
100 | public function __construct($storageDir, $imagesDir, $filesDir) |
||
107 | |||
108 | /** |
||
109 | * Retrieve the data from the storagepath |
||
110 | * so it can be interacted with |
||
111 | * |
||
112 | * @throws \Exception |
||
113 | */ |
||
114 | private function config() |
||
124 | |||
125 | /** |
||
126 | * @return \CloudControl\Cms\storage\storage\UsersStorage |
||
127 | */ |
||
128 | public function getUsers() |
||
135 | |||
136 | /** |
||
137 | * Get documents |
||
138 | * |
||
139 | * @return DocumentStorage |
||
140 | */ |
||
141 | public function getDocuments() |
||
148 | |||
149 | /** |
||
150 | * Add new document in given path |
||
151 | * |
||
152 | * @param array $postValues |
||
153 | * |
||
154 | * @throws \Exception |
||
155 | */ |
||
156 | public function addDocumentFolder($postValues) |
||
167 | |||
168 | /** |
||
169 | * Delete a folder by its compound slug |
||
170 | * |
||
171 | * @param $slug |
||
172 | * |
||
173 | * @throws \Exception |
||
174 | */ |
||
175 | public function deleteDocumentFolderBySlug($slug) |
||
182 | |||
183 | public function publishDocumentBySlug($slug) |
||
188 | |||
189 | public function unpublishDocumentBySlug($slug) |
||
194 | |||
195 | /** |
||
196 | * Retrieve a folder by its compound slug |
||
197 | * |
||
198 | * @param $slug |
||
199 | * |
||
200 | * @return mixed |
||
201 | * @throws \Exception |
||
202 | */ |
||
203 | public function getDocumentFolderBySlug($slug) |
||
209 | |||
210 | /** |
||
211 | * Save changes to folder |
||
212 | * |
||
213 | * @param $postValues |
||
214 | * |
||
215 | * @throws \Exception |
||
216 | */ |
||
217 | public function saveDocumentFolder($postValues) |
||
221 | |||
222 | /** |
||
223 | * @return SitemapStorage |
||
224 | */ |
||
225 | public function getSitemap() |
||
232 | |||
233 | /** |
||
234 | * Get all images |
||
235 | * |
||
236 | * @return ImagesStorage |
||
237 | */ |
||
238 | public function getImages() |
||
246 | |||
247 | /** |
||
248 | * Get all files |
||
249 | * |
||
250 | * @return FilesStorage |
||
251 | */ |
||
252 | public function getFiles() |
||
259 | |||
260 | /** |
||
261 | * @return string |
||
262 | */ |
||
263 | public function getStorageDir() |
||
267 | |||
268 | /** |
||
269 | * @return \PDO |
||
270 | */ |
||
271 | public function getContentDbHandle() |
||
275 | |||
276 | /** |
||
277 | * @return DocumentTypesStorage |
||
278 | */ |
||
279 | public function getDocumentTypes() |
||
286 | |||
287 | /** |
||
288 | * @return BricksStorage |
||
289 | */ |
||
290 | View Code Duplication | public function getBricks() |
|
297 | |||
298 | /** |
||
299 | * Get the image set |
||
300 | * |
||
301 | * @return ImageSetStorage |
||
302 | */ |
||
303 | View Code Duplication | public function getImageSet() |
|
310 | |||
311 | /** |
||
312 | * @return ApplicationComponentsStorage |
||
313 | */ |
||
314 | public function getApplicationComponents() |
||
321 | |||
322 | /** |
||
323 | * @return \CloudControl\Cms\storage\Repository |
||
324 | */ |
||
325 | public function getRepository() |
||
329 | |||
330 | /** |
||
331 | * @return \CloudControl\Cms\storage\storage\ValuelistsStorage |
||
332 | */ |
||
333 | public function getValuelists() |
||
340 | |||
341 | /** |
||
342 | * @return \CloudControl\Cms\storage\storage\RedirectsStorage |
||
343 | */ |
||
344 | public function getRedirects() |
||
351 | |||
352 | /** |
||
353 | * @return ActivityLogStorage |
||
354 | */ |
||
355 | public function getActivityLog() |
||
362 | |||
363 | |||
364 | } |
||
365 | } |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.