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 $user_id |
||
78 | * @param array $group_ids |
||
79 | * @return object |
||
80 | */ |
||
81 | View Code Duplication | public function assignGroups($user_id, $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) |
||
101 | { |
||
102 | if ( ! $user = $this->first(['email' => $credentials['email']])) |
||
103 | { |
||
104 | \ErrorHandler::loginFailed(); |
||
105 | } |
||
106 | else if ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) |
||
107 | { |
||
108 | \ErrorHandler::loginFailed(); |
||
109 | } |
||
110 | else if ( ! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) |
||
111 | { |
||
112 | \ErrorHandler::loginFailed(); |
||
113 | } |
||
114 | else if ($user->blocked) |
||
115 | { |
||
116 | \ErrorHandler::userIsBlocked(); |
||
117 | } |
||
118 | else if ( ! config('skeleton.disable_confirm_email') && ! $user->confirmed) |
||
119 | { |
||
120 | \ErrorHandler::emailNotConfirmed(); |
||
121 | } |
||
122 | |||
123 | return $user; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Handle a social login request of the none admin to the application. |
||
128 | * |
||
129 | * @param array $credentials |
||
130 | * @return array |
||
131 | */ |
||
132 | public function loginSocial($credentials) |
||
158 | |||
159 | /** |
||
160 | * Handle a registration request. |
||
161 | * |
||
162 | * @param array $credentials |
||
163 | * @return array |
||
164 | */ |
||
165 | public function register($credentials) |
||
174 | |||
175 | /** |
||
176 | * Block the user. |
||
177 | * |
||
178 | * @param integer $user_id |
||
179 | * @return object |
||
180 | */ |
||
181 | public function block($user_id) |
||
205 | |||
206 | /** |
||
207 | * Unblock the user. |
||
208 | * |
||
209 | * @param integer $user_id |
||
210 | * @return object |
||
211 | */ |
||
212 | public function unblock($user_id) |
||
225 | |||
226 | /** |
||
227 | * Send a reset link to the given user. |
||
228 | * |
||
229 | * @param string $email |
||
230 | * @return void |
||
231 | */ |
||
232 | public function sendReset($email) |
||
242 | |||
243 | /** |
||
244 | * Reset the given user's password. |
||
245 | * |
||
246 | * @param array $credentials |
||
247 | * @return array |
||
248 | */ |
||
249 | public function resetPassword($credentials) |
||
273 | |||
274 | /** |
||
275 | * Change the logged in user password. |
||
276 | * |
||
277 | * @param array $credentials |
||
278 | * @return void |
||
279 | */ |
||
280 | public function changePassword($credentials) |
||
291 | |||
292 | /** |
||
293 | * Confirm email using the confirmation code. |
||
294 | * |
||
295 | * @param string $confirmationCode |
||
296 | * @return void |
||
297 | */ |
||
298 | public function confirmEmail($confirmationCode) |
||
305 | |||
306 | /** |
||
307 | * Send the confirmation mail. |
||
308 | * |
||
309 | * @param string $email |
||
310 | * @return void |
||
311 | */ |
||
312 | public function sendConfirmationEmail($email) |
||
325 | |||
326 | /** |
||
327 | * Paginate all users in the given group based on the given conditions. |
||
328 | * |
||
329 | * @param string $groupName |
||
330 | * @param array $relations |
||
331 | * @param integer $perPage |
||
332 | * @param string $sortBy |
||
333 | * @param boolean $desc |
||
334 | * @return \Illuminate\Http\Response |
||
335 | */ |
||
336 | public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
||
360 | |||
361 | /** |
||
362 | * Save the given data to the logged in user. |
||
363 | * |
||
364 | * @param array $credentials |
||
365 | * @return void |
||
366 | */ |
||
367 | public function saveProfile($data) |
||
377 | |||
378 | /** |
||
379 | * Ensure access token hasn't expired or revoked. |
||
380 | * |
||
381 | * @param string $accessToken |
||
382 | * @return boolean |
||
383 | */ |
||
384 | public function accessTokenExpiredOrRevoked($accessToken) |
||
398 | |||
399 | /** |
||
400 | * Revoke the given access token and all |
||
401 | * associated refresh tokens. |
||
402 | * |
||
403 | * @param string $accessToken |
||
404 | * @return void |
||
405 | */ |
||
406 | public function revokeAccessToken($accessToken) |
||
416 | } |
||
417 |
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.