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\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($groups, $user = false) |
||
73 | |||
74 | /** |
||
75 | * Assign the given group ids to the given user. |
||
76 | * |
||
77 | * @param integer $userId |
||
78 | * @param array $group_ids |
||
79 | * @return object |
||
80 | */ |
||
81 | View Code Duplication | public function assignGroups($userId, $group_ids) |
|
91 | |||
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) |
||
125 | |||
126 | /** |
||
127 | * Handle a social login request of the none admin to the application. |
||
128 | * |
||
129 | * @param string $authCode |
||
130 | * @param string $accessToken |
||
131 | * @param string $type |
||
132 | * @return array |
||
133 | */ |
||
134 | public function loginSocial($authCode, $accessToken, $type) |
||
152 | |||
153 | /** |
||
154 | * Handle a registration request. |
||
155 | * |
||
156 | * @param array $credentials |
||
157 | * @param boolean $skipConfirmEmail |
||
158 | * @return array |
||
159 | */ |
||
160 | public function register($credentials, $skipConfirmEmail = false) |
||
176 | |||
177 | /** |
||
178 | * Block the user. |
||
179 | * |
||
180 | * @param integer $userId |
||
181 | * @return object |
||
182 | */ |
||
183 | public function block($userId) |
||
207 | |||
208 | /** |
||
209 | * Unblock the user. |
||
210 | * |
||
211 | * @param integer $userId |
||
212 | * @return object |
||
213 | */ |
||
214 | public function unblock($userId) |
||
227 | |||
228 | /** |
||
229 | * Send a reset link to the given user. |
||
230 | * |
||
231 | * @param string $email |
||
232 | * @return void |
||
233 | */ |
||
234 | public function sendReset($email) |
||
244 | |||
245 | /** |
||
246 | * Reset the given user's password. |
||
247 | * |
||
248 | * @param array $credentials |
||
249 | * @return array |
||
250 | */ |
||
251 | public function resetPassword($credentials) |
||
275 | |||
276 | /** |
||
277 | * Change the logged in user password. |
||
278 | * |
||
279 | * @param array $credentials |
||
280 | * @return void |
||
281 | */ |
||
282 | public function changePassword($credentials) |
||
293 | |||
294 | /** |
||
295 | * Confirm email using the confirmation code. |
||
296 | * |
||
297 | * @param string $confirmationCode |
||
298 | * @return void |
||
299 | */ |
||
300 | public function confirmEmail($confirmationCode) |
||
307 | |||
308 | /** |
||
309 | * Send the confirmation mail. |
||
310 | * |
||
311 | * @param string $email |
||
312 | * @return void |
||
313 | */ |
||
314 | public function sendConfirmationEmail($email) |
||
327 | |||
328 | /** |
||
329 | * Paginate all users in the given group based on the given conditions. |
||
330 | * |
||
331 | * @param string $groupName |
||
332 | * @param array $relations |
||
333 | * @param integer $perPage |
||
334 | * @param string $sortBy |
||
335 | * @param boolean $desc |
||
336 | * @return \Illuminate\Http\Response |
||
337 | */ |
||
338 | public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
||
362 | |||
363 | /** |
||
364 | * Save the given data to the logged in user. |
||
365 | * |
||
366 | * @param array $credentials |
||
367 | * @return void |
||
368 | */ |
||
369 | public function saveProfile($data) |
||
379 | |||
380 | /** |
||
381 | * Ensure access token hasn't expired or revoked. |
||
382 | * |
||
383 | * @param string $accessToken |
||
384 | * @return boolean |
||
385 | */ |
||
386 | public function accessTokenExpiredOrRevoked($accessToken) |
||
400 | |||
401 | /** |
||
402 | * Revoke the given access token and all |
||
403 | * associated refresh tokens. |
||
404 | * |
||
405 | * @param string $accessToken |
||
406 | * @return void |
||
407 | */ |
||
408 | public function revokeAccessToken($accessToken) |
||
418 | } |
||
419 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.