| Conditions | 8 |
| Paths | 7 |
| Total Lines | 73 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 200 | public function update(Request $request, $id) |
||
| 201 | { |
||
| 202 | $response = array(); |
||
| 203 | $message = array(); |
||
| 204 | $orang_tua = $this->orang_tua->findOrFail($id); |
||
| 205 | |||
| 206 | $validator = Validator::make($request->all(), [ |
||
| 207 | 'user_id' => 'required|unique:orangtuas,user_id,'.$id, |
||
| 208 | 'nomor_un' => 'required |unique:orangtuas,nomor_un,'.$id, |
||
| 209 | 'no_kk' => 'required|unique:orangtuas,no_kk,'.$id, |
||
| 210 | 'no_telp' => 'required|unique:orangtuas,no_telp,'.$id, |
||
| 211 | 'nama_ayah' => 'required', |
||
| 212 | 'nama_ibu' => 'required', |
||
| 213 | 'pendidikan_ayah' => 'required', |
||
| 214 | 'kerja_ayah' => 'required', |
||
| 215 | 'pendidikan_ibu' => 'required', |
||
| 216 | 'kerja_ibu' => 'required', |
||
| 217 | 'alamat_ortu' => 'required', |
||
| 218 | ]); |
||
| 219 | |||
| 220 | if ($validator->fails()) { |
||
| 221 | |||
| 222 | foreach($validator->messages()->getMessages() as $key => $error){ |
||
| 223 | foreach($error AS $error_get) { |
||
| 224 | array_push($message, $error_get); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | $check_user = $this->orang_tua->where('id','!=', $id)->where('user_id', $request->user_id); |
||
| 229 | $check_nomor_un = $this->orang_tua->where('id','!=', $id)->where('nomor_un', $request->nomor_un); |
||
| 230 | $check_no_kk = $this->orang_tua->where('id','!=', $id)->where('no_kk', $request->no_kk); |
||
| 231 | $check_no_telp = $this->orang_tua->where('id','!=', $id)->where('no_telp', $request->no_telp); |
||
| 232 | |||
| 233 | if($check_user->count() > 0 || $check_nomor_un->count() > 0 || $check_no_kk->count() > 0 || $check_no_telp->count() > 0){ |
||
| 234 | $response['message'] = implode("\n",$message); |
||
| 235 | } else { |
||
| 236 | $orang_tua->user_id = $request->input('user_id'); |
||
| 237 | $orang_tua->nomor_un = $request->input('nomor_un'); |
||
| 238 | $orang_tua->no_kk = $request->input('no_kk'); |
||
| 239 | $orang_tua->no_telp = $request->input('no_telp'); |
||
| 240 | $orang_tua->nama_ayah = $request->input('nama_ayah'); |
||
| 241 | $orang_tua->nama_ibu = $request->input('nama_ibu'); |
||
| 242 | $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah'); |
||
| 243 | $orang_tua->kerja_ayah = $request->input('kerja_ayah'); |
||
| 244 | $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu'); |
||
| 245 | $orang_tua->kerja_ibu = $request->input('kerja_ibu'); |
||
| 246 | $orang_tua->alamat_ortu = $request->input('alamat_ortu'); |
||
| 247 | $orang_tua->save(); |
||
| 248 | |||
| 249 | $response['message'] = 'success'; |
||
| 250 | |||
| 251 | } |
||
| 252 | } else { |
||
| 253 | $orang_tua->user_id = $request->input('user_id'); |
||
| 254 | $orang_tua->nomor_un = $request->input('nomor_un'); |
||
| 255 | $orang_tua->no_kk = $request->input('no_kk'); |
||
| 256 | $orang_tua->no_telp = $request->input('no_telp'); |
||
| 257 | $orang_tua->nama_ayah = $request->input('nama_ayah'); |
||
| 258 | $orang_tua->nama_ibu = $request->input('nama_ibu'); |
||
| 259 | $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah'); |
||
| 260 | $orang_tua->kerja_ayah = $request->input('kerja_ayah'); |
||
| 261 | $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu'); |
||
| 262 | $orang_tua->kerja_ibu = $request->input('kerja_ibu'); |
||
| 263 | $orang_tua->alamat_ortu = $request->input('alamat_ortu'); |
||
| 264 | $orang_tua->save(); |
||
| 265 | |||
| 266 | $response['message'] = 'success'; |
||
| 267 | } |
||
| 268 | |||
| 269 | $response['loaded'] = true; |
||
| 270 | |||
| 271 | return response()->json($response); |
||
| 272 | } |
||
| 273 | |||
| 293 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.