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 |
||
21 | class PermissionsController extends BaseDashboardController |
||
22 | { |
||
23 | /** |
||
24 | * Display a listing of the resource. |
||
25 | * |
||
26 | * @return Response |
||
27 | */ |
||
28 | 1 | public function index() |
|
34 | |||
35 | /** |
||
36 | * Create a new resource. |
||
37 | * |
||
38 | * @return Response |
||
39 | */ |
||
40 | 1 | public function create() |
|
44 | |||
45 | /** |
||
46 | * Store a newly created resource in storage. |
||
47 | * |
||
48 | * @param Request $request |
||
49 | * |
||
50 | * @return $this|\Illuminate\Http\RedirectResponse |
||
51 | */ |
||
52 | 2 | View Code Duplication | public function store(Request $request) |
69 | |||
70 | /** |
||
71 | * Show the form for editing the specified resource. |
||
72 | * |
||
73 | * @param int $id |
||
74 | * |
||
75 | * @return Response |
||
76 | */ |
||
77 | 2 | public function edit($id) |
|
87 | |||
88 | /** |
||
89 | * Update the specified resource in storage. |
||
90 | * |
||
91 | * @param \Illuminate\Http\Request $request |
||
92 | * @param int $id |
||
93 | * |
||
94 | * @return $this|\Illuminate\Http\RedirectResponse |
||
95 | */ |
||
96 | 3 | View Code Duplication | public function update(Request $request, $id) |
117 | |||
118 | /** |
||
119 | * Remove the specified resource from storage. |
||
120 | * |
||
121 | * @param int $id |
||
122 | * |
||
123 | * @return \Illuminate\Http\RedirectResponse |
||
124 | */ |
||
125 | 2 | public function delete($id) |
|
139 | } |
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.