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 |
||
15 | class RoleController extends Controller |
||
16 | { |
||
17 | /** |
||
18 | * Display a listing of the resource. |
||
19 | * |
||
20 | * @param RoleDataTable $table |
||
21 | * @return Response |
||
22 | * @throws AuthorizationException |
||
23 | */ |
||
24 | public function index(RoleDataTable $table) |
||
29 | |||
30 | /** |
||
31 | * Show the form for creating a new resource. |
||
32 | * |
||
33 | * @return View |
||
34 | * @throws AuthorizationException |
||
35 | */ |
||
36 | public function create() |
||
41 | |||
42 | /** |
||
43 | * Store a newly created resource in storage. |
||
44 | * |
||
45 | * @param StoreFormRequest $request |
||
46 | * @return RedirectResponse |
||
47 | * @throws AuthorizationException |
||
48 | */ |
||
49 | View Code Duplication | public function store(StoreFormRequest $request) |
|
56 | |||
57 | /** |
||
58 | * Display the specified resource. |
||
59 | * |
||
60 | * @param Role $role |
||
61 | * @return View |
||
62 | * @throws AuthorizationException |
||
63 | */ |
||
64 | public function show(Role $role) |
||
69 | |||
70 | /** |
||
71 | * Show the form for editing the specified resource. |
||
72 | * |
||
73 | * @param Role $role |
||
74 | * @return View |
||
75 | * @throws AuthorizationException |
||
76 | */ |
||
77 | public function edit(Role $role) |
||
82 | |||
83 | /** |
||
84 | * Update the specified resource in storage. |
||
85 | * |
||
86 | * @param UpdateFormRequest $request |
||
87 | * @param Role $role |
||
88 | * @return RedirectResponse |
||
89 | * @throws AuthorizationException |
||
90 | */ |
||
91 | View Code Duplication | public function update(UpdateFormRequest $request, Role $role) |
|
98 | |||
99 | /** |
||
100 | * Remove the specified resource from storage. |
||
101 | * |
||
102 | * @param Role $role |
||
103 | * @return RedirectResponse |
||
104 | * @throws AuthorizationException |
||
105 | * @throws Exception |
||
106 | */ |
||
107 | public function destroy(Role $role) |
||
113 | } |
||
114 |
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.