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 |
||
20 | class PermissionController extends Controller |
||
21 | { |
||
22 | /** |
||
23 | * Display a listing of the resource. |
||
24 | * |
||
25 | * @param Request $request |
||
26 | * |
||
27 | * @return \Illuminate\Http\Response |
||
28 | */ |
||
29 | public function index(Request $request) |
||
51 | |||
52 | /** |
||
53 | * Show the form for creating a new resource. |
||
54 | * |
||
55 | * @return \Illuminate\Http\Response |
||
56 | */ |
||
57 | public function create() |
||
62 | |||
63 | /** |
||
64 | * Store a newly created resource in storage. |
||
65 | * |
||
66 | * @param Request|PermissionRequest $request |
||
67 | * |
||
68 | * @return \Illuminate\Http\Response |
||
69 | */ |
||
70 | View Code Duplication | public function store(PermissionRequest $request) |
|
77 | |||
78 | |||
79 | /** |
||
80 | * Show the form for editing the specified resource. |
||
81 | * |
||
82 | * @param int|Permissions $permission |
||
83 | * |
||
84 | * @return \Illuminate\Http\Response |
||
85 | */ |
||
86 | public function edit(Permissions $permission) |
||
91 | |||
92 | /** |
||
93 | * Update the permission in storage. |
||
94 | * |
||
95 | * @param Request|PermissionRequest $request |
||
96 | * @param int $id |
||
97 | * |
||
98 | * @return \Illuminate\Http\Response |
||
99 | */ |
||
100 | View Code Duplication | public function update(PermissionRequest $request, $id) |
|
108 | |||
109 | /** |
||
110 | * Remove the permission from storage. |
||
111 | * |
||
112 | * @param int $id Permission Id |
||
113 | * |
||
114 | * @return \Illuminate\Http\Response |
||
115 | */ |
||
116 | View Code Duplication | public function destroy($id) |
|
124 | } |
||
125 |
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.