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 |
||
39 | class UserController extends BaseController |
||
40 | { |
||
41 | public function __construct() |
||
42 | { |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * List All Users. |
||
47 | * |
||
48 | * Lists all users currently on the system. |
||
49 | * |
||
50 | * @Get("/users/{?page}") |
||
51 | * @Versions({"v1"}) |
||
52 | * @Parameters({ |
||
53 | * @Parameter("page", type="integer", description="The page of results to view.", default=1) |
||
54 | * }) |
||
55 | * @Response(200) |
||
56 | */ |
||
57 | public function lists(Request $request) |
||
|
|||
58 | { |
||
59 | return Models\User::all()->toArray(); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * List Specific User. |
||
64 | * |
||
65 | * Lists specific fields about a user or all fields pertaining to that user. |
||
66 | * |
||
67 | * @Get("/users/{id}/{fields}") |
||
68 | * @Versions({"v1"}) |
||
69 | * @Parameters({ |
||
70 | * @Parameter("id", type="integer", required=true, description="The ID of the user to get information on."), |
||
71 | * @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.") |
||
72 | * }) |
||
73 | * @Response(200) |
||
74 | */ |
||
75 | public function view(Request $request, $id) |
||
76 | { |
||
77 | $user = Models\User::with('servers')->where((is_numeric($id) ? 'id' : 'email'), $id)->first(); |
||
78 | if (! $user->first()) { |
||
79 | throw new NotFoundHttpException('No user by that ID was found.'); |
||
80 | } |
||
81 | |||
82 | $user->servers->transform(function ($item) { |
||
83 | return collect($item)->only([ |
||
84 | 'id', 'node_id', 'uuidShort', |
||
85 | 'uuid', 'name', 'suspended', |
||
86 | 'owner_id', |
||
87 | ]); |
||
88 | }); |
||
89 | |||
90 | View Code Duplication | if (! is_null($request->input('fields'))) { |
|
91 | $fields = explode(',', $request->input('fields')); |
||
92 | if (! empty($fields) && is_array($fields)) { |
||
93 | return collect($user)->only($fields); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | return $user->toArray(); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Create a New User. |
||
102 | * |
||
103 | * @Post("/users") |
||
104 | * @Versions({"v1"}) |
||
105 | * @Transaction({ |
||
106 | * @Request({ |
||
107 | * "email": "[email protected]", |
||
108 | * "password": "foopassword", |
||
109 | * "admin": false, |
||
110 | * "custom_id": 123 |
||
111 | * }, headers={"Authorization": "Bearer <token>"}), |
||
112 | * @Response(201), |
||
113 | * @Response(422) |
||
114 | * }) |
||
115 | */ |
||
116 | public function create(Request $request) |
||
117 | { |
||
118 | try { |
||
119 | $user = new UserRepository; |
||
120 | $create = $user->create($request->only([ |
||
121 | 'email', 'username', 'name_first', |
||
122 | 'name_last', 'password', |
||
123 | 'root_admin', 'custom_id', |
||
124 | ])); |
||
125 | $create = $user->create($request->input('email'), $request->input('password'), $request->input('admin'), $request->input('custom_id')); |
||
126 | |||
127 | return ['id' => $create]; |
||
128 | } catch (DisplayValidationException $ex) { |
||
129 | throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true)); |
||
130 | } catch (DisplayException $ex) { |
||
131 | throw new ResourceException($ex->getMessage()); |
||
132 | } catch (\Exception $ex) { |
||
133 | throw new ServiceUnavailableHttpException('Unable to create a user on the system due to an error.'); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Update an Existing User. |
||
139 | * |
||
140 | * The data sent in the request will be used to update the existing user on the system. |
||
141 | * |
||
142 | * @Patch("/users/{id}") |
||
143 | * @Versions({"v1"}) |
||
144 | * @Transaction({ |
||
145 | * @Request({ |
||
146 | * "email": "[email protected]" |
||
147 | * }, headers={"Authorization": "Bearer <token>"}), |
||
148 | * @Response(200, body={"email": "[email protected]"}), |
||
149 | * @Response(422) |
||
150 | * }) |
||
151 | * @Parameters({ |
||
152 | * @Parameter("id", type="integer", required=true, description="The ID of the user to modify.") |
||
153 | * }) |
||
154 | */ |
||
155 | View Code Duplication | public function update(Request $request, $id) |
|
156 | { |
||
157 | try { |
||
158 | $user = new UserRepository; |
||
159 | $user->update($id, $request->only([ |
||
160 | 'username', 'email', 'name_first', |
||
161 | 'name_last', 'password', |
||
162 | 'root_admin', 'language', |
||
163 | ])); |
||
164 | |||
165 | return Models\User::findOrFail($id); |
||
166 | } catch (DisplayValidationException $ex) { |
||
167 | throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true)); |
||
168 | } catch (DisplayException $ex) { |
||
169 | throw new ResourceException($ex->getMessage()); |
||
170 | } catch (\Exception $ex) { |
||
171 | throw new ServiceUnavailableHttpException('Unable to update a user on the system due to an error.'); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Delete a User. |
||
177 | * |
||
178 | * @Delete("/users/{id}") |
||
179 | * @Versions({"v1"}) |
||
180 | * @Transaction({ |
||
181 | * @Request(headers={"Authorization": "Bearer <token>"}), |
||
182 | * @Response(204), |
||
183 | * @Response(422) |
||
184 | * }) |
||
185 | * @Parameters({ |
||
186 | * @Parameter("id", type="integer", required=true, description="The ID of the user to delete.") |
||
187 | * }) |
||
188 | */ |
||
189 | View Code Duplication | public function delete(Request $request, $id) |
|
202 | } |
||
203 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.