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 |
||
27 | class AkademikController extends Controller |
||
28 | { |
||
29 | protected $akademik; |
||
30 | protected $siswa; |
||
31 | protected $user; |
||
32 | protected $nilai; |
||
33 | |||
34 | /** |
||
35 | * Create a new controller instance. |
||
36 | * |
||
37 | * @return void |
||
|
|||
38 | */ |
||
39 | public function __construct() |
||
46 | |||
47 | /** |
||
48 | * Display a listing of the resource. |
||
49 | * |
||
50 | * @return \Illuminate\Http\Response |
||
51 | */ |
||
52 | public function index(Request $request) |
||
53 | { |
||
54 | if (request()->has('sort')) { |
||
55 | list($sortCol, $sortDir) = explode('|', request()->sort); |
||
56 | |||
57 | $query = $this->akademik->orderBy($sortCol, $sortDir); |
||
58 | } else { |
||
59 | $query = $this->akademik->orderBy('id', 'asc'); |
||
60 | } |
||
61 | |||
62 | if ($request->exists('filter')) { |
||
63 | $query->where(function($q) use($request) { |
||
64 | $value = "%{$request->filter}%"; |
||
65 | |||
66 | $q->where('nomor_un', 'like', $value); |
||
67 | }); |
||
68 | } |
||
69 | |||
70 | $perPage = request()->has('per_page') ? (int) request()->per_page : null; |
||
71 | |||
72 | $response = $query->with(['siswa', 'user'])->paginate($perPage); |
||
73 | |||
74 | return response()->json($response) |
||
75 | ->header('Access-Control-Allow-Origin', '*') |
||
76 | ->header('Access-Control-Allow-Methods', 'GET'); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Display a listing of the resource. |
||
81 | * |
||
82 | * @return \Illuminate\Http\Response |
||
83 | */ |
||
84 | View Code Duplication | public function get() |
|
103 | |||
104 | /** |
||
105 | * Show the form for creating a new resource. |
||
106 | * |
||
107 | * @return \Illuminate\Http\Response |
||
108 | */ |
||
109 | public function create() |
||
110 | { |
||
111 | $user_id = isset(Auth::User()->id) ? Auth::User()->id : null; |
||
112 | $akademik = $this->akademik->getAttributes(); |
||
113 | $siswas = $this->siswa->getAttributes(); |
||
114 | $users = $this->user->getAttributes(); |
||
115 | $users_special = $this->user->all(); |
||
116 | $users_standar = $this->user->findOrFail($user_id); |
||
117 | $current_user = Auth::User(); |
||
118 | |||
119 | foreach ($siswas as $siswa) { |
||
120 | array_set($siswa, 'label', $siswa->nomor_un.' - '.$siswa->nama_siswa); |
||
121 | } |
||
122 | |||
123 | $role_check = Auth::User()->hasRole(['superadministrator','administrator']); |
||
124 | |||
125 | if ($role_check) { |
||
126 | $user_special = true; |
||
127 | |||
128 | foreach($users_special as $user){ |
||
129 | array_set($user, 'label', $user->name); |
||
130 | } |
||
131 | |||
132 | $users = $users_special; |
||
133 | } else { |
||
134 | $user_special = false; |
||
135 | |||
136 | array_set($users_standar, 'label', $users_standar->name); |
||
137 | |||
138 | $users = $users_standar; |
||
139 | } |
||
140 | |||
141 | array_set($current_user, 'label', $current_user->name); |
||
142 | |||
143 | $response['akademik'] = $akademik; |
||
144 | $response['siswas'] = $siswas; |
||
145 | $response['users'] = $users; |
||
146 | $response['user_special'] = $user_special; |
||
147 | $response['current_user'] = $current_user; |
||
148 | $response['error'] = false; |
||
149 | $response['message'] = 'Success'; |
||
150 | $response['status'] = true; |
||
151 | |||
152 | return response()->json($response); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Store a newly created resource in storage. |
||
157 | * |
||
158 | * @param \Illuminate\Http\Request $request |
||
159 | * @return \Illuminate\Http\Response |
||
160 | */ |
||
161 | public function store(Request $request) |
||
221 | |||
222 | /** |
||
223 | * Display the specified resource. |
||
224 | * |
||
225 | * @param \App\Nilai $nilai |
||
226 | * @return \Illuminate\Http\Response |
||
227 | */ |
||
228 | View Code Duplication | public function show($id) |
|
239 | |||
240 | /** |
||
241 | * Show the form for editing the specified resource. |
||
242 | * |
||
243 | * @param \App\Nilai $nilai |
||
244 | * @return \Illuminate\Http\Response |
||
245 | */ |
||
246 | View Code Duplication | public function edit($id) |
|
295 | |||
296 | /** |
||
297 | * Update the specified resource in storage. |
||
298 | * |
||
299 | * @param \Illuminate\Http\Request $request |
||
300 | * @param \App\Nilai $nilai |
||
301 | * @return \Illuminate\Http\Response |
||
302 | */ |
||
303 | public function update(Request $request, $id) |
||
363 | |||
364 | /** |
||
365 | * Remove the specified resource from storage. |
||
366 | * |
||
367 | * @param \App\Nilai $nilai |
||
368 | * @return \Illuminate\Http\Response |
||
369 | */ |
||
370 | View Code Duplication | public function destroy($id) |
|
386 | } |
||
387 |
Adding a
@return
annotation 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.