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 |
||
| 23 | class JenisSekolahController extends Controller |
||
| 24 | { |
||
| 25 | protected $jenis_sekolah; |
||
| 26 | protected $user; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Create a new controller instance. |
||
| 30 | * |
||
| 31 | * @return void |
||
|
|
|||
| 32 | */ |
||
| 33 | public function __construct() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Display a listing of the resource. |
||
| 41 | * |
||
| 42 | * @return \Illuminate\Http\Response |
||
| 43 | */ |
||
| 44 | public function index(Request $request) |
||
| 45 | { |
||
| 46 | if (request()->has('sort')) { |
||
| 47 | list($sortCol, $sortDir) = explode('|', request()->sort); |
||
| 48 | |||
| 49 | $query = $this->jenis_sekolah->orderBy($sortCol, $sortDir); |
||
| 50 | } else { |
||
| 51 | $query = $this->jenis_sekolah->orderBy('id', 'asc'); |
||
| 52 | } |
||
| 53 | |||
| 54 | if ($request->exists('filter')) { |
||
| 55 | $query->where(function($q) use($request) { |
||
| 56 | $value = "%{$request->filter}%"; |
||
| 57 | |||
| 58 | $q->where('jenis_sekolah', 'like', $value); |
||
| 59 | }); |
||
| 60 | } |
||
| 61 | |||
| 62 | $perPage = request()->has('per_page') ? (int) request()->per_page : null; |
||
| 63 | |||
| 64 | $response = $query->with('user')->paginate($perPage); |
||
| 65 | |||
| 66 | return response()->json($response) |
||
| 67 | ->header('Access-Control-Allow-Origin', '*') |
||
| 68 | ->header('Access-Control-Allow-Methods', 'GET'); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Display a listing of the resource. |
||
| 73 | * |
||
| 74 | * @return \Illuminate\Http\Response |
||
| 75 | */ |
||
| 76 | View Code Duplication | public function get() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Show the form for creating a new resource. |
||
| 90 | * |
||
| 91 | * @return \Illuminate\Http\Response |
||
| 92 | */ |
||
| 93 | public function create() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Store a newly created resource in storage. |
||
| 135 | * |
||
| 136 | * @param \Illuminate\Http\Request $request |
||
| 137 | * @return \Illuminate\Http\Response |
||
| 138 | */ |
||
| 139 | View Code Duplication | public function store(Request $request) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Display the specified resource. |
||
| 150 | * |
||
| 151 | * @param \App\JenisSekolah $jenis_sekolah |
||
| 152 | * @return \Illuminate\Http\Response |
||
| 153 | */ |
||
| 154 | View Code Duplication | public function show($id) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Show the form for editing the specified resource. |
||
| 168 | * |
||
| 169 | * @param \App\JenisSekolah $jenis_sekolah |
||
| 170 | * @return \Illuminate\Http\Response |
||
| 171 | */ |
||
| 172 | View Code Duplication | public function edit($id) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Update the specified resource in storage. |
||
| 186 | * |
||
| 187 | * @param \Illuminate\Http\Request $request |
||
| 188 | * @param \App\JenisSekolah $jenis_sekolah |
||
| 189 | * @return \Illuminate\Http\Response |
||
| 190 | */ |
||
| 191 | View Code Duplication | public function update(Request $request, $id) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Remove the specified resource from storage. |
||
| 202 | * |
||
| 203 | * @param \App\JenisSekolah $jenis_sekolah |
||
| 204 | * @return \Illuminate\Http\Response |
||
| 205 | */ |
||
| 206 | View Code Duplication | public function destroy($id) |
|
| 222 | } |
||
| 223 |
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.