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 JenisPrestasiController extends Controller |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Create a new controller instance. |
||
| 27 | * |
||
| 28 | * @return void |
||
| 29 | */ |
||
| 30 | protected $jenis_prestasi; |
||
| 31 | protected $user; |
||
| 32 | |||
| 33 | public function __construct(JenisPrestasi $jenis_prestasi, 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->jenis_prestasi->orderBy($sortCol, $sortDir); |
||
| 50 | } else { |
||
| 51 | $query = $this->jenis_prestasi->orderBy('id', 'asc'); |
||
| 52 | } |
||
| 53 | |||
| 54 | if ($request->exists('filter')) { |
||
| 55 | $query->where(function($q) use($request) { |
||
| 56 | $value = "%{$request->filter}%"; |
||
| 57 | $q->where('user_id', 'like', $value) |
||
| 58 | ->orWhere('nama_jenis_prestasi', '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() |
||
| 81 | { |
||
| 82 | $users = $this->user->all(); |
||
| 83 | |||
| 84 | foreach($users as $user){ |
||
| 85 | array_set($user, 'label', $user->name); |
||
| 86 | } |
||
| 87 | |||
| 88 | $response['user'] = $users; |
||
|
|
|||
| 89 | $response['status'] = true; |
||
| 90 | |||
| 91 | return response()->json($response); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Display the specified resource. |
||
| 96 | * |
||
| 97 | * @param \App\Prestasi $prestasi |
||
| 98 | * @return \Illuminate\Http\Response |
||
| 99 | */ |
||
| 100 | public function store(Request $request) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Store a newly created resource in storage. |
||
| 136 | * |
||
| 137 | * @param \Illuminate\Http\Request $request |
||
| 138 | * @return \Illuminate\Http\Response |
||
| 139 | */ |
||
| 140 | public function show($id) |
||
| 141 | { |
||
| 142 | $jenis_prestasi = $this->jenis_prestasi->findOrFail($id); |
||
| 143 | |||
| 144 | $response['user'] = $jenis_prestasi->user; |
||
| 145 | $response['jenis_prestasi'] = $jenis_prestasi; |
||
| 146 | $response['status'] = true; |
||
| 147 | |||
| 148 | return response()->json($response); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Show the form for editing the specified resource. |
||
| 153 | * |
||
| 154 | * @param \App\Prestasi $prestasi |
||
| 155 | * @return \Illuminate\Http\Response |
||
| 156 | */ |
||
| 157 | |||
| 158 | View Code Duplication | public function edit($id) |
|
| 159 | { |
||
| 160 | $jenis_prestasi = $this->jenis_prestasi->findOrFail($id); |
||
| 161 | |||
| 162 | array_set($jenis_prestasi->user, 'label', $jenis_prestasi->user->name); |
||
| 163 | |||
| 164 | $response['jenis_prestasi'] = $jenis_prestasi; |
||
| 165 | $response['user'] = $jenis_prestasi->user; |
||
| 166 | $response['status'] = true; |
||
| 167 | |||
| 168 | return response()->json($response); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Update the specified resource in storage. |
||
| 173 | * |
||
| 174 | * @param \Illuminate\Http\Request $request |
||
| 175 | * @param \App\Prestasi $prestasi |
||
| 176 | * @return \Illuminate\Http\Response |
||
| 177 | */ |
||
| 178 | public function update(Request $request, $id) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Remove the specified resource from storage. |
||
| 222 | * |
||
| 223 | * @param \App\Prestasi $prestasi |
||
| 224 | * @return \Illuminate\Http\Response |
||
| 225 | */ |
||
| 226 | View Code Duplication | public function destroy($id) |
|
| 238 | } |
||
| 239 |
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.