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 |
||
17 | class HostkeysController extends Controller |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * Display a listing of the resource. |
||
22 | * |
||
23 | * @return Response |
||
24 | */ |
||
25 | View Code Duplication | public function index(Request $request) |
|
47 | |||
48 | /** |
||
49 | * Show the form for creating a new resource. |
||
50 | * |
||
51 | * @return Response |
||
52 | */ |
||
53 | public function create() |
||
57 | |||
58 | /** |
||
59 | * Store a newly created resource in storage. |
||
60 | * |
||
61 | * @return Response |
||
62 | */ |
||
63 | View Code Duplication | public function store(Request $request) |
|
103 | |||
104 | /** |
||
105 | * Display the specified resource. |
||
106 | * |
||
107 | * @param int $id |
||
108 | * @return Response |
||
109 | */ |
||
110 | public function show($id) |
||
114 | |||
115 | public function get($hostname) |
||
152 | |||
153 | public function request(Request $request){ |
||
154 | Validator::extend('without_spaces', function($attr, $value){ |
||
155 | return preg_match('/^\S*$/u', $value); |
||
156 | }); |
||
157 | $validator = Validator::make($request->all(), [ |
||
158 | 'host' => 'required|without_spaces|unique:host_keys,hostname', |
||
159 | 'description' => 'required', |
||
160 | ]); |
||
161 | if($validator->fails()) |
||
162 | { |
||
163 | Session::flash('message', 'Please fix the error(s) below'); |
||
164 | return redirect()->back() |
||
165 | ->withErrors($validator) |
||
166 | ->withInput(); |
||
167 | } |
||
168 | View Code Duplication | if(Auth::guest()){ $current_user = 1; } |
|
169 | else{ $current_user = (Auth::user())?((Auth::user()->id)?Auth::user()->id:1):1; } |
||
170 | $headers = ['Content-Type' => 'application/json']; |
||
171 | $data = [ |
||
172 | 'host' => $request->host, |
||
173 | 'client' => $request->client, |
||
174 | 'request' => 'Request', |
||
175 | 'deskripsi' => $request->description, |
||
176 | 'user_id' => $current_user |
||
177 | ]; |
||
178 | $body = json_encode($data); |
||
179 | $host = str_replace(array('https://', 'http://'), array('',''),$request->host); |
||
180 | |||
181 | try { |
||
182 | $url = "https://".$host."/api/v1/api-manager/request"; |
||
183 | $client = new \GuzzleHttp\Client(); |
||
184 | $res = $client->request('POST', $url,['headers'=>$headers,'body'=>$body]); |
||
185 | $response = $res->getBody(); |
||
186 | $responses = json_decode($response); |
||
187 | $msg = "success"; |
||
188 | } catch (GuzzleException $e) { |
||
189 | $msg = "error"; |
||
190 | $responses = $e; |
||
191 | } |
||
192 | |||
193 | if($msg == "success"){ |
||
194 | $responses = $responses; |
||
195 | }else { |
||
196 | try { |
||
197 | $urlz = "http://".$host."/api/v1/api-manager/request"; |
||
198 | $clientz = new \GuzzleHttp\Client(); |
||
199 | $resz = $clientz->request('POST', $urlz,['headers'=>$headers,'body'=>$body]); |
||
200 | $responsez = $resz->getBody(); |
||
201 | $responsesz = json_decode($responsez); |
||
202 | $msgz = "success"; |
||
203 | } catch (GuzzleException $er) { |
||
204 | $msgz = "error"; |
||
205 | $responsesz = $er; |
||
206 | } |
||
207 | |||
208 | if($msgz == "success"){ |
||
209 | $responses = $responsesz; |
||
210 | }else { |
||
211 | $message = $msgz.' - '.$responsesz->getMessage(); |
||
212 | Session::flash('message', $message); |
||
213 | return Redirect::to('host-keys'); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | if($responses->status == 200){ |
||
218 | $hostkey = New Hostkeys; |
||
219 | $hostkey->hostname = $host; |
||
220 | $hostkey->keys = ""; |
||
221 | $hostkey->state = $responses->result->request; |
||
222 | $hostkey->transition = "Propose To ".$responses->result->request; |
||
223 | $hostkey->user_id = $responses->result->user_id; |
||
224 | $hostkey->save(); |
||
225 | View Code Duplication | if(env('URL_APIMANAGER') != NULL){ |
|
226 | $url_apimanager = str_replace('"', '',env('URL_APIMANAGER')); |
||
227 | if($url_apimanager != "" || $url_apimanager != NULL || $url_apimanager != false || !empty($url_apimanager)){ |
||
228 | $this->send_apimanager($url_apimanager,$request,$current_user,$hostkey->transition); |
||
229 | } |
||
230 | } |
||
231 | Session::flash('message', 'Send Request Api Keys Successfuly'); |
||
232 | }else { |
||
233 | Session::flash('message', $responses->message); |
||
234 | } |
||
235 | |||
236 | return Redirect::to('host-keys'); |
||
237 | } |
||
238 | |||
239 | private function send_apimanager($url_apimanager,$request,$current_user,$keterangan){ |
||
291 | |||
292 | /** |
||
293 | * Show the form for editing the specified resource. |
||
294 | * |
||
295 | * @param int $id |
||
296 | * @return Response |
||
297 | */ |
||
298 | public function edit($id) |
||
302 | |||
303 | /** |
||
304 | * Update the specified resource in storage. |
||
305 | * |
||
306 | * @param int $id |
||
307 | * @return Response |
||
308 | */ |
||
309 | View Code Duplication | public function update(Request $request, $id) |
|
349 | |||
350 | /** |
||
351 | * Remove the specified resource from storage. |
||
352 | * |
||
353 | * @param int $id |
||
354 | * @return Response |
||
355 | */ |
||
356 | public function destroy($id) |
||
360 | |||
361 | } |
||
362 | |||
364 |
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.