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 |
||
| 23 | class MasterSktmController extends Controller |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Create a new controller instance. |
||
| 27 | * |
||
| 28 | * @return void |
||
| 29 | */ |
||
| 30 | protected $master_sktm; |
||
| 31 | protected $user; |
||
| 32 | |||
| 33 | public function __construct(MasterSktm $master_sktm, User $user) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Display a listing of the resource. |
||
| 41 | * |
||
| 42 | * @return \Illuminate\Http\Response |
||
| 43 | */ |
||
| 44 | public function index(Request $request) |
||
| 45 | { |
||
| 46 | if (request()->has('sort')) { |
||
| 47 | list($sortCol, $sortDir) = explode('|', request()->sort); |
||
| 48 | |||
| 49 | $query = $this->master_sktm->orderBy($sortCol, $sortDir); |
||
| 50 | } else { |
||
| 51 | $query = $this->master_sktm->orderBy('id', 'asc'); |
||
| 52 | } |
||
| 53 | |||
| 54 | if ($request->exists('filter')) { |
||
| 55 | $query->where(function($q) use($request) { |
||
| 56 | $value = "%{$request->filter}%"; |
||
| 57 | $q->where('nama', 'like', $value) |
||
| 58 | ->orWhere('nilai', 'like', $value); |
||
| 59 | }); |
||
| 60 | } |
||
| 61 | |||
| 62 | $perPage = request()->has('per_page') ? (int) request()->per_page : null; |
||
| 63 | $response = $query->paginate($perPage); |
||
| 64 | |||
| 65 | foreach($response as $user){ |
||
| 66 | array_set($response->data, 'user', $user->user->name); |
||
| 67 | } |
||
| 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 | |||
| 80 | public function create() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Display the specified resource. |
||
| 96 | * |
||
| 97 | * @param \App\Sktm $sktm |
||
| 98 | * @return \Illuminate\Http\Response |
||
| 99 | */ |
||
| 100 | public function store(Request $request) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Store a newly created resource in storage. |
||
| 142 | * |
||
| 143 | * @param \Illuminate\Http\Request $request |
||
| 144 | * @return \Illuminate\Http\Response |
||
| 145 | */ |
||
| 146 | public function show($id) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Show the form for editing the specified resource. |
||
| 159 | * |
||
| 160 | * @param \App\Sktm $sktm |
||
| 161 | * @return \Illuminate\Http\Response |
||
| 162 | */ |
||
| 163 | |||
| 164 | View Code Duplication | public function edit($id) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Update the specified resource in storage. |
||
| 179 | * |
||
| 180 | * @param \Illuminate\Http\Request $request |
||
| 181 | * @param \App\Sktm $sktm |
||
| 182 | * @return \Illuminate\Http\Response |
||
| 183 | */ |
||
| 184 | public function update(Request $request, $id) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Remove the specified resource from storage. |
||
| 237 | * |
||
| 238 | * @param \App\Sktm $sktm |
||
| 239 | * @return \Illuminate\Http\Response |
||
| 240 | */ |
||
| 241 | View Code Duplication | public function destroy($id) |
|
| 253 | } |
||
| 254 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.