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\Users\Repositories; |
||
7 | class UserRepository extends BaseRepository |
||
8 | { |
||
9 | /** |
||
10 | * Init new object. |
||
11 | * |
||
12 | * @param AclUser $model |
||
13 | * @return void |
||
|
|||
14 | */ |
||
15 | public function __construct(AclUser $model) |
||
19 | |||
20 | /** |
||
21 | * Return the logged in user account. |
||
22 | * |
||
23 | * @param array $relations |
||
24 | * @return boolean |
||
25 | */ |
||
26 | 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 mixed $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[] $groups |
||
65 | * @param mixed $user |
||
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 $groupIds |
||
79 | * @return object |
||
80 | */ |
||
81 | View Code Duplication | public function assignGroups($userId, $groupIds) |
|
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) |
||
116 | |||
117 | /** |
||
118 | * Handle a social login request of the none admin to the application. |
||
119 | * |
||
120 | * @param string $authCode |
||
121 | * @param string $accessToken |
||
122 | * @param string $type |
||
123 | * @return array |
||
124 | */ |
||
125 | public function loginSocial($authCode, $accessToken, $type) |
||
141 | |||
142 | /** |
||
143 | * Handle a registration request. |
||
144 | * |
||
145 | * @param array $credentials |
||
146 | * @param boolean $skipConfirmEmail |
||
147 | * @return array |
||
148 | */ |
||
149 | public function register($credentials, $skipConfirmEmail = false) |
||
162 | |||
163 | /** |
||
164 | * Block the user. |
||
165 | * |
||
166 | * @param integer $userId |
||
167 | * @return object |
||
168 | */ |
||
169 | public function block($userId) |
||
187 | |||
188 | /** |
||
189 | * Unblock the user. |
||
190 | * |
||
191 | * @param integer $userId |
||
192 | * @return object |
||
193 | */ |
||
194 | public function unblock($userId) |
||
206 | |||
207 | /** |
||
208 | * Send a reset link to the given user. |
||
209 | * |
||
210 | * @param string $email |
||
211 | * @return void |
||
212 | */ |
||
213 | public function sendReset($email) |
||
222 | |||
223 | /** |
||
224 | * Reset the given user's password. |
||
225 | * |
||
226 | * @param array $credentials |
||
227 | * @return string|null |
||
228 | */ |
||
229 | public function resetPassword($credentials) |
||
256 | |||
257 | /** |
||
258 | * Change the logged in user password. |
||
259 | * |
||
260 | * @param array $credentials |
||
261 | * @return void |
||
262 | */ |
||
263 | public function changePassword($credentials) |
||
273 | |||
274 | /** |
||
275 | * Confirm email using the confirmation code. |
||
276 | * |
||
277 | * @param string $confirmationCode |
||
278 | * @return void |
||
279 | */ |
||
280 | public function confirmEmail($confirmationCode) |
||
290 | |||
291 | /** |
||
292 | * Send the confirmation mail. |
||
293 | * |
||
294 | * @param string $email |
||
295 | * @return void |
||
296 | */ |
||
297 | public function sendConfirmationEmail($email) |
||
309 | |||
310 | /** |
||
311 | * Paginate all users in the given group based on the given conditions. |
||
312 | * |
||
313 | * @param string $groupName |
||
314 | * @param array $relations |
||
315 | * @param integer $perPage |
||
316 | * @param string $sortBy |
||
317 | * @param boolean $desc |
||
318 | * @return \Illuminate\Http\Response |
||
319 | */ |
||
320 | public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
||
342 | |||
343 | /** |
||
344 | * Save the given data to the logged in user. |
||
345 | * |
||
346 | * @param array $data |
||
347 | * @return void |
||
348 | */ |
||
349 | public function saveProfile($data) |
||
358 | |||
359 | /** |
||
360 | * Ensure access token hasn't expired or revoked. |
||
361 | * |
||
362 | * @param string $accessToken |
||
363 | * @return boolean |
||
364 | */ |
||
365 | public function accessTokenExpiredOrRevoked($accessToken) |
||
378 | |||
379 | /** |
||
380 | * Revoke the given access token and all |
||
381 | * associated refresh tokens. |
||
382 | * |
||
383 | * @param oject $accessToken |
||
384 | * @return void |
||
385 | */ |
||
386 | public function revokeAccessToken($accessToken) |
||
396 | } |
||
397 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.