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 |
||
38 | class PackController extends Controller |
||
39 | { |
||
40 | /** |
||
41 | * Display listing of all packs on the system. |
||
42 | * |
||
43 | * @param \Illuminate\Http\Request $request |
||
44 | * @return \Illuminate\View\View |
||
45 | */ |
||
46 | View Code Duplication | public function index(Request $request) |
|
56 | |||
57 | /** |
||
58 | * Display new pack creation form. |
||
59 | * |
||
60 | * @param \Illuminate\Http\Request $request |
||
61 | * @return \Illuminate\View\View |
||
62 | */ |
||
63 | public function create(Request $request) |
||
64 | { |
||
65 | return view('admin.packs.new', [ |
||
66 | 'services' => Service::with('options')->get(), |
||
67 | ]); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Display new pack creation modal for use with template upload. |
||
72 | * |
||
73 | * @param \Illuminate\Http\Request $request |
||
74 | * @return \Illuminate\View\View |
||
75 | */ |
||
76 | public function newTemplate(Request $request) |
||
82 | |||
83 | /** |
||
84 | * Handle create pack request and route user to location. |
||
85 | * |
||
86 | * @param \Illuminate\Http\Request $request |
||
87 | * @return \Illuminate\View\View |
||
88 | */ |
||
89 | public function store(Request $request) |
||
90 | { |
||
91 | $repo = new PackRepository; |
||
92 | |||
93 | try { |
||
94 | if ($request->input('action') === 'from_template') { |
||
95 | $pack = $repo->createWithTemplate($request->intersect(['option_id', 'file_upload'])); |
||
96 | } else { |
||
97 | $pack = $repo->create($request->intersect([ |
||
98 | 'name', 'description', 'version', 'option_id', |
||
99 | 'selectable', 'visible', 'locked', 'file_upload', |
||
100 | ])); |
||
101 | } |
||
102 | Alert::success('Pack successfully created on the system.')->flash(); |
||
103 | |||
104 | return redirect()->route('admin.packs.view', $pack->id); |
||
105 | } catch (DisplayValidationException $ex) { |
||
106 | return redirect()->route('admin.packs.new')->withErrors(json_decode($ex->getMessage()))->withInput(); |
||
107 | } catch (DisplayException $ex) { |
||
108 | Alert::danger($ex->getMessage())->flash(); |
||
109 | } catch (\Exception $ex) { |
||
110 | Log::error($ex); |
||
111 | Alert::danger('An error occured while attempting to add a new service pack. This error has been logged.')->flash(); |
||
112 | } |
||
113 | |||
114 | return redirect()->route('admin.packs.new')->withInput(); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Display pack view template to user. |
||
119 | * |
||
120 | * @param \Illuminate\Http\Request $request |
||
121 | * @param int $id |
||
122 | * @return \Illuminate\View\View |
||
123 | */ |
||
124 | public function view(Request $request, $id) |
||
131 | |||
132 | /** |
||
133 | * Handle updating or deleting pack information. |
||
134 | * |
||
135 | * @param \Illuminate\Http\Request $request |
||
136 | * @param int $id |
||
137 | * @return \Illuminate\Http\RedirectResponse |
||
138 | */ |
||
139 | View Code Duplication | public function update(Request $request, $id) |
|
140 | { |
||
141 | $repo = new PackRepository; |
||
142 | |||
143 | try { |
||
144 | if ($request->input('action') !== 'delete') { |
||
145 | $pack = $repo->update($id, $request->intersect([ |
||
146 | 'name', 'description', 'version', |
||
147 | 'option_id', 'selectable', 'visible', 'locked', |
||
148 | ])); |
||
149 | Alert::success('Pack successfully updated.')->flash(); |
||
150 | } else { |
||
151 | $repo->delete($id); |
||
152 | Alert::success('Pack was successfully deleted from the system.')->flash(); |
||
153 | |||
154 | return redirect()->route('admin.packs'); |
||
155 | } |
||
156 | } catch (DisplayValidationException $ex) { |
||
157 | return redirect()->route('admin.packs.view', $id)->withErrors(json_decode($ex->getMessage())); |
||
158 | } catch (DisplayException $ex) { |
||
159 | Alert::danger($ex->getMessage())->flash(); |
||
160 | } catch (\Exception $ex) { |
||
161 | Log::error($ex); |
||
162 | Alert::danger('An error occured while attempting to edit this service pack. This error has been logged.')->flash(); |
||
163 | } |
||
164 | |||
165 | return redirect()->route('admin.packs.view', $id); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Creates an archive of the pack and downloads it to the browser. |
||
170 | * |
||
171 | * @param \Illuminate\Http\Request $request |
||
172 | * @param int $id |
||
173 | * @param bool $files |
||
174 | * @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
||
175 | */ |
||
176 | public function export(Request $request, $id, $files = false) |
||
214 | } |
||
215 |
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.