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 |
||
18 | class HostkeysController extends Controller |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * Display a listing of the resource. |
||
23 | * |
||
24 | * @return Response |
||
25 | */ |
||
26 | public function index(Request $request) |
||
48 | |||
49 | /** |
||
50 | * Show the form for creating a new resource. |
||
51 | * |
||
52 | * @return Response |
||
53 | */ |
||
54 | public function create() |
||
55 | { |
||
56 | return view('host_keys.create'); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Store a newly created resource in storage. |
||
61 | * |
||
62 | * @return Response |
||
63 | */ |
||
64 | View Code Duplication | public function store(Request $request) |
|
65 | { |
||
66 | try { |
||
67 | $token = $request->keys; |
||
68 | $hostkey = New Hostkeys; |
||
69 | $hostkey->hostname = str_replace(array('https://', 'http://'), array('',''),$request->hostname); |
||
70 | $hostkey->keys = $token; |
||
71 | $hostkey->state = $request->state; |
||
72 | $hostkey->transition = $request->transition; |
||
73 | $hostkey->user_id = $request->user_id; |
||
74 | if($request->state == 'Approved' || $request->state == 'approved' || $request->state == 'Rejected' || $request->state == 'rejected'){ |
||
75 | $hostkey->save(); |
||
76 | |||
77 | $error = false; |
||
78 | $statusCode = 200; |
||
79 | $title = 'Success'; |
||
80 | $type = 'success'; |
||
81 | $message = 'Data Saved Successfuly.'; |
||
82 | $result = $hostkey; |
||
83 | }else { |
||
84 | $error = true; |
||
85 | $statusCode = 404; |
||
86 | $title = 'Error'; |
||
87 | $type = 'error'; |
||
88 | $message = 'Error'; |
||
89 | $result = 'Not Found'; |
||
90 | } |
||
91 | } catch (Exception $e) { |
||
92 | $error = true; |
||
93 | $statusCode = 404; |
||
94 | $title = 'Error'; |
||
95 | $type = 'error'; |
||
96 | $message = 'Error'; |
||
97 | $result = 'Not Found'; |
||
98 | } finally { |
||
99 | return Response::json(array( |
||
100 | 'error' => $error, |
||
101 | 'status' => $statusCode, |
||
102 | 'title' => $title, |
||
103 | 'type' => $type, |
||
104 | 'message' => $message, |
||
105 | 'result' => $result |
||
106 | )); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Display the specified resource. |
||
112 | * |
||
113 | * @param int $id |
||
114 | * @return Response |
||
115 | */ |
||
116 | public function show($id) |
||
120 | |||
121 | public function get($hostname) |
||
158 | |||
159 | public function request(Request $request){ |
||
160 | $validator = Validator::make($request->all(), [ |
||
216 | |||
217 | private function send_apimanager($url_apimanager,$request,$current_user,$keterangan){ |
||
241 | |||
242 | /** |
||
243 | * Show the form for editing the specified resource. |
||
244 | * |
||
245 | * @param int $id |
||
246 | * @return Response |
||
247 | */ |
||
248 | public function edit($id) |
||
252 | |||
253 | /** |
||
254 | * Update the specified resource in storage. |
||
255 | * |
||
256 | * @param int $id |
||
257 | * @return Response |
||
258 | */ |
||
259 | View Code Duplication | public function update(Request $request, $id) |
|
304 | |||
305 | /** |
||
306 | * Remove the specified resource from storage. |
||
307 | * |
||
308 | * @param int $id |
||
309 | * @return Response |
||
310 | */ |
||
311 | public function destroy($id) |
||
315 | |||
316 | } |
||
317 | |||
319 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.