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 |
||
| 21 | class StaffController extends Controller |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * StaffController constructor. |
||
| 25 | */ |
||
| 26 | public function __construct() |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Display all users. |
||
| 34 | * |
||
| 35 | * @return mixed |
||
| 36 | */ |
||
| 37 | public function index() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Show the form for creating a new employee. |
||
| 46 | * |
||
| 47 | * @return mixed |
||
| 48 | */ |
||
| 49 | public function create() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Store a newly created employee in storage. |
||
| 58 | * |
||
| 59 | * @param Requests\StaffValidator|Request $request |
||
| 60 | * |
||
| 61 | * @return mixed |
||
| 62 | */ |
||
| 63 | public function store(Requests\StaffValidator $request) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Update a user. |
||
| 91 | * |
||
| 92 | * @param Int, $id |
||
| 93 | * @param Request $request |
||
| 94 | * |
||
| 95 | * @return mixed |
||
| 96 | */ |
||
| 97 | public function updateUser($id, Request $request) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return mixed |
||
| 119 | */ |
||
| 120 | public function policies() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return mixed |
||
| 129 | */ |
||
| 130 | public function addpolicies() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param Request $request |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public function addRole(Request $request) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param $id |
||
| 157 | * |
||
| 158 | * @return mixed |
||
| 159 | */ |
||
| 160 | public function editpolicies($id) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Remove the user role. |
||
| 169 | * |
||
| 170 | * @param int, $id |
||
| 171 | * |
||
| 172 | * @return redirect |
||
| 173 | */ |
||
| 174 | public function destroyRole($id) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Show all permission. |
||
| 185 | */ |
||
| 186 | public function permissions() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Show the form to create a new permission. |
||
| 195 | */ |
||
| 196 | public function createPermission() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Show the form to edit a permission. |
||
| 203 | */ |
||
| 204 | public function EditPermission(Request $request, $id) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Save the new permission. |
||
| 212 | * |
||
| 213 | * @param Request $request |
||
| 214 | * |
||
| 215 | * @return redirect |
||
| 216 | */ |
||
| 217 | public function savePermission(Request $request) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Delete a permission out off timecontrol. |
||
| 227 | * |
||
| 228 | * @param int, $id, the id off the permission. |
||
| 229 | * @return Redirect |
||
| 230 | */ |
||
| 231 | public function destroyPermission($id) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Display the specified resource. |
||
| 243 | * |
||
| 244 | * @param int $id |
||
| 245 | * |
||
| 246 | * @return \Illuminate\Http\Response |
||
| 247 | */ |
||
| 248 | public function show($id) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Show the form for editing the specified resource. |
||
| 255 | * |
||
| 256 | * @param $id |
||
| 257 | * |
||
| 258 | * @return |
||
| 259 | */ |
||
| 260 | public function edit($id) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @return mixed |
||
| 273 | */ |
||
| 274 | public function profile() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * change a password. |
||
| 283 | */ |
||
| 284 | public function chPass() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Update the specified resource in storage. |
||
| 291 | * |
||
| 292 | * @param Requests\accountManagementValidator|Request $request |
||
| 293 | * @return \Illuminate\Http\Response |
||
| 294 | */ |
||
| 295 | public function update(Requests\accountManagementValidator $request) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Remove the specified employee from the database. |
||
| 320 | * |
||
| 321 | * @param int $id |
||
| 322 | * |
||
| 323 | * @return \Illuminate\Http\Response |
||
| 324 | */ |
||
| 325 | public function destroy($id) |
||
| 336 | } |
||
| 337 |
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.