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 |
||
| 26 | class ProdiSekolahController extends Controller |
||
| 27 | { |
||
| 28 | protected $prodi_sekolah; |
||
| 29 | protected $sekolah; |
||
| 30 | protected $program_keahlian; |
||
| 31 | protected $user; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Create a new controller instance. |
||
| 35 | * |
||
| 36 | * @return void |
||
|
|
|||
| 37 | */ |
||
| 38 | public function __construct() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Display a listing of the resource. |
||
| 48 | * |
||
| 49 | * @return \Illuminate\Http\Response |
||
| 50 | */ |
||
| 51 | public function index(Request $request) |
||
| 52 | { |
||
| 53 | if (request()->has('sort')) { |
||
| 54 | list($sortCol, $sortDir) = explode('|', request()->sort); |
||
| 55 | |||
| 56 | $query = $this->prodi_sekolah->orderBy($sortCol, $sortDir); |
||
| 57 | } else { |
||
| 58 | $query = $this->prodi_sekolah->orderBy('id', 'asc'); |
||
| 59 | } |
||
| 60 | |||
| 61 | if ($request->exists('filter')) { |
||
| 62 | $query->where(function($q) use($request) { |
||
| 63 | $value = "%{$request->filter}%"; |
||
| 64 | |||
| 65 | $q->where('keterangan', 'like', $value) |
||
| 66 | ->orWhere('kuota_siswa', 'like', $value); |
||
| 67 | }); |
||
| 68 | } |
||
| 69 | |||
| 70 | $perPage = request()->has('per_page') ? (int) request()->per_page : null; |
||
| 71 | |||
| 72 | $response = $query->with(['sekolah', 'program_keahlian', 'user'])->paginate($perPage); |
||
| 73 | |||
| 74 | return response()->json($response) |
||
| 75 | ->header('Access-Control-Allow-Origin', '*') |
||
| 76 | ->header('Access-Control-Allow-Methods', 'GET'); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Display a listing of the resource. |
||
| 81 | * |
||
| 82 | * @return \Illuminate\Http\Response |
||
| 83 | */ |
||
| 84 | public function get() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Display a listing of the resource. |
||
| 98 | * |
||
| 99 | * @return \Illuminate\Http\Response |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function getBySekolah($id) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Show the form for creating a new resource. |
||
| 115 | * |
||
| 116 | * @return \Illuminate\Http\Response |
||
| 117 | */ |
||
| 118 | public function create() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Store a newly created resource in storage. |
||
| 166 | * |
||
| 167 | * @param \Illuminate\Http\Request $request |
||
| 168 | * @return \Illuminate\Http\Response |
||
| 169 | */ |
||
| 170 | public function store(Request $request) |
||
| 171 | { |
||
| 172 | $prodi_sekolah = $this->prodi_sekolah; |
||
| 173 | |||
| 174 | $validator = Validator::make($request->all(), [ |
||
| 175 | 'sekolah_id' => "required|exists:{$this->sekolah->getTable()},id", |
||
| 176 | 'program_keahlian_id' => "required|exists:{$this->program_keahlian->getTable()},id|unique:{$this->prodi_sekolah->getTable()},program_keahlian_id,NULL,id,sekolah_id,{$request->input('sekolah_id')},deleted_at,NULL", |
||
| 177 | 'kuota_siswa' => 'required|numeric|min:0|max:100000', |
||
| 178 | 'keterangan' => 'max:255', |
||
| 179 | 'user_id' => "required|exists:{$this->user->getTable()},id", |
||
| 180 | ]); |
||
| 181 | |||
| 182 | View Code Duplication | if ($validator->fails()) { |
|
| 183 | $error = true; |
||
| 184 | $message = $validator->errors()->first(); |
||
| 185 | } else { |
||
| 186 | $prodi_sekolah->sekolah_id = $request->input('sekolah_id'); |
||
| 187 | $prodi_sekolah->program_keahlian_id = $request->input('program_keahlian_id'); |
||
| 188 | $prodi_sekolah->kuota_siswa = $request->input('kuota_siswa'); |
||
| 189 | $prodi_sekolah->keterangan = $request->input('keterangan'); |
||
| 190 | $prodi_sekolah->user_id = $request->input('user_id'); |
||
| 191 | $prodi_sekolah->save(); |
||
| 192 | |||
| 193 | $error = false; |
||
| 194 | $message = 'Success'; |
||
| 195 | } |
||
| 196 | |||
| 197 | $response['prodi_sekolah'] = $prodi_sekolah; |
||
| 198 | $response['error'] = $error; |
||
| 199 | $response['message'] = $message; |
||
| 200 | $response['status'] = true; |
||
| 201 | |||
| 202 | return response()->json($response); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Display the specified resource. |
||
| 207 | * |
||
| 208 | * @param \App\ProdiSekolah $prodi_sekolah |
||
| 209 | * @return \Illuminate\Http\Response |
||
| 210 | */ |
||
| 211 | View Code Duplication | public function show($id) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Show the form for editing the specified resource. |
||
| 225 | * |
||
| 226 | * @param \App\Sekolah $sekolah |
||
| 227 | * @return \Illuminate\Http\Response |
||
| 228 | */ |
||
| 229 | public function edit($id) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Update the specified resource in storage. |
||
| 281 | * |
||
| 282 | * @param \Illuminate\Http\Request $request |
||
| 283 | * @param \App\Sekolah $sekolah |
||
| 284 | * @return \Illuminate\Http\Response |
||
| 285 | */ |
||
| 286 | public function update(Request $request, $id) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Remove the specified resource from storage. |
||
| 323 | * |
||
| 324 | * @param \App\Sekolah $sekolah |
||
| 325 | * @return \Illuminate\Http\Response |
||
| 326 | */ |
||
| 327 | View Code Duplication | public function destroy($id) |
|
| 343 | } |
||
| 344 |
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.