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 SktmController extends Controller |
||
25 | { |
||
26 | /** |
||
27 | * Create a new controller instance. |
||
28 | * |
||
29 | * @return void |
||
30 | */ |
||
31 | protected $sktm; |
||
32 | protected $master_sktm; |
||
33 | protected $user; |
||
34 | |||
35 | public function __construct(Sktm $sktm, MasterSktm $master_sktm, User $user) |
||
41 | |||
42 | /** |
||
43 | * Display a listing of the resource. |
||
44 | * |
||
45 | * @return \Illuminate\Http\Response |
||
46 | */ |
||
47 | public function index(Request $request) |
||
48 | { |
||
49 | if (request()->has('sort')) { |
||
50 | list($sortCol, $sortDir) = explode('|', request()->sort); |
||
51 | |||
52 | $query = $this->sktm->orderBy($sortCol, $sortDir); |
||
53 | } else { |
||
54 | $query = $this->sktm->orderBy('id', 'asc'); |
||
55 | } |
||
56 | |||
57 | if ($request->exists('filter')) { |
||
58 | $query->where(function($q) use($request) { |
||
59 | $value = "%{$request->filter}%"; |
||
60 | $q->where('nomor_un', 'like', $value) |
||
61 | ->orWhere('nilai', 'like', $value); |
||
62 | }); |
||
63 | } |
||
64 | |||
65 | $perPage = request()->has('per_page') ? (int) request()->per_page : null; |
||
66 | $response = $query->paginate($perPage); |
||
67 | |||
68 | foreach($response as $master_sktm){ |
||
69 | array_set($response->data, 'master_sktm', $master_sktm->master_sktm->nama); |
||
70 | } |
||
71 | |||
72 | foreach($response as $user){ |
||
73 | array_set($response->data, 'user', $user->user->name); |
||
74 | } |
||
75 | |||
76 | return response()->json($response) |
||
77 | ->header('Access-Control-Allow-Origin', '*') |
||
78 | ->header('Access-Control-Allow-Methods', 'GET'); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Show the form for creating a new resource. |
||
83 | * |
||
84 | * @return \Illuminate\Http\Response |
||
85 | */ |
||
86 | |||
87 | public function create() |
||
106 | |||
107 | /** |
||
108 | * Display the specified resource. |
||
109 | * |
||
110 | * @param \App\Sktm $sktm |
||
111 | * @return \Illuminate\Http\Response |
||
112 | */ |
||
113 | public function store(Request $request) |
||
155 | |||
156 | /** |
||
157 | * Store a newly created resource in storage. |
||
158 | * |
||
159 | * @param \Illuminate\Http\Request $request |
||
160 | * @return \Illuminate\Http\Response |
||
161 | */ |
||
162 | View Code Duplication | public function show($id) |
|
173 | |||
174 | /** |
||
175 | * Show the form for editing the specified resource. |
||
176 | * |
||
177 | * @param \App\Sktm $sktm |
||
178 | * @return \Illuminate\Http\Response |
||
179 | */ |
||
180 | |||
181 | public function edit($id) |
||
195 | |||
196 | /** |
||
197 | * Update the specified resource in storage. |
||
198 | * |
||
199 | * @param \Illuminate\Http\Request $request |
||
200 | * @param \App\Sktm $sktm |
||
201 | * @return \Illuminate\Http\Response |
||
202 | */ |
||
203 | public function update(Request $request, $id) |
||
257 | |||
258 | /** |
||
259 | * Remove the specified resource from storage. |
||
260 | * |
||
261 | * @param \App\Sktm $sktm |
||
262 | * @return \Illuminate\Http\Response |
||
263 | */ |
||
264 | View Code Duplication | public function destroy($id) |
|
276 | } |
||
277 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.