| Conditions | 4 |
| Paths | 6 |
| Total Lines | 61 |
| Code Lines | 46 |
| Lines | 29 |
| Ratio | 47.54 % |
| 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 |
||
| 193 | public function update(Request $request, $id) |
||
| 194 | { |
||
| 195 | $sktm = $this->sktm->findOrFail($id); |
||
| 196 | |||
| 197 | if ($request->input('user_id') == $request->input('user_id')) |
||
| 198 | { |
||
| 199 | $validator = Validator::make($request->all(), [ |
||
| 200 | 'user_id' => 'required|unique:sktms,user_id', |
||
| 201 | 'nomor_un' => 'required', |
||
| 202 | 'kode_sktm' => 'required', |
||
| 203 | 'nama_suket' => 'required', |
||
| 204 | 'instansi_suket' => 'required', |
||
| 205 | 'no_suket' => 'required', |
||
| 206 | 'nilai_sktm' => 'required', |
||
| 207 | ]); |
||
| 208 | } else { |
||
| 209 | $validator = Validator::make($request->all(), [ |
||
| 210 | 'user_id' => 'required|unique:sktms,user_id', |
||
| 211 | 'nomor_un' => 'required', |
||
| 212 | 'kode_sktm' => 'required', |
||
| 213 | 'nama_suket' => 'required', |
||
| 214 | 'instansi_suket' => 'required', |
||
| 215 | 'no_suket' => 'required', |
||
| 216 | 'nilai_sktm' => 'required', |
||
| 217 | ]); |
||
| 218 | } |
||
| 219 | |||
| 220 | View Code Duplication | if ($validator->fails()) { |
|
| 221 | $check = $sktm->where('user_id',$request->user_id)->whereNull('deleted_at')->count(); |
||
| 222 | |||
| 223 | if ($check > 0) { |
||
| 224 | $response['message'] = 'Failed, Username ' . $request->user_id . ' already exists'; |
||
| 225 | } else { |
||
| 226 | $sktm->user_id = $request->input('user_id'); |
||
| 227 | $sktm->nomor_un = $request->input('nomor_un'); |
||
| 228 | $sktm->kode_sktm = $request->input('kode_sktm'); |
||
| 229 | $sktm->nama_suket = $request->input('nama_suket'); |
||
| 230 | $sktm->instansi_suket = $request->input('instansi_suket'); |
||
| 231 | $sktm->no_suket = $request->input('no_suket'); |
||
| 232 | $sktm->nilai_sktm = $request->input('nilai_sktm'); |
||
| 233 | $sktm->save(); |
||
| 234 | |||
| 235 | $response['message'] = 'success'; |
||
| 236 | } |
||
| 237 | } else { |
||
| 238 | $sktm->user_id = $request->input('user_id'); |
||
| 239 | $sktm->nomor_un = $request->input('nomor_un'); |
||
| 240 | $sktm->kode_sktm = $request->input('kode_sktm'); |
||
| 241 | $sktm->nama_suket = $request->input('nama_suket'); |
||
| 242 | $sktm->instansi_suket = $request->input('instansi_suket'); |
||
| 243 | $sktm->no_suket = $request->input('no_suket'); |
||
| 244 | $sktm->nilai_sktm = $request->input('nilai_sktm'); |
||
| 245 | $sktm->save(); |
||
| 246 | |||
| 247 | $response['message'] = 'success'; |
||
| 248 | } |
||
| 249 | |||
| 250 | $response['status'] = true; |
||
| 251 | |||
| 252 | return response()->json($response); |
||
| 253 | } |
||
| 254 | |||
| 274 |
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.