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 |
||
30 | class ProjectController extends Controller |
||
31 | { |
||
32 | /** |
||
33 | * Display activity for a project. |
||
34 | * |
||
35 | * @param Project $project |
||
36 | * |
||
37 | * @return \Illuminate\View\View |
||
38 | */ |
||
39 | 11 | public function getIndex(Project $project) |
|
60 | |||
61 | /** |
||
62 | * Display issues for a project. |
||
63 | * |
||
64 | * @param FilterForm $filterForm |
||
65 | * @param Request $request |
||
66 | * @param Project $project |
||
67 | * @param int $status |
||
68 | * |
||
69 | * @return \Illuminate\View\View |
||
70 | */ |
||
71 | 2 | public function getIssues(FilterForm $filterForm, Request $request, Project $project, $status = Issue::STATUS_OPEN) |
|
86 | |||
87 | /** |
||
88 | * Display issues assigned to current user for a project. |
||
89 | * |
||
90 | * @param Project $project |
||
91 | * |
||
92 | * @return \Illuminate\View\View |
||
93 | */ |
||
94 | 1 | View Code Duplication | public function getAssigned(Project $project) |
106 | |||
107 | /** |
||
108 | * Display issues created to current user for a project. |
||
109 | * |
||
110 | * @param Project $project |
||
111 | * |
||
112 | * @return \Illuminate\View\View |
||
113 | */ |
||
114 | View Code Duplication | public function getCreated(Project $project) |
|
126 | |||
127 | /** |
||
128 | * Display notes for a project. |
||
129 | * |
||
130 | * @param Project $project |
||
131 | * @param NoteForm $form |
||
132 | * |
||
133 | * @return \Illuminate\View\View |
||
134 | */ |
||
135 | 7 | public function getNotes(Project $project, NoteForm $form) |
|
148 | |||
149 | /** |
||
150 | * @param Project $project |
||
151 | * @param string $view |
||
152 | * @param null $data |
||
153 | * @param bool $status |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | 21 | protected function projectMainViewTabs(Project $project, $view, $data = null, $status = false) |
|
212 | |||
213 | /** |
||
214 | * Edit the project. |
||
215 | * |
||
216 | * @param Project $project |
||
217 | * @param Form $form |
||
218 | * |
||
219 | * @return \Illuminate\View\View |
||
220 | */ |
||
221 | 2 | public function getEdit(Project $project, Form $form) |
|
229 | |||
230 | /** |
||
231 | * To update project details. |
||
232 | * |
||
233 | * @param Project $project |
||
234 | * @param FormRequest\Project $request |
||
235 | * |
||
236 | * @return \Illuminate\Http\RedirectResponse |
||
237 | */ |
||
238 | 2 | public function postEdit(Project $project, FormRequest\Project $request) |
|
253 | |||
254 | /** |
||
255 | * Ajax: returns list of users that are not in the project. |
||
256 | * |
||
257 | * @param Project $project |
||
258 | * |
||
259 | * @return \Symfony\Component\HttpFoundation\Response |
||
260 | */ |
||
261 | 1 | public function getInactiveUsers(Project $project = null) |
|
267 | |||
268 | /** |
||
269 | * Ajax: add user to the project. |
||
270 | * |
||
271 | * @param Project $project |
||
272 | * @param Request $request |
||
273 | * |
||
274 | * @return \Symfony\Component\HttpFoundation\Response |
||
275 | */ |
||
276 | 1 | View Code Duplication | public function postAssign(Project $project, Request $request) |
286 | |||
287 | /** |
||
288 | * Ajax: remove user from the project. |
||
289 | * |
||
290 | * @param Project $project |
||
291 | * @param Request $request |
||
292 | * |
||
293 | * @return \Symfony\Component\HttpFoundation\Response |
||
294 | */ |
||
295 | 1 | View Code Duplication | public function postUnassign(Project $project, Request $request) |
305 | |||
306 | /** |
||
307 | * To add a new note to the project. |
||
308 | * |
||
309 | * @param Project $project |
||
310 | * @param Note $note |
||
311 | * @param FormRequest\Note $request |
||
312 | * |
||
313 | * @return \Illuminate\Http\RedirectResponse |
||
314 | */ |
||
315 | 2 | public function postAddNote(Project $project, Note $note, FormRequest\Note $request) |
|
323 | |||
324 | /** |
||
325 | * Ajax: To update project note. |
||
326 | * |
||
327 | * @param Project $project |
||
328 | * @param Note $note |
||
329 | * @param Request $request |
||
330 | * |
||
331 | * @return \Symfony\Component\HttpFoundation\Response |
||
332 | */ |
||
333 | 1 | public function postEditNote(Project $project, Project\Note $note, Request $request) |
|
345 | |||
346 | /** |
||
347 | * Ajax: to delete a project note. |
||
348 | * |
||
349 | * @param Project $project |
||
350 | * @param Note $note |
||
351 | * |
||
352 | * @return \Symfony\Component\HttpFoundation\Response |
||
353 | */ |
||
354 | 1 | public function getDeleteNote(Project $project, Project\Note $note) |
|
360 | |||
361 | /** |
||
362 | * Ajax: generate the issues export file. |
||
363 | * |
||
364 | * @param Project $project |
||
365 | * @param Exporter $exporter |
||
366 | * @param Request $request |
||
367 | * |
||
368 | * @return \Symfony\Component\HttpFoundation\Response |
||
369 | */ |
||
370 | 4 | public function postExportIssues(Project $project, Exporter $exporter, Request $request) |
|
393 | |||
394 | /** |
||
395 | * Download and then delete an export file. |
||
396 | * |
||
397 | * @param Project $project |
||
398 | * @param string $file |
||
399 | * |
||
400 | * @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
||
401 | */ |
||
402 | 4 | public function getDownloadExport(Project $project, $file) |
|
410 | } |
||
411 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: