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 |
||
| 37 | class UserController extends Controller |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * Controller Constructor. |
||
| 41 | */ |
||
| 42 | public function __construct() |
||
| 46 | |||
| 47 | // @TODO: implement nicolaslopezj/searchable to clean up this disaster. |
||
| 48 | public function getIndex(Request $request) |
||
| 54 | |||
| 55 | public function getNew(Request $request) |
||
| 59 | |||
| 60 | public function getView(Request $request, $id) |
||
| 66 | |||
| 67 | View Code Duplication | public function deleteUser(Request $request, $id) |
|
| 84 | |||
| 85 | public function postNew(Request $request) |
||
| 86 | { |
||
| 87 | try { |
||
| 88 | $user = new UserRepository; |
||
| 89 | $userid = $user->create($request->only([ |
||
| 90 | 'email', 'password', 'name_first', |
||
| 91 | 'name_last', 'username', 'root_admin', |
||
| 92 | ])); |
||
| 93 | Alert::success('Account has been successfully created.')->flash(); |
||
| 94 | |||
| 95 | return redirect()->route('admin.users.view', $userid); |
||
| 96 | } catch (DisplayValidationException $ex) { |
||
| 97 | return redirect()->route('admin.users.new')->withErrors(json_decode($ex->getMessage()))->withInput(); |
||
| 98 | } catch (\Exception $ex) { |
||
| 99 | Log::error($ex); |
||
| 100 | Alert::danger('An error occured while attempting to add a new user.')->flash(); |
||
| 101 | |||
| 102 | return redirect()->route('admin.users.new'); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | public function updateUser(Request $request, $user) |
||
| 107 | { |
||
| 108 | try { |
||
| 109 | $repo = new UserRepository; |
||
| 110 | $repo->update($user, $request->only([ |
||
| 111 | 'email', 'password', 'name_first', |
||
| 112 | 'name_last', 'username', 'root_admin', |
||
| 113 | ])); |
||
| 114 | Alert::success('User account was successfully updated.')->flash(); |
||
| 115 | } catch (DisplayValidationException $ex) { |
||
| 116 | return redirect()->route('admin.users.view', $user)->withErrors(json_decode($ex->getMessage())); |
||
| 117 | } catch (\Exception $e) { |
||
| 118 | Log::error($e); |
||
| 119 | Alert::danger('An error occured while attempting to update this user.')->flash(); |
||
| 120 | } |
||
| 121 | |||
| 122 | return redirect()->route('admin.users.view', $user); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function getJson(Request $request) |
||
| 129 | } |
||
| 130 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.