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 |
||
| 16 | class UserController extends Controller { |
||
| 17 | |||
| 18 | public function __construct(PasswordBroker $passwords) |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Display a listing of the Users. |
||
| 28 | * |
||
| 29 | * @return Response |
||
| 30 | */ |
||
| 31 | public function index() |
||
| 36 | |||
| 37 | public function new_user() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Show the form for creating a new resource. |
||
| 45 | * |
||
| 46 | * @return Response |
||
| 47 | */ |
||
| 48 | public function create(Requests\CreateRequest $request) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Show the form for editing the specified resource. |
||
| 72 | * |
||
| 73 | * @param int $id |
||
| 74 | * @return Response |
||
| 75 | */ |
||
| 76 | public function edit($id) |
||
| 77 | { |
||
| 78 | $roles = Role::lists('name', 'id'); |
||
| 79 | $user = User::findOrFail($id); |
||
| 80 | |||
| 81 | return view('admin.users.edit', ['roles' => $roles, 'user' => $user]); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function update(Requests\UpdateRequest $request, $id) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Remove the specified resource from storage. |
||
| 97 | * |
||
| 98 | * @param int $id |
||
| 99 | * @return Response |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function delete($id) |
|
| 109 | |||
| 110 | } |
||
| 111 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: