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 UserService 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 UserService, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace App\Modules\Users\Services; |
||
| 12 | class UserService extends BaseService |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var PermissionService |
||
| 16 | */ |
||
| 17 | protected $permissionService; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var LoginProxy |
||
| 21 | */ |
||
| 22 | protected $loginProxy; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Media |
||
| 26 | */ |
||
| 27 | protected $media; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var NotificationService |
||
| 31 | */ |
||
| 32 | protected $notificationService; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var OauthClientService |
||
| 36 | */ |
||
| 37 | protected $oauthClientService; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Init new object. |
||
| 41 | * |
||
| 42 | * @param UserRepository $repo |
||
| 43 | * @param PermissionService $permissionService |
||
| 44 | * @param LoginProxy $loginProxy |
||
| 45 | * @param Media $media |
||
| 46 | * @param NotificationService $notificationService |
||
| 47 | * @param OauthClientService $oauthClientService |
||
| 48 | * @return void |
||
|
|
|||
| 49 | */ |
||
| 50 | public function __construct( |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Return the logged in user account. |
||
| 68 | * |
||
| 69 | * @param array $relations |
||
| 70 | * @return boolean |
||
| 71 | */ |
||
| 72 | public function account($relations = ['roles.permissions']) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Check if the logged in user or the given user |
||
| 88 | * has the given permissions on the given model. |
||
| 89 | * |
||
| 90 | * @param string $permissionName |
||
| 91 | * @param string $model |
||
| 92 | * @param mixed $userId |
||
| 93 | * @return boolean |
||
| 94 | */ |
||
| 95 | public function can($permissionName, $model, $userId = false) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Check if the logged in or the given user has the given role. |
||
| 120 | * |
||
| 121 | * @param string[] $roles |
||
| 122 | * @param mixed $user |
||
| 123 | * @return boolean |
||
| 124 | */ |
||
| 125 | public function hasRoles($roles, $user = false) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Assign the given role ids to the given user. |
||
| 132 | * |
||
| 133 | * @param integer $userId |
||
| 134 | * @param array $roleIds |
||
| 135 | * @return object |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function assignRoles($userId, $roleIds) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Handle a login request to the application. |
||
| 151 | * |
||
| 152 | * @param string $email |
||
| 153 | * @param string $password |
||
| 154 | * @param boolean $adminLogin |
||
| 155 | * @return object |
||
| 156 | */ |
||
| 157 | public function login($email, $password, $adminLogin = false) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Handle a social login request of the none admin to the application. |
||
| 176 | * |
||
| 177 | * @param string $authCode |
||
| 178 | * @param string $accessToken |
||
| 179 | * @param string $type |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | public function loginSocial($authCode, $accessToken, $type) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Handle a registration request. |
||
| 200 | * |
||
| 201 | * @param string $name |
||
| 202 | * @param string $email |
||
| 203 | * @param string $password |
||
| 204 | * @param boolean $skipConfirmEmail |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | public function register($name, $email, $password, $skipConfirmEmail = false) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Block the user. |
||
| 225 | * |
||
| 226 | * @param integer $userId |
||
| 227 | * @return object |
||
| 228 | */ |
||
| 229 | public function block($userId) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Unblock the user. |
||
| 240 | * |
||
| 241 | * @param integer $userId |
||
| 242 | * @return object |
||
| 243 | */ |
||
| 244 | public function unblock($userId) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Send a reset link to the given user. |
||
| 251 | * |
||
| 252 | * @param string $email |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | public function sendReset($email) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Reset the given user's password. |
||
| 267 | * |
||
| 268 | * @param string $email |
||
| 269 | * @param string $password |
||
| 270 | * @param string $passwordConfirmation |
||
| 271 | * @param string $token |
||
| 272 | * @return string|void |
||
| 273 | */ |
||
| 274 | public function resetPassword($email, $password, $passwordConfirmation, $token) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Change the logged in user password. |
||
| 310 | * |
||
| 311 | * @param string $password |
||
| 312 | * @param string $oldPassword |
||
| 313 | * @return void |
||
| 314 | */ |
||
| 315 | public function changePassword($password, $oldPassword) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Confirm email using the confirmation code. |
||
| 327 | * |
||
| 328 | * @param string $confirmationCode |
||
| 329 | * @return void |
||
| 330 | */ |
||
| 331 | public function confirmEmail($confirmationCode) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Send the confirmation mail. |
||
| 342 | * |
||
| 343 | * @param string $email |
||
| 344 | * @return void |
||
| 345 | */ |
||
| 346 | public function sendConfirmationEmail($email) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Save the given data to the logged in user. |
||
| 359 | * |
||
| 360 | * @param string $name |
||
| 361 | * @param string $email |
||
| 362 | * @param string $profilePicture |
||
| 363 | * @return void |
||
| 364 | */ |
||
| 365 | public function saveProfile($name, $email, $profilePicture = false) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Logs out the user, revoke access token and refresh token. |
||
| 382 | * |
||
| 383 | * @return void |
||
| 384 | */ |
||
| 385 | public function logout() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Attempt to refresh the access token using the given refresh token. |
||
| 392 | * |
||
| 393 | * @param string $refreshToken |
||
| 394 | * @return array |
||
| 395 | */ |
||
| 396 | public function refreshToken($refreshToken) |
||
| 400 | } |
||
| 401 |
Adding a
@returnannotation 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.