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:
Complex classes like ProjectController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProjectController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class ProjectController extends Controller |
||
30 | { |
||
31 | /** |
||
32 | * Display activity for a project. |
||
33 | * |
||
34 | * @param Project $project |
||
35 | * |
||
36 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
37 | */ |
||
38 | public function getIndex(Project $project) |
||
59 | |||
60 | /** |
||
61 | * Display issues for a project. |
||
62 | * |
||
63 | * @param FilterForm $filterForm |
||
64 | * @param Request $request |
||
65 | * @param Project $project |
||
66 | * @param int $status |
||
67 | * |
||
68 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
69 | */ |
||
70 | public function getIssues(FilterForm $filterForm, Request $request, Project $project, $status = Issue::STATUS_OPEN) |
||
87 | |||
88 | /** |
||
89 | * Display issues assigned to current user for a project. |
||
90 | * |
||
91 | * @param Project $project |
||
92 | * |
||
93 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
94 | */ |
||
95 | View Code Duplication | public function getAssigned(Project $project) |
|
107 | |||
108 | /** |
||
109 | * Display issues created to current user for a project. |
||
110 | * |
||
111 | * @param Project $project |
||
112 | * |
||
113 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
114 | */ |
||
115 | View Code Duplication | public function getCreated(Project $project) |
|
127 | |||
128 | /** |
||
129 | * Display notes for a project. |
||
130 | * |
||
131 | * @param Project $project |
||
132 | * @param NoteForm $form |
||
133 | * |
||
134 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
135 | */ |
||
136 | View Code Duplication | public function getNotes(Project $project, NoteForm $form) |
|
149 | |||
150 | /** |
||
151 | * @param Project $project |
||
152 | * @param string $view |
||
153 | * @param null $data |
||
154 | * @param bool $status |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | protected function projectMainViewTabs(Project $project, $view, $data = null, $status = false) |
||
221 | |||
222 | /** |
||
223 | * Edit the project. |
||
224 | * |
||
225 | * @param Project $project |
||
226 | 3 | * @param Form $form |
|
227 | * |
||
228 | 3 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
229 | 3 | */ |
|
230 | 3 | public function getEdit(Project $project, Form $form) |
|
238 | |||
239 | /** |
||
240 | * To update project details. |
||
241 | * |
||
242 | * @param Project $project |
||
243 | 3 | * @param FormRequest\Project $request |
|
244 | * |
||
245 | * @return \Illuminate\Http\RedirectResponse |
||
246 | 3 | */ |
|
247 | 1 | public function postEdit(Project $project, FormRequest\Project $request) |
|
262 | |||
263 | /** |
||
264 | * Ajax: returns list of users that are not in the project. |
||
265 | * |
||
266 | 1 | * @param Project $project |
|
267 | * |
||
268 | 1 | * @return \Symfony\Component\HttpFoundation\Response |
|
269 | */ |
||
270 | 1 | public function getInactiveUsers(Project $project) |
|
276 | |||
277 | /** |
||
278 | * Ajax: add user to the project. |
||
279 | * |
||
280 | * @param Project $project |
||
281 | 1 | * @param Request $request |
|
282 | * |
||
283 | 1 | * @return \Symfony\Component\HttpFoundation\Response |
|
284 | 1 | */ |
|
285 | 1 | View Code Duplication | public function postAssign(Project $project, Request $request) |
295 | |||
296 | /** |
||
297 | * Ajax: remove user from the project. |
||
298 | * |
||
299 | * @param Project $project |
||
300 | 1 | * @param Request $request |
|
301 | * |
||
302 | 1 | * @return \Symfony\Component\HttpFoundation\Response |
|
303 | 1 | */ |
|
304 | 1 | View Code Duplication | public function postUnassign(Project $project, Request $request) |
314 | |||
315 | /** |
||
316 | * To add a new note to the project. |
||
317 | * |
||
318 | * @param Project $project |
||
319 | * @param Note $note |
||
320 | 2 | * @param FormRequest\Note $request |
|
321 | * |
||
322 | 2 | * @return \Illuminate\Http\RedirectResponse |
|
323 | 2 | */ |
|
324 | 2 | public function postAddNote(Project $project, Note $note, FormRequest\Note $request) |
|
332 | |||
333 | /** |
||
334 | * Ajax: To update project note. |
||
335 | * |
||
336 | * @param Project $project |
||
337 | * @param Note $note |
||
338 | 1 | * @param Request $request |
|
339 | * |
||
340 | 1 | * @return \Symfony\Component\HttpFoundation\Response |
|
341 | 1 | */ |
|
342 | 1 | public function postEditNote(Project $project, Project\Note $note, Request $request) |
|
354 | |||
355 | /** |
||
356 | * Ajax: to delete a project note. |
||
357 | * |
||
358 | * @param Project $project |
||
359 | 1 | * @param Note $note |
|
360 | * |
||
361 | 1 | * @return \Symfony\Component\HttpFoundation\Response |
|
362 | */ |
||
363 | 1 | public function getDeleteNote(Project $project, Project\Note $note) |
|
369 | |||
370 | /** |
||
371 | * Ajax: generate the issues export file. |
||
372 | * |
||
373 | * @param Project $project |
||
374 | * @param Exporter $exporter |
||
375 | 4 | * @param Request $request |
|
376 | * |
||
377 | * @return \Symfony\Component\HttpFoundation\Response |
||
378 | 4 | */ |
|
379 | 4 | public function postExportIssues(Project $project, Exporter $exporter, Request $request) |
|
402 | |||
403 | /** |
||
404 | * Download and then delete an export file. |
||
405 | * |
||
406 | * @param Project $project |
||
407 | 4 | * @param string $file |
|
408 | * |
||
409 | * @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
||
410 | 4 | */ |
|
411 | public function getDownloadExport(Project $project, $file) |
||
419 | } |
||
420 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: