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 |
||
39 | class OptionController extends Controller |
||
40 | { |
||
41 | /** |
||
42 | * Handles request to view page for adding new option. |
||
43 | * |
||
44 | * @param \Illuminate\Http\Request $request |
||
45 | * @return \Illuminate\View\View |
||
46 | */ |
||
47 | public function create(Request $request) |
||
|
|||
48 | { |
||
49 | $services = Service::with('options')->get(); |
||
50 | Javascript::put(['services' => $services->keyBy('id')]); |
||
51 | |||
52 | return view('admin.services.options.new', ['services' => $services]); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Handles POST request to create a new option. |
||
57 | * |
||
58 | * @param \Illuminate\Http\Request $request |
||
59 | * @return \Illuminate\Response\RedirectResponse |
||
60 | */ |
||
61 | View Code Duplication | public function store(Request $request) |
|
62 | { |
||
63 | $repo = new OptionRepository; |
||
64 | |||
65 | try { |
||
66 | $option = $repo->create($request->intersect([ |
||
67 | 'service_id', 'name', 'description', 'tag', |
||
68 | 'docker_image', 'startup', 'config_from', 'config_startup', |
||
69 | 'config_logs', 'config_files', 'config_stop', |
||
70 | ])); |
||
71 | Alert::success('Successfully created new service option.')->flash(); |
||
72 | |||
73 | return redirect()->route('admin.services.option.view', $option->id); |
||
74 | } catch (DisplayValidationException $ex) { |
||
75 | return redirect()->route('admin.services.option.new')->withErrors(json_decode($ex->getMessage()))->withInput(); |
||
76 | } catch (DisplayException $ex) { |
||
77 | Alert::danger($ex->getMessage())->flash(); |
||
78 | } catch (\Exception $ex) { |
||
79 | Log::error($ex); |
||
80 | Alert::danger('An unhandled exception occurred while attempting to create this service. This error has been logged.')->flash(); |
||
81 | } |
||
82 | |||
83 | return redirect()->route('admin.services.option.new')->withInput(); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Handles POST request to create a new option variable. |
||
88 | * |
||
89 | * @param \Illuminate\Http\Request $request |
||
90 | * @param int $id |
||
91 | * @return \Illuminate\Http\RedirectResponse |
||
92 | */ |
||
93 | public function createVariable(Request $request, $id) |
||
94 | { |
||
95 | $repo = new VariableRepository; |
||
96 | |||
97 | try { |
||
98 | $variable = $repo->create($id, $request->intersect([ |
||
99 | 'name', 'description', 'env_variable', |
||
100 | 'default_value', 'options', 'rules', |
||
101 | ])); |
||
102 | |||
103 | Alert::success('New variable successfully assigned to this service option.')->flash(); |
||
104 | } catch (DisplayValidationException $ex) { |
||
105 | return redirect()->route('admin.services.option.variables', $id)->withErrors(json_decode($ex->getMessage())); |
||
106 | } catch (DisplayException $ex) { |
||
107 | Alert::danger($ex->getMessage())->flash(); |
||
108 | } catch (\Exception $ex) { |
||
109 | Log::error($ex); |
||
110 | Alert::danger('An unhandled exception was encountered while attempting to process that request. This error has been logged.')->flash(); |
||
111 | } |
||
112 | |||
113 | return redirect()->route('admin.services.option.variables', $id); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Display option overview page. |
||
118 | * |
||
119 | * @param \Illuminate\Http\Request $request |
||
120 | * @param int $id |
||
121 | * @return \Illuminate\View\View |
||
122 | */ |
||
123 | public function viewConfiguration(Request $request, $id) |
||
127 | |||
128 | /** |
||
129 | * Display variable overview page for a service option. |
||
130 | * |
||
131 | * @param \Illuminate\Http\Request $request |
||
132 | * @param int $id |
||
133 | * @return \Illuminate\View\View |
||
134 | */ |
||
135 | public function viewVariables(Request $request, $id) |
||
139 | |||
140 | /** |
||
141 | * Display script management page for an option. |
||
142 | * |
||
143 | * @param Request $request |
||
144 | * @param int $id |
||
145 | * @return \Illuminate\View\View |
||
146 | */ |
||
147 | public function viewScripts(Request $request, $id) |
||
151 | |||
152 | /** |
||
153 | * Handles POST when editing a configration for a service option. |
||
154 | * |
||
155 | * @param \Illuminate\Http\Request $request |
||
156 | * @param int $id |
||
157 | * @return \Illuminate\Http\RedirectResponse |
||
158 | */ |
||
159 | public function editConfiguration(Request $request, $id) |
||
188 | |||
189 | /** |
||
190 | * Handles POST when editing a configration for a service option. |
||
191 | * |
||
192 | * @param \Illuminate\Http\Request $request |
||
193 | * @param int $option |
||
194 | * @param int $variable |
||
195 | * @return \Illuminate\Http\RedirectResponse |
||
196 | */ |
||
197 | public function editVariable(Request $request, $option, $variable) |
||
223 | |||
224 | /** |
||
225 | * Handles POST when updating scripts for a service option. |
||
226 | * |
||
227 | * @param Request $request |
||
228 | * @param int $id |
||
229 | * @return \Illuminate\Response\RedirectResponse |
||
230 | */ |
||
231 | View Code Duplication | public function updateScripts(Request $request, $id) |
|
249 | } |
||
250 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.