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 |
||
| 30 | class SiswaController extends Controller |
||
| 31 | { |
||
| 32 | protected $siswa; |
||
| 33 | protected $province; |
||
| 34 | protected $city; |
||
| 35 | protected $district; |
||
| 36 | protected $village; |
||
| 37 | protected $sekolah; |
||
| 38 | protected $prodi_sekolah; |
||
| 39 | protected $user; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Create a new controller instance. |
||
| 43 | * |
||
| 44 | * @return void |
||
|
|
|||
| 45 | */ |
||
| 46 | public function __construct() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Display a listing of the resource. |
||
| 60 | * |
||
| 61 | * @return \Illuminate\Http\Response |
||
| 62 | */ |
||
| 63 | public function index(Request $request) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Display a listing of the resource. |
||
| 102 | * |
||
| 103 | * @return \Illuminate\Http\Response |
||
| 104 | */ |
||
| 105 | View Code Duplication | public function get() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Show the form for creating a new resource. |
||
| 119 | * |
||
| 120 | * @return \Illuminate\Http\Response |
||
| 121 | */ |
||
| 122 | public function create(Request $request) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Store a newly created resource in storage. |
||
| 188 | * |
||
| 189 | * @param \Illuminate\Http\Request $request |
||
| 190 | * @return \Illuminate\Http\Response |
||
| 191 | */ |
||
| 192 | public function store(Request $request) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Display the specified resource. |
||
| 254 | * |
||
| 255 | * @param \App\Siswa $siswa |
||
| 256 | * @return \Illuminate\Http\Response |
||
| 257 | */ |
||
| 258 | View Code Duplication | public function show($id) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Show the form for editing the specified resource. |
||
| 272 | * |
||
| 273 | * @param \App\Siswa $siswa |
||
| 274 | * @return \Illuminate\Http\Response |
||
| 275 | */ |
||
| 276 | public function edit($id) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Update the specified resource in storage. |
||
| 296 | * |
||
| 297 | * @param \Illuminate\Http\Request $request |
||
| 298 | * @param \App\Siswa $siswa |
||
| 299 | * @return \Illuminate\Http\Response |
||
| 300 | */ |
||
| 301 | public function update(Request $request, $id) |
||
| 302 | { |
||
| 303 | $siswa = $this->siswa->with(['province', 'city', 'district', 'village', 'sekolah', 'prodi_sekolah', 'user', 'kegiatan'])->findOrFail($id); |
||
| 304 | |||
| 305 | $validator = Validator::make($request->all(), [ |
||
| 306 | 'nomor_un' => "required|max:255|unique:{$this->siswa->getTable()},nomor_un,{$id},id,deleted_at,NULL", |
||
| 307 | 'nik' => "required|digits:16|unique:{$this->siswa->getTable()},nik,{$id},id,deleted_at,NULL", |
||
| 308 | 'nama_siswa' => 'required|max:255', |
||
| 309 | 'no_kk' => "required|digits:16|unique:{$this->siswa->getTable()},no_kk,{$id},id,deleted_at,NULL", |
||
| 310 | 'alamat_kk' => 'required|max:255', |
||
| 311 | 'province_id' => "required|exists:{$this->province->getTable()},id", |
||
| 312 | 'city_id' => "required|exists:{$this->city->getTable()},id", |
||
| 313 | 'district_id' => "required|exists:{$this->district->getTable()},id", |
||
| 314 | 'village_id' => "required|exists:{$this->village->getTable()},id", |
||
| 315 | 'tempat_lahir' => 'required|max:255', |
||
| 316 | 'tgl_lahir' => 'required|date', |
||
| 317 | 'jenis_kelamin' => 'required|max:255', |
||
| 318 | 'agama' => 'required|max:255', |
||
| 319 | 'nisn' => "required|between:4,17|unique:{$this->siswa->getTable()},nisn,{$id},id,deleted_at,NULL", |
||
| 320 | 'tahun_lulus' => 'required|date_format:Y', |
||
| 321 | 'sekolah_id' => "required|exists:{$this->sekolah->getTable()},id", |
||
| 322 | 'prodi_sekolah_id' => "required|exists:{$this->prodi_sekolah->getTable()},id", |
||
| 323 | 'user_id' => "required|exists:{$this->user->getTable()},id", |
||
| 324 | ]); |
||
| 325 | |||
| 326 | View Code Duplication | if ($validator->fails()) { |
|
| 327 | $error = true; |
||
| 328 | $message = $validator->errors()->first(); |
||
| 329 | } else { |
||
| 330 | $siswa->user_id = $request->input('user_id'); |
||
| 331 | $siswa->nomor_un = $request->input('nomor_un'); |
||
| 332 | $siswa->nik = $request->input('nik'); |
||
| 333 | $siswa->nama_siswa = $request->input('nama_siswa'); |
||
| 334 | $siswa->no_kk = $request->input('no_kk'); |
||
| 335 | $siswa->alamat_kk = $request->input('alamat_kk'); |
||
| 336 | $siswa->province_id = $request->input('province_id'); |
||
| 337 | $siswa->city_id = $request->input('city_id'); |
||
| 338 | $siswa->district_id = $request->input('district_id'); |
||
| 339 | $siswa->village_id = $request->input('village_id'); |
||
| 340 | $siswa->tempat_lahir = $request->input('tempat_lahir'); |
||
| 341 | $siswa->tgl_lahir = $request->input('tgl_lahir'); |
||
| 342 | $siswa->jenis_kelamin = $request->input('jenis_kelamin'); |
||
| 343 | $siswa->agama = $request->input('agama'); |
||
| 344 | $siswa->nisn = $request->input('nisn'); |
||
| 345 | $siswa->sekolah_id = $request->input('sekolah_id'); |
||
| 346 | $siswa->prodi_sekolah_id = $request->input('prodi_sekolah_id'); |
||
| 347 | $siswa->tahun_lulus = $request->input('tahun_lulus'); |
||
| 348 | $siswa->save(); |
||
| 349 | |||
| 350 | $error = false; |
||
| 351 | $message = 'Success'; |
||
| 352 | } |
||
| 353 | |||
| 354 | $response['error'] = $error; |
||
| 355 | $response['message'] = $message; |
||
| 356 | $response['status'] = true; |
||
| 357 | |||
| 358 | return response()->json($response); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Remove the specified resource from storage. |
||
| 363 | * |
||
| 364 | * @param \App\Siswa $siswa |
||
| 365 | * @return \Illuminate\Http\Response |
||
| 366 | */ |
||
| 367 | public function destroy($id) |
||
| 383 | } |
||
| 384 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.