Conditions | 3 |
Paths | 3 |
Total Lines | 60 |
Code Lines | 48 |
Lines | 31 |
Ratio | 51.67 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
100 | public function store(Request $request) |
||
101 | { |
||
102 | $orang_tua = $this->orang_tua; |
||
103 | |||
104 | $validator = Validator::make($request->all(), [ |
||
105 | 'user_id' => 'required', |
||
106 | 'nomor_un' => 'required|max:255', |
||
107 | 'no_kk' => 'required|max:255', |
||
108 | 'no_telp' => 'required|max:255', |
||
109 | 'nama_ayah' => 'required|max:255', |
||
110 | 'nama_ibu' => 'required|max:255', |
||
111 | 'pendidikan_ayah' => 'required|max:255', |
||
112 | 'kerja_ayah' => 'required|max:255', |
||
113 | 'pendidikan_ibu' => 'required|max:255', |
||
114 | 'kerja_ibu' => 'required|max:255', |
||
115 | 'alamat_ortu' => 'required|max:255', |
||
116 | ]); |
||
117 | |||
118 | if($validator->fails()){ |
||
119 | $check = $orang_tua->where('label',$request->label)->whereNull('deleted_at')->count(); |
||
120 | |||
121 | if ($check > 0) { |
||
122 | $response['message'] = 'Failed, label ' . $request->label . ' already exists'; |
||
123 | View Code Duplication | } else { |
|
124 | $orang_tua->user_id = $request->input('user_id'); |
||
125 | $orang_tua->nomor_un = $request->input('nomor_un'); |
||
126 | $orang_tua->no_kk = $request->input('no_kk'); |
||
127 | $orang_tua->no_telp = $request->input('no_telp'); |
||
128 | $orang_tua->nama_ayah = $request->input('nama_ayah'); |
||
129 | $orang_tua->nama_ibu = $request->input('nama_ibu'); |
||
130 | $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah'); |
||
131 | $orang_tua->kerja_ayah = $request->input('kerja_ayah'); |
||
132 | $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu'); |
||
133 | $orang_tua->kerja_ibu = $request->input('kerja_ibu'); |
||
134 | $orang_tua->alamat_ortu = $request->input('alamat_ortu'); |
||
135 | $orang_tua->save(); |
||
136 | |||
137 | $response['message'] = 'success'; |
||
138 | } |
||
139 | |||
140 | View Code Duplication | } else { |
|
141 | $orang_tua->user_id = $request->input('user_id'); |
||
142 | $orang_tua->nomor_un = $request->input('nomor_un'); |
||
143 | $orang_tua->no_kk = $request->input('no_kk'); |
||
144 | $orang_tua->no_telp = $request->input('no_telp'); |
||
145 | $orang_tua->nama_ayah = $request->input('nama_ayah'); |
||
146 | $orang_tua->nama_ibu = $request->input('nama_ibu'); |
||
147 | $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah'); |
||
148 | $orang_tua->kerja_ayah = $request->input('kerja_ayah'); |
||
149 | $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu'); |
||
150 | $orang_tua->kerja_ibu = $request->input('kerja_ibu'); |
||
151 | $orang_tua->alamat_ortu = $request->input('alamat_ortu'); |
||
152 | $orang_tua->save(); |
||
153 | $response['message'] = 'success'; |
||
154 | } |
||
155 | |||
156 | $response['loaded'] = true; |
||
157 | |||
158 | return response()->json($response); |
||
159 | } |
||
160 | |||
263 |
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.