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 |
||
24 | class OrangTuaController extends Controller |
||
25 | { |
||
26 | /** |
||
27 | * Create a new controller instance. |
||
28 | * |
||
29 | * @return void |
||
30 | */ |
||
31 | |||
32 | protected $orang_tua; |
||
33 | protected $siswa; |
||
34 | protected $user; |
||
35 | |||
36 | public function __construct(OrangTua $orang_tua, User $user, Siswa $siswa) |
||
37 | { |
||
38 | $this->orang_tua = $orang_tua; |
||
39 | $this->siswa = $siswa; |
||
40 | $this->user = $user; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Display a listing of the resource. |
||
45 | * |
||
46 | * @return \Illuminate\Http\Response |
||
47 | */ |
||
48 | public function index(Request $request) |
||
49 | { |
||
50 | if ($request->has('sort')) { |
||
51 | list($sortCol, $sortDir) = explode('|', $request->sort); |
||
52 | |||
53 | $query = $this->orang_tua->orderBy($sortCol, $sortDir); |
||
54 | } else { |
||
55 | $query = $this->orang_tua->orderBy('id', 'asc'); |
||
56 | } |
||
57 | |||
58 | if ($request->exists('filter')) { |
||
59 | $query->where(function($q) use($request) { |
||
60 | $value = "%{$request->filter}%"; |
||
61 | $q->where('id', 'like', $value) |
||
62 | ->orWhere('nama_ayah', 'like', $value); |
||
63 | }); |
||
64 | } |
||
65 | |||
66 | $perPage = request()->has('per_page') ? (int) request()->per_page : null; |
||
67 | $response = $query->with('user')->with('siswa')->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 | * Show the form for creating a new resource. |
||
76 | * |
||
77 | * @return \Illuminate\Http\Response |
||
78 | */ |
||
79 | public function create() |
||
80 | { |
||
81 | $response = []; |
||
82 | $siswas = $this->siswa->all(); |
||
83 | $users_special = $this->user->all(); |
||
84 | $users_standar = $this->user->find(\Auth::User()->id); |
||
85 | $current_user = \Auth::User(); |
||
86 | |||
87 | $role_check = \Auth::User()->hasRole(['superadministrator','administrator']); |
||
88 | |||
89 | if($role_check){ |
||
90 | $response['user_special'] = true; |
||
91 | foreach($users_special as $user){ |
||
92 | array_set($user, 'label', $user->name); |
||
93 | } |
||
94 | $response['user'] = $users_special; |
||
95 | }else{ |
||
96 | $response['user_special'] = false; |
||
97 | array_set($users_standar, 'label', $users_standar->name); |
||
98 | $response['user'] = $users_standar; |
||
99 | } |
||
100 | |||
101 | array_set($current_user, 'label', $current_user->name); |
||
102 | |||
103 | foreach($siswas as $siswa){ |
||
104 | array_set($siswa, 'label', $siswa->nama_siswa); |
||
105 | } |
||
106 | |||
107 | $response['current_user'] = $current_user; |
||
108 | $response['siswa'] = $siswas; |
||
109 | $response['status'] = true; |
||
110 | |||
111 | return response()->json($response); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Display the specified resource. |
||
116 | * |
||
117 | * @param \App\OrangTua $orang_tua |
||
118 | * @return \Illuminate\Http\Response |
||
119 | */ |
||
120 | public function store(Request $request) |
||
121 | { |
||
122 | $orang_tua = $this->orang_tua; |
||
123 | |||
124 | $validator = Validator::make($request->all(), [ |
||
125 | 'user_id' => 'required|unique:orangtuas,user_id', |
||
126 | 'nomor_un' => 'required|unique:orangtuas,nomor_un', |
||
127 | 'no_telp' => 'required|unique:orangtuas,no_telp', |
||
128 | 'nama_ayah' => 'required', |
||
129 | 'nama_ibu' => 'required', |
||
130 | 'pendidikan_ayah' => 'required', |
||
131 | 'kerja_ayah' => 'required', |
||
132 | 'pendidikan_ibu' => 'required', |
||
133 | 'kerja_ibu' => 'required', |
||
134 | 'alamat_ortu' => 'required', |
||
135 | ]); |
||
136 | |||
137 | if($validator->fails()){ |
||
138 | $check = $orang_tua->where('user_id',$request->user_id)->orWhere('nomor_un',$request->nomor_un)->orWhere('no_telp',$request->no_telp)->whereNull('deleted_at')->count(); |
||
139 | |||
140 | if ($check > 0) { |
||
141 | $response['message'] = 'Failed ! Username, Nama Siswa, Nomor Telp already exists'; |
||
142 | } else { |
||
143 | $orang_tua->user_id = $request->input('user_id'); |
||
144 | $orang_tua->nomor_un = $request->input('nomor_un'); |
||
145 | $orang_tua->no_telp = $request->input('no_telp'); |
||
146 | $orang_tua->nama_ayah = $request->input('nama_ayah'); |
||
147 | $orang_tua->nama_ibu = $request->input('nama_ibu'); |
||
148 | $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah'); |
||
149 | $orang_tua->kerja_ayah = $request->input('kerja_ayah'); |
||
150 | $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu'); |
||
151 | $orang_tua->kerja_ibu = $request->input('kerja_ibu'); |
||
152 | $orang_tua->alamat_ortu = $request->input('alamat_ortu'); |
||
153 | $orang_tua->save(); |
||
154 | |||
155 | $response['message'] = 'success'; |
||
156 | } |
||
157 | |||
158 | } else { |
||
159 | $orang_tua->user_id = $request->input('user_id'); |
||
160 | $orang_tua->nomor_un = $request->input('nomor_un'); |
||
161 | $orang_tua->no_telp = $request->input('no_telp'); |
||
162 | $orang_tua->nama_ayah = $request->input('nama_ayah'); |
||
163 | $orang_tua->nama_ibu = $request->input('nama_ibu'); |
||
164 | $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah'); |
||
165 | $orang_tua->kerja_ayah = $request->input('kerja_ayah'); |
||
166 | $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu'); |
||
167 | $orang_tua->kerja_ibu = $request->input('kerja_ibu'); |
||
168 | $orang_tua->alamat_ortu = $request->input('alamat_ortu'); |
||
169 | $orang_tua->save(); |
||
170 | $response['message'] = 'success'; |
||
171 | |||
172 | $response['status'] = true; |
||
173 | |||
174 | return response()->json($response); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Store a newly created resource in storage. |
||
179 | * |
||
180 | * @param \Illuminate\Http\Request $request |
||
181 | * @return \Illuminate\Http\Response |
||
182 | */ |
||
183 | public function show($id) |
||
184 | { |
||
185 | $orang_tua = $this->orang_tua->findOrFail($id); |
||
186 | |||
187 | $response['user'] = $orang_tua->user; |
||
188 | $response['siswa'] = $orang_tua->siswa; |
||
189 | $response['orang_tua'] = $orang_tua; |
||
190 | $response['status'] = true; |
||
191 | |||
192 | return response()->json($response); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Show the form for editing the specified resource. |
||
197 | * |
||
198 | * @param \App\OrangTua $orang_tua |
||
199 | * @return \Illuminate\Http\Response |
||
200 | */ |
||
201 | public function edit($id) |
||
202 | { |
||
203 | $orang_tua = $this->orang_tua->findOrFail($id); |
||
204 | |||
205 | array_set($orang_tua->user, 'label', $orang_tua->user->name); |
||
206 | array_set($orang_tua->siswa, 'label', $orang_tua->siswa->nama_siswa); |
||
207 | |||
208 | $response['user'] = $orang_tua->user; |
||
209 | $response['siswa'] = $orang_tua->siswa; |
||
210 | $response['orang_tua'] = $orang_tua; |
||
211 | $response['status'] = true; |
||
212 | |||
213 | return response()->json($response); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Update the specified resource in storage. |
||
218 | * |
||
219 | * @param \Illuminate\Http\Request $request |
||
220 | * @param \App\OrangTua $orang_tua |
||
221 | * @return \Illuminate\Http\Response |
||
222 | */ |
||
223 | public function update(Request $request, $id) |
||
224 | { |
||
225 | $response = array(); |
||
226 | $message = array(); |
||
227 | $orang_tua = $this->orang_tua->findOrFail($id); |
||
228 | |||
229 | $validator = Validator::make($request->all(), [ |
||
230 | 'user_id' => 'required|unique:orangtuas,user_id,'.$id, |
||
231 | 'nomor_un' => 'required|unique:orangtuas,nomor_un,'.$id, |
||
232 | 'no_telp' => 'required|unique:orangtuas,no_telp,'.$id, |
||
233 | 'nama_ayah' => 'required', |
||
234 | 'nama_ibu' => 'required', |
||
235 | 'pendidikan_ayah' => 'required', |
||
236 | 'kerja_ayah' => 'required', |
||
237 | 'pendidikan_ibu' => 'required', |
||
238 | 'kerja_ibu' => 'required', |
||
239 | 'alamat_ortu' => 'required', |
||
240 | ]); |
||
241 | |||
242 | if ($validator->fails()) { |
||
243 | |||
244 | foreach($validator->messages()->getMessages() as $key => $error){ |
||
245 | foreach($error AS $error_get) { |
||
246 | array_push($message, $error_get); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | $check_user = $this->orang_tua->where('id','!=', $id)->where('user_id', $request->user_id); |
||
251 | $check_siswa = $this->orang_tua->where('id','!=', $id)->where('nomor_un', $request->nomor_un); |
||
252 | $check_no_telp = $this->orang_tua->where('id','!=', $id)->where('no_telp', $request->no_telp); |
||
253 | |||
254 | if($check_user->count() > 0 || $check_siswa->count() > 0 || $check_no_telp->count() > 0){ |
||
255 | $response['message'] = implode("\n",$message); |
||
256 | } else { |
||
257 | $orang_tua->user_id = $request->input('user_id'); |
||
258 | $orang_tua->nomor_un = $request->input('nomor_un'); |
||
259 | $orang_tua->no_telp = $request->input('no_telp'); |
||
260 | $orang_tua->nama_ayah = $request->input('nama_ayah'); |
||
261 | $orang_tua->nama_ibu = $request->input('nama_ibu'); |
||
262 | $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah'); |
||
263 | $orang_tua->kerja_ayah = $request->input('kerja_ayah'); |
||
264 | $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu'); |
||
265 | $orang_tua->kerja_ibu = $request->input('kerja_ibu'); |
||
266 | $orang_tua->alamat_ortu = $request->input('alamat_ortu'); |
||
267 | $orang_tua->save(); |
||
268 | |||
269 | $response['message'] = 'success'; |
||
270 | |||
271 | } |
||
272 | } else { |
||
273 | $orang_tua->user_id = $request->input('user_id'); |
||
274 | $orang_tua->nomor_un = $request->input('nomor_un'); |
||
275 | $orang_tua->no_telp = $request->input('no_telp'); |
||
276 | $orang_tua->nama_ayah = $request->input('nama_ayah'); |
||
277 | $orang_tua->nama_ibu = $request->input('nama_ibu'); |
||
278 | $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah'); |
||
279 | $orang_tua->kerja_ayah = $request->input('kerja_ayah'); |
||
280 | $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu'); |
||
281 | $orang_tua->kerja_ibu = $request->input('kerja_ibu'); |
||
282 | $orang_tua->alamat_ortu = $request->input('alamat_ortu'); |
||
283 | $orang_tua->save(); |
||
284 | |||
285 | $response['message'] = 'success'; |
||
286 | } |
||
287 | |||
288 | $response['status'] = true; |
||
289 | |||
290 | return response()->json($response); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Remove the specified resource from storage. |
||
295 | * |
||
296 | * @param \App\OrangTua $orang_tua |
||
297 | * @return \Illuminate\Http\Response |
||
298 | */ |
||
299 | public function destroy($id) |
||
300 | { |
||
301 | $orang_tua = $this->orang_tua->findOrFail($id); |
||
302 | |||
303 | if ($orang_tua->delete()) { |
||
304 | $response['status'] = true; |
||
305 | } else { |
||
306 | $response['status'] = false; |
||
307 | } |
||
308 | |||
309 | return json_encode($response); |
||
310 | } |
||
311 | } |
||
312 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.