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 |
||
8 | View Code Duplication | class LocationController extends Controller |
|
|
|||
9 | { |
||
10 | /** |
||
11 | * Display a listing of the resource. |
||
12 | * |
||
13 | * @return \Illuminate\Http\Response |
||
14 | */ |
||
15 | public function __construct() |
||
16 | { |
||
17 | $this->middleware('auth'); |
||
18 | } |
||
19 | |||
20 | public function index() |
||
26 | |||
27 | /** |
||
28 | * Show the form for creating a new resource. |
||
29 | * |
||
30 | * @return \Illuminate\Http\Response |
||
31 | */ |
||
32 | public function create() |
||
36 | |||
37 | /** |
||
38 | * Store a newly created resource in storage. |
||
39 | * |
||
40 | * @param \Illuminate\Http\Request $request |
||
41 | * |
||
42 | * @return \Illuminate\Http\Response |
||
43 | */ |
||
44 | public function store(Request $request) |
||
57 | |||
58 | /** |
||
59 | * Display the specified resource. |
||
60 | * |
||
61 | * @param int $id |
||
62 | * |
||
63 | * @return \Illuminate\Http\Response |
||
64 | */ |
||
65 | public function show($id) |
||
69 | |||
70 | /** |
||
71 | * Show the form for editing the specified resource. |
||
72 | * |
||
73 | * @param int $id |
||
74 | * |
||
75 | * @return \Illuminate\Http\Response |
||
76 | */ |
||
77 | 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 \Illuminate\Http\Response |
||
95 | */ |
||
96 | public function update(Request $request, $id) |
||
114 | |||
115 | /** |
||
116 | * Remove the specified resource from storage. |
||
117 | * |
||
118 | * @param int $id |
||
119 | * |
||
120 | * @return \Illuminate\Http\Response |
||
121 | */ |
||
122 | public function destroy($id) |
||
128 | |||
129 | public function search(Request $request) |
||
130 | { |
||
131 | $constraints = [ |
||
132 | 'name' => $request['name'], |
||
133 | 'shortName' => $request['shortName'], |
||
134 | ]; |
||
135 | $locations = $this->doSearchingQuery($constraints); |
||
136 | |||
137 | return view('manteniments/location/index', ['locations' => $locations, 'searchingVals' => $constraints]); |
||
138 | } |
||
139 | |||
140 | private function doSearchingQuery($constraints) |
||
154 | |||
155 | private function validateInput($request) |
||
162 | } |
||
163 |
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.