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:
Complex classes like ServiceController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ServiceController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class ServiceController extends Controller |
||
39 | { |
||
40 | public function __construct() |
||
44 | |||
45 | public function getIndex(Request $request) |
||
|
|||
46 | { |
||
47 | return view('admin.services.index', [ |
||
48 | 'services' => Models\Service::withCount('servers')->get(), |
||
49 | ]); |
||
50 | } |
||
51 | |||
52 | public function getNew(Request $request) |
||
53 | { |
||
54 | return view('admin.services.new'); |
||
55 | } |
||
56 | |||
57 | View Code Duplication | public function postNew(Request $request) |
|
58 | { |
||
59 | try { |
||
60 | $repo = new ServiceRepository\Service; |
||
61 | $service = $repo->create($request->only([ |
||
62 | 'name', |
||
63 | 'description', |
||
64 | 'file', |
||
65 | 'executable', |
||
66 | 'startup', |
||
67 | ])); |
||
68 | Alert::success('Successfully created new service!')->flash(); |
||
69 | |||
70 | return redirect()->route('admin.services.service', $service->id); |
||
71 | } catch (DisplayValidationException $ex) { |
||
72 | return redirect()->route('admin.services.new')->withErrors(json_decode($ex->getMessage()))->withInput(); |
||
73 | } catch (DisplayException $ex) { |
||
74 | Alert::danger($ex->getMessage())->flash(); |
||
75 | } catch (\Exception $ex) { |
||
76 | Log::error($ex); |
||
77 | Alert::danger('An error occured while attempting to add a new service.')->flash(); |
||
78 | } |
||
79 | |||
80 | return redirect()->route('admin.services.new')->withInput(); |
||
81 | } |
||
82 | |||
83 | public function getService(Request $request, $service) |
||
84 | { |
||
85 | return view('admin.services.view', [ |
||
86 | 'service' => Models\Service::with('options', 'options.servers')->findOrFail($service), |
||
87 | ]); |
||
88 | } |
||
89 | |||
90 | View Code Duplication | public function postService(Request $request, $service) |
|
91 | { |
||
92 | try { |
||
93 | $repo = new ServiceRepository\Service; |
||
94 | $repo->update($service, $request->only([ |
||
95 | 'name', |
||
96 | 'description', |
||
97 | 'file', |
||
98 | 'executable', |
||
99 | 'startup', |
||
100 | ])); |
||
101 | Alert::success('Successfully updated this service.')->flash(); |
||
102 | } catch (DisplayValidationException $ex) { |
||
103 | return redirect()->route('admin.services.service', $service)->withErrors(json_decode($ex->getMessage()))->withInput(); |
||
104 | } catch (DisplayException $ex) { |
||
105 | Alert::danger($ex->getMessage())->flash(); |
||
106 | } catch (\Exception $ex) { |
||
107 | Log::error($ex); |
||
108 | Alert::danger('An error occurred while attempting to update this service.')->flash(); |
||
109 | } |
||
110 | |||
111 | return redirect()->route('admin.services.service', $service)->withInput(); |
||
112 | } |
||
113 | |||
114 | View Code Duplication | public function deleteService(Request $request, $service) |
|
115 | { |
||
116 | try { |
||
117 | $repo = new ServiceRepository\Service; |
||
118 | $repo->delete($service); |
||
119 | Alert::success('Successfully deleted that service.')->flash(); |
||
120 | |||
121 | return redirect()->route('admin.services'); |
||
122 | } catch (DisplayException $ex) { |
||
123 | Alert::danger($ex->getMessage())->flash(); |
||
124 | } catch (\Exception $ex) { |
||
125 | Log::error($ex); |
||
126 | Alert::danger('An error was encountered while attempting to delete that service.')->flash(); |
||
127 | } |
||
128 | |||
129 | return redirect()->route('admin.services.service', $service); |
||
130 | } |
||
131 | |||
132 | public function getOption(Request $request, $service, $option) |
||
133 | { |
||
134 | $option = Models\ServiceOptions::with('service', 'variables')->findOrFail($option); |
||
135 | $option->setRelation('servers', $option->servers()->with('user')->paginate(25)); |
||
136 | |||
137 | return view('admin.services.options.view', [ |
||
138 | 'option' => $option, |
||
139 | ]); |
||
140 | } |
||
141 | |||
142 | public function postOption(Request $request, $service, $option) |
||
143 | { |
||
144 | try { |
||
145 | $repo = new ServiceRepository\Option; |
||
146 | $repo->update($option, $request->only([ |
||
147 | 'name', |
||
148 | 'description', |
||
149 | 'tag', |
||
150 | 'executable', |
||
151 | 'docker_image', |
||
152 | 'startup', |
||
153 | ])); |
||
154 | Alert::success('Option settings successfully updated.')->flash(); |
||
155 | } catch (DisplayValidationException $ex) { |
||
156 | return redirect()->route('admin.services.option', [$service, $option])->withErrors(json_decode($ex->getMessage()))->withInput(); |
||
157 | } catch (\Exception $ex) { |
||
158 | Log::error($ex); |
||
159 | Alert::danger('An error occured while attempting to modify this option.')->flash(); |
||
160 | } |
||
161 | |||
162 | return redirect()->route('admin.services.option', [$service, $option])->withInput(); |
||
163 | } |
||
164 | |||
165 | View Code Duplication | public function deleteOption(Request $request, $service, $option) |
|
166 | { |
||
167 | try { |
||
168 | $repo = new ServiceRepository\Option; |
||
169 | $repo->delete($option); |
||
170 | |||
171 | Alert::success('Successfully deleted that option.')->flash(); |
||
172 | |||
173 | return redirect()->route('admin.services.service', $service); |
||
174 | } catch (DisplayException $ex) { |
||
175 | Alert::danger($ex->getMessage())->flash(); |
||
176 | } catch (\Exception $ex) { |
||
177 | Log::error($ex); |
||
178 | Alert::danger('An error was encountered while attempting to delete this option.')->flash(); |
||
179 | } |
||
180 | |||
181 | return redirect()->route('admin.services.option', [$service, $option]); |
||
182 | } |
||
183 | |||
184 | public function postOptionVariable(Request $request, $service, $option, $variable) |
||
217 | |||
218 | public function getNewVariable(Request $request, $service, $option) |
||
219 | { |
||
220 | return view('admin.services.options.variable', [ |
||
221 | 'option' => Models\ServiceOptions::with('service')->findOrFail($option), |
||
222 | ]); |
||
223 | } |
||
224 | |||
225 | public function postNewVariable(Request $request, $service, $option) |
||
226 | { |
||
227 | try { |
||
253 | |||
254 | public function newOption(Request $request, $service) |
||
260 | |||
261 | public function postNewOption(Request $request, $service) |
||
280 | |||
281 | public function deleteVariable(Request $request, $service, $option, $variable) |
||
296 | |||
297 | public function getConfiguration(Request $request, $serviceId) |
||
309 | |||
310 | public function postConfiguration(Request $request, $serviceId) |
||
332 | } |
||
333 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.