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 MasterSktmController extends Controller |
||
| 26 | { |
||
| 27 | protected $master_sktm; |
||
| 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_sktm->orderBy($sortCol, $sortDir); |
||
| 52 | } else { |
||
| 53 | $query = $this->master_sktm->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('nama', 'like', $value) |
||
| 61 | ->orWhere('instansi', 'like', $value); |
||
| 62 | }); |
||
| 63 | } |
||
| 64 | |||
| 65 | $perPage = request()->has('per_page') ? (int) request()->per_page : null; |
||
| 66 | |||
| 67 | $response = $query->with(['user'])->paginate($perPage); |
||
| 68 | |||
| 69 | return response()->json($response) |
||
| 70 | ->header('Access-Control-Allow-Origin', '*') |
||
| 71 | ->header('Access-Control-Allow-Methods', 'GET'); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Display a listing of the resource. |
||
| 76 | * |
||
| 77 | * @return \Illuminate\Http\Response |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function get() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Show the form for creating a new resource. |
||
| 93 | * |
||
| 94 | * @return \Illuminate\Http\Response |
||
| 95 | */ |
||
| 96 | public function create() |
||
| 97 | { |
||
| 98 | $user_id = isset(Auth::User()->id) ? Auth::User()->id : null; |
||
| 99 | $master_sktm = $this->master_sktm->getAttributes(); |
||
| 100 | $users = $this->user->getAttributes(); |
||
| 101 | $users_special = $this->user->all(); |
||
| 102 | $users_standar = $this->user->findOrFail($user_id); |
||
| 103 | $current_user = Auth::User(); |
||
| 104 | |||
| 105 | $role_check = Auth::User()->hasRole(['superadministrator','administrator']); |
||
| 106 | |||
| 107 | if ($role_check) { |
||
| 108 | $user_special = true; |
||
| 109 | |||
| 110 | foreach ($users_special as $user) { |
||
| 111 | array_set($user, 'label', $user->name); |
||
| 112 | } |
||
| 113 | |||
| 114 | $users = $users_special; |
||
| 115 | } else { |
||
| 116 | $user_special = false; |
||
| 117 | |||
| 118 | array_set($users_standar, 'label', $users_standar->name); |
||
| 119 | |||
| 120 | $users = $users_standar; |
||
| 121 | } |
||
| 122 | |||
| 123 | array_set($current_user, 'label', $current_user->name); |
||
| 124 | |||
| 125 | $response['master_sktm'] = $master_sktm; |
||
| 126 | $response['users'] = $users; |
||
| 127 | $response['user_special'] = $user_special; |
||
| 128 | $response['current_user'] = $current_user; |
||
| 129 | $response['error'] = false; |
||
| 130 | $response['message'] = 'Success'; |
||
| 131 | $response['status'] = true; |
||
| 132 | |||
| 133 | return response()->json($response); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Store a newly created resource in storage. |
||
| 138 | * |
||
| 139 | * @param \Illuminate\Http\Request $request |
||
| 140 | * @return \Illuminate\Http\Response |
||
| 141 | */ |
||
| 142 | View Code Duplication | public function store(Request $request) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Display the specified resource. |
||
| 177 | * |
||
| 178 | * @param \App\MasterSktm $master_sktm |
||
| 179 | * @return \Illuminate\Http\Response |
||
| 180 | */ |
||
| 181 | View Code Duplication | public function show($id) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Show the form for editing the specified resource. |
||
| 195 | * |
||
| 196 | * @param \App\MasterSktm $master_sktm |
||
| 197 | * @return \Illuminate\Http\Response |
||
| 198 | */ |
||
| 199 | View Code Duplication | public function edit($id) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Update the specified resource in storage. |
||
| 245 | * |
||
| 246 | * @param \Illuminate\Http\Request $request |
||
| 247 | * @param \App\MasterSktm $master_sktm |
||
| 248 | * @return \Illuminate\Http\Response |
||
| 249 | */ |
||
| 250 | View Code Duplication | public function update(Request $request, $id) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Remove the specified resource from storage. |
||
| 284 | * |
||
| 285 | * @param \App\MasterSktm $master_sktm |
||
| 286 | * @return \Illuminate\Http\Response |
||
| 287 | */ |
||
| 288 | public function destroy($id) |
||
| 304 | } |
||
| 305 |
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.