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:
Complex classes like UserRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UserRepository, and based on these observations, apply Extract Interface, too.
1 | <?php namespace App\Modules\V1\Acl\Repositories; |
||
6 | class UserRepository extends AbstractRepository |
||
7 | { |
||
8 | /** |
||
9 | * Return the model full namespace. |
||
10 | * |
||
11 | * @return string |
||
12 | */ |
||
13 | protected function getModel() |
||
17 | |||
18 | |||
19 | /** |
||
20 | * Return the logged in user account. |
||
21 | * |
||
22 | * @param array $relations |
||
23 | * @return boolean |
||
24 | */ |
||
25 | public function account($relations = []) |
||
39 | |||
40 | /** |
||
41 | * Check if the logged in user or the given user |
||
42 | * has the given permissions on the given model. |
||
43 | * |
||
44 | * @param string $nameOfPermission |
||
45 | * @param string $model |
||
46 | * @param boolean $user |
||
47 | * @return boolean |
||
48 | */ |
||
49 | public function can($nameOfPermission, $model, $user = false) |
||
60 | |||
61 | /** |
||
62 | * Check if the logged in user has the given group. |
||
63 | * |
||
64 | * @param string $groupName |
||
65 | * @param integer $userId |
||
66 | * @return boolean |
||
67 | */ |
||
68 | public function hasGroup($groupName, $userId = false) |
||
74 | |||
75 | /** |
||
76 | * Assign the given group ids to the given user. |
||
77 | * |
||
78 | * @param integer $user_id |
||
79 | * @param array $group_ids |
||
80 | * @return object |
||
81 | */ |
||
82 | public function assignGroups($user_id, $group_ids) |
||
92 | |||
93 | /** |
||
94 | * Handle a login request to the application. |
||
95 | * |
||
96 | * @param array $credentials |
||
97 | * @param boolean $adminLogin |
||
98 | * @return object |
||
99 | */ |
||
100 | public function login($credentials, $adminLogin = false) |
||
121 | |||
122 | /** |
||
123 | * Handle a social login request of the none admin to the application. |
||
124 | * |
||
125 | * @param array $credentials |
||
126 | * @return array |
||
127 | */ |
||
128 | public function loginSocial($credentials) |
||
154 | |||
155 | /** |
||
156 | * Handle a registration request. |
||
157 | * |
||
158 | * @param array $credentials |
||
159 | * @return array |
||
160 | */ |
||
161 | public function register($credentials) |
||
168 | |||
169 | /** |
||
170 | * Block the user. |
||
171 | * |
||
172 | * @param integer $user_id |
||
173 | * @return object |
||
174 | */ |
||
175 | public function block($user_id) |
||
199 | |||
200 | /** |
||
201 | * Unblock the user. |
||
202 | * |
||
203 | * @param integer $user_id |
||
204 | * @return object |
||
205 | */ |
||
206 | public function unblock($user_id) |
||
219 | |||
220 | /** |
||
221 | * Send a reset link to the given user. |
||
222 | * |
||
223 | * @param string $email |
||
224 | * @return void |
||
225 | */ |
||
226 | public function sendReset($email) |
||
240 | |||
241 | /** |
||
242 | * Reset the given user's password. |
||
243 | * |
||
244 | * @param array $credentials |
||
245 | * @return array |
||
246 | */ |
||
247 | public function resetPassword($credentials) |
||
271 | |||
272 | /** |
||
273 | * Change the logged in user password. |
||
274 | * |
||
275 | * @param array $credentials |
||
276 | * @return void |
||
277 | */ |
||
278 | public function changePassword($credentials) |
||
289 | |||
290 | /** |
||
291 | * Paginate all users in the given group based on the given conditions. |
||
292 | * |
||
293 | * @param string $groupName |
||
294 | * @param array $relations |
||
295 | * @param integer $perPage |
||
296 | * @param string $sortBy |
||
297 | * @param boolean $desc |
||
298 | * @return \Illuminate\Http\Response |
||
299 | */ |
||
300 | public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
||
324 | |||
325 | /** |
||
326 | * Save the given data to the logged in user. |
||
327 | * |
||
328 | * @param array $credentials |
||
329 | * @return object |
||
330 | */ |
||
331 | public function saveProfile($credentials) |
||
338 | |||
339 | /** |
||
340 | * Ensure access token hasn't expired or revoked. |
||
341 | * |
||
342 | * @param string $accessToken |
||
343 | * @return boolean |
||
344 | */ |
||
345 | public function accessTokenExpiredOrRevoked($accessToken) |
||
359 | } |
||
360 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.