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 AdminSekolahController extends Controller |
||
| 26 | { |
||
| 27 | protected $admin_sekolah; |
||
| 28 | protected $sekolah; |
||
| 29 | protected $user; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Create a new controller instance. |
||
| 33 | * |
||
| 34 | * @return void |
||
|
|
|||
| 35 | */ |
||
| 36 | public function __construct() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Display a listing of the resource. |
||
| 45 | * |
||
| 46 | * @return \Illuminate\Http\Response |
||
| 47 | */ |
||
| 48 | public function index(Request $request) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Display a listing of the resource. |
||
| 105 | * |
||
| 106 | * @return \Illuminate\Http\Response |
||
| 107 | */ |
||
| 108 | View Code Duplication | public function get() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Display a listing of the resource. |
||
| 122 | * |
||
| 123 | * @return \Illuminate\Http\Response |
||
| 124 | */ |
||
| 125 | View Code Duplication | public function getBySekolah($id) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Show the form for creating a new resource. |
||
| 139 | * |
||
| 140 | * @return \Illuminate\Http\Response |
||
| 141 | */ |
||
| 142 | public function create() |
||
| 143 | { |
||
| 144 | $user_id = isset(Auth::User()->id) ? Auth::User()->id : null; |
||
| 145 | $admin_sekolah = $this->admin_sekolah->getAttributes(); |
||
| 146 | $users = $this->user->getAttributes(); |
||
| 147 | $users_special = $this->user->all(); |
||
| 148 | $users_standar = $this->user->findOrFail($user_id); |
||
| 149 | $current_user = Auth::User(); |
||
| 150 | $admins = []; |
||
| 151 | |||
| 152 | foreach($users_special as $user){ |
||
| 153 | if($user->hasRole(['superadministrator','admin_sekolah'])){ |
||
| 154 | array_set($user, 'label', $user->name); |
||
| 155 | array_push($admins, $user); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | array_set($current_user, 'label', $current_user->name); |
||
| 160 | |||
| 161 | $response['admin_sekolah'] = $admin_sekolah; |
||
| 162 | $response['users'] = $admins; |
||
| 163 | $response['user_special'] = true; |
||
| 164 | $response['current_user'] = $current_user; |
||
| 165 | $response['error'] = false; |
||
| 166 | $response['message'] = 'Success'; |
||
| 167 | $response['status'] = true; |
||
| 168 | |||
| 169 | return response()->json($response); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Store a newly created resource in storage. |
||
| 174 | * |
||
| 175 | * @param \Illuminate\Http\Request $request |
||
| 176 | * @return \Illuminate\Http\Response |
||
| 177 | */ |
||
| 178 | public function store(Request $request) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Display the specified resource. |
||
| 220 | * |
||
| 221 | * @param \App\ProdiSekolah $prodi_sekolah |
||
| 222 | * @return \Illuminate\Http\Response |
||
| 223 | */ |
||
| 224 | public function show($id) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Show the form for editing the specified resource. |
||
| 243 | * |
||
| 244 | * @param \App\Sekolah $sekolah |
||
| 245 | * @return \Illuminate\Http\Response |
||
| 246 | */ |
||
| 247 | public function edit($id) |
||
| 248 | { |
||
| 249 | $user_id = isset(Auth::User()->id) ? Auth::User()->id : null; |
||
| 250 | |||
| 251 | View Code Duplication | if($this->checkRole(['superadministrator'])){ |
|
| 252 | $admin_sekolah = $this->admin_sekolah->with(['sekolah', 'admin_sekolah', 'user'])->findOrFail($id); |
||
| 253 | }else{ |
||
| 254 | $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->with(['sekolah', 'admin_sekolah', 'user'])->findOrFail($id); |
||
| 255 | } |
||
| 256 | |||
| 257 | |||
| 258 | $users = $this->user->getAttributes(); |
||
| 259 | $users_special = $this->user->all(); |
||
| 260 | $users_standar = $this->user->findOrFail($user_id); |
||
| 261 | $current_user = Auth::User(); |
||
| 262 | $admins = []; |
||
| 263 | |||
| 264 | |||
| 265 | $role_check = Auth::User()->hasRole(['superadministrator','administrator']); |
||
| 266 | |||
| 267 | if ($admin_sekolah->user !== null) { |
||
| 268 | array_set($admin_sekolah->user, 'label', $admin_sekolah->user->name); |
||
| 269 | } |
||
| 270 | |||
| 271 | if ($admin_sekolah->admin_sekolah !== null) { |
||
| 272 | array_set($admin_sekolah->admin_sekolah, 'label', $admin_sekolah->admin_sekolah->name); |
||
| 273 | } |
||
| 274 | |||
| 275 | foreach($users_special as $user){ |
||
| 276 | if($user->hasRole(['superadministrator','admin_sekolah'])){ |
||
| 277 | array_set($user, 'label', $user->name); |
||
| 278 | array_push($admins, $user); |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | array_set($current_user, 'label', $current_user->name); |
||
| 283 | |||
| 284 | $response['admin_sekolah'] = $admin_sekolah; |
||
| 285 | $response['users'] = $admins; |
||
| 286 | $response['user_special'] = true; |
||
| 287 | $response['current_user'] = $current_user; |
||
| 288 | $response['error'] = false; |
||
| 289 | $response['message'] = 'Success'; |
||
| 290 | $response['status'] = true; |
||
| 291 | |||
| 292 | return response()->json($response); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Update the specified resource in storage. |
||
| 297 | * |
||
| 298 | * @param \Illuminate\Http\Request $request |
||
| 299 | * @param \App\Sekolah $sekolah |
||
| 300 | * @return \Illuminate\Http\Response |
||
| 301 | */ |
||
| 302 | public function update(Request $request, $id) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Remove the specified resource from storage. |
||
| 343 | * |
||
| 344 | * @param \App\Sekolah $sekolah |
||
| 345 | * @return \Illuminate\Http\Response |
||
| 346 | */ |
||
| 347 | public function destroy($id) |
||
| 370 | |||
| 371 | protected function checkRole($role = array()) |
||
| 375 | } |
||
| 376 |
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.