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 SekolahController extends Controller |
||
| 31 | { |
||
| 32 | protected $sekolah; |
||
| 33 | protected $jenis_sekolah; |
||
| 34 | protected $master_zona; |
||
| 35 | protected $user; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Create a new controller instance. |
||
| 39 | * |
||
| 40 | * @return void |
||
|
|
|||
| 41 | */ |
||
| 42 | public function __construct() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Display a listing of the resource. |
||
| 56 | * |
||
| 57 | * @return \Illuminate\Http\Response |
||
| 58 | */ |
||
| 59 | public function index(Request $request) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Display a listing of the resource. |
||
| 91 | * |
||
| 92 | * @return \Illuminate\Http\Response |
||
| 93 | */ |
||
| 94 | public function get() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Show the form for creating a new resource. |
||
| 112 | * |
||
| 113 | * @return \Illuminate\Http\Response |
||
| 114 | */ |
||
| 115 | public function create() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Store a newly created resource in storage. |
||
| 193 | * |
||
| 194 | * @param \Illuminate\Http\Request $request |
||
| 195 | * @return \Illuminate\Http\Response |
||
| 196 | */ |
||
| 197 | public function store(Request $request) |
||
| 198 | { |
||
| 199 | $sekolah = $this->sekolah; |
||
| 200 | |||
| 201 | $validator = Validator::make($request->all(), [ |
||
| 202 | 'nama' => 'required|max:255', |
||
| 203 | 'npsn' => "required|between:4,17|unique:{$this->sekolah->getTable()},npsn,NULL,id,deleted_at,NULL", |
||
| 204 | 'jenis_sekolah_id' => "required|exists:{$this->jenis_sekolah->getTable()},id", |
||
| 205 | 'alamat' => 'required|max:255', |
||
| 206 | 'logo' => 'max:255', |
||
| 207 | 'foto_gedung' => 'max:255', |
||
| 208 | 'province_id' => "required|exists:{$this->province->getTable()},id", |
||
| 209 | 'city_id' => "required|exists:{$this->city->getTable()},id", |
||
| 210 | 'district_id' => "required|exists:{$this->district->getTable()},id", |
||
| 211 | 'village_id' => "required|exists:{$this->village->getTable()},id", |
||
| 212 | 'no_telp' => 'required|digits_between:10,12', |
||
| 213 | 'email' => 'required|email|max:255', |
||
| 214 | 'kode_zona' => "required|exists:{$this->master_zona->getTable()},id", |
||
| 215 | 'user_id' => "required|exists:{$this->user->getTable()},id", |
||
| 216 | ]); |
||
| 217 | |||
| 218 | View Code Duplication | if ($validator->fails()) { |
|
| 219 | $error = true; |
||
| 220 | $message = $validator->errors()->first(); |
||
| 221 | } else { |
||
| 222 | $sekolah->nama = $request->input('nama'); |
||
| 223 | $sekolah->npsn = $request->input('npsn'); |
||
| 224 | $sekolah->jenis_sekolah_id = $request->input('jenis_sekolah_id'); |
||
| 225 | $sekolah->alamat = $request->input('alamat'); |
||
| 226 | $sekolah->logo = $request->input('logo'); |
||
| 227 | $sekolah->foto_gedung = $request->input('foto_gedung'); |
||
| 228 | $sekolah->province_id = $request->input('province_id'); |
||
| 229 | $sekolah->city_id = $request->input('city_id'); |
||
| 230 | $sekolah->district_id = $request->input('district_id'); |
||
| 231 | $sekolah->village_id = $request->input('village_id'); |
||
| 232 | $sekolah->no_telp = $request->input('no_telp'); |
||
| 233 | $sekolah->email = $request->input('email'); |
||
| 234 | $sekolah->kode_zona = $request->input('kode_zona'); |
||
| 235 | $sekolah->user_id = $request->input('user_id'); |
||
| 236 | $sekolah->save(); |
||
| 237 | |||
| 238 | $error = false; |
||
| 239 | $message = 'Success'; |
||
| 240 | } |
||
| 241 | |||
| 242 | $response['error'] = $error; |
||
| 243 | $response['message'] = $message; |
||
| 244 | $response['status'] = true; |
||
| 245 | |||
| 246 | return response()->json($response); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Display the specified resource. |
||
| 251 | * |
||
| 252 | * @param \App\Sekolah $sekolah |
||
| 253 | * @return \Illuminate\Http\Response |
||
| 254 | */ |
||
| 255 | public function show($id) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Show the form for editing the specified resource. |
||
| 269 | * |
||
| 270 | * @param \App\Sekolah $sekolah |
||
| 271 | * @return \Illuminate\Http\Response |
||
| 272 | */ |
||
| 273 | public function edit($id) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Update the specified resource in storage. |
||
| 301 | * |
||
| 302 | * @param \Illuminate\Http\Request $request |
||
| 303 | * @param \App\Sekolah $sekolah |
||
| 304 | * @return \Illuminate\Http\Response |
||
| 305 | */ |
||
| 306 | public function update(Request $request, $id) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Remove the specified resource from storage. |
||
| 360 | * |
||
| 361 | * @param \App\Sekolah $sekolah |
||
| 362 | * @return \Illuminate\Http\Response |
||
| 363 | */ |
||
| 364 | View Code Duplication | public function destroy($id) |
|
| 380 | } |
||
| 381 |
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.