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 |
||
| 25 | class MasterZonaController extends Controller |
||
| 26 | { |
||
| 27 | protected $master_zona; |
||
| 28 | protected $user; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Create a new controller instance. |
||
| 32 | * |
||
| 33 | * @return void |
||
|
|
|||
| 34 | */ |
||
| 35 | public function __construct() |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Display a listing of the resource. |
||
| 43 | * |
||
| 44 | * @return \Illuminate\Http\Response |
||
| 45 | */ |
||
| 46 | public function index(Request $request) |
||
| 47 | { |
||
| 48 | if (request()->has('sort')) { |
||
| 49 | list($sortCol, $sortDir) = explode('|', request()->sort); |
||
| 50 | |||
| 51 | $query = $this->master_zona->orderBy($sortCol, $sortDir); |
||
| 52 | } else { |
||
| 53 | $query = $this->master_zona->orderBy('id', 'asc'); |
||
| 54 | } |
||
| 55 | |||
| 56 | if ($request->exists('filter')) { |
||
| 57 | $query->where(function($q) use($request) { |
||
| 58 | $value = "%{$request->filter}%"; |
||
| 59 | |||
| 60 | $q->where('tingkat', 'like', $value) |
||
| 61 | ->orWhere('kode', 'like', $value) |
||
| 62 | ->orWhere('label', 'like', $value); |
||
| 63 | }); |
||
| 64 | } |
||
| 65 | |||
| 66 | $perPage = request()->has('per_page') ? (int) request()->per_page : null; |
||
| 67 | |||
| 68 | $response = $query->with(['user'])->paginate($perPage); |
||
| 69 | |||
| 70 | return response()->json($response) |
||
| 71 | ->header('Access-Control-Allow-Origin', '*') |
||
| 72 | ->header('Access-Control-Allow-Methods', 'GET'); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Display a listing of the resource. |
||
| 77 | * |
||
| 78 | * @return \Illuminate\Http\Response |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function get() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Show the form for creating a new resource. |
||
| 94 | * |
||
| 95 | * @return \Illuminate\Http\Response |
||
| 96 | */ |
||
| 97 | public function create() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Store a newly created resource in storage. |
||
| 139 | * |
||
| 140 | * @param \Illuminate\Http\Request $request |
||
| 141 | * @return \Illuminate\Http\Response |
||
| 142 | */ |
||
| 143 | View Code Duplication | public function store(Request $request) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Display the specified resource. |
||
| 179 | * |
||
| 180 | * @param \App\MasterZona $master-zona |
||
| 181 | * @return \Illuminate\Http\Response |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function show($id) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Show the form for editing the specified resource. |
||
| 197 | * |
||
| 198 | * @param \App\MasterZona $master_zona |
||
| 199 | * @return \Illuminate\Http\Response |
||
| 200 | */ |
||
| 201 | public function edit($id) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Update the specified resource in storage. |
||
| 247 | * |
||
| 248 | * @param \Illuminate\Http\Request $request |
||
| 249 | * @param \App\MasterZona $master_zona |
||
| 250 | * @return \Illuminate\Http\Response |
||
| 251 | */ |
||
| 252 | View Code Duplication | public function update(Request $request, $id) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Remove the specified resource from storage. |
||
| 287 | * |
||
| 288 | * @param \App\MasterZona $master_zona |
||
| 289 | * @return \Illuminate\Http\Response |
||
| 290 | */ |
||
| 291 | View Code Duplication | public function destroy($id) |
|
| 307 | } |
||
| 308 |
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.