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 |
||
| 13 | class UserService extends BaseService |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var PermissionService |
||
| 17 | */ |
||
| 18 | protected $permissionService; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var LoginProxy |
||
| 22 | */ |
||
| 23 | protected $loginProxy; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var NotificationService |
||
| 27 | */ |
||
| 28 | protected $notificationService; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var OauthClientService |
||
| 32 | */ |
||
| 33 | protected $oauthClientService; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Init new object. |
||
| 37 | * |
||
| 38 | * @param UserRepository $repo |
||
| 39 | * @param PermissionService $permissionService |
||
| 40 | * @param LoginProxy $loginProxy |
||
| 41 | * @param NotificationService $notificationService |
||
| 42 | * @param OauthClientService $oauthClientService |
||
| 43 | * @return void |
||
|
|
|||
| 44 | */ |
||
| 45 | public function __construct( |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Return the logged in user account. |
||
| 61 | * |
||
| 62 | * @param array $relations |
||
| 63 | * @return boolean |
||
| 64 | */ |
||
| 65 | public function account($relations = ['roles.permissions']) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Check if the logged in user or the given user |
||
| 81 | * has the given permissions on the given model. |
||
| 82 | * |
||
| 83 | * @param string $permissionName |
||
| 84 | * @param string $model |
||
| 85 | * @param mixed $userId |
||
| 86 | * @return boolean |
||
| 87 | */ |
||
| 88 | public function can($permissionName, $model, $userId = false) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Check if the logged in or the given user has the given role. |
||
| 113 | * |
||
| 114 | * @param string[] $roles |
||
| 115 | * @param mixed $user |
||
| 116 | * @return boolean |
||
| 117 | */ |
||
| 118 | public function hasRoles($roles, $user = false) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Assign the given role ids to the given user. |
||
| 125 | * |
||
| 126 | * @param integer $userId |
||
| 127 | * @param array $roleIds |
||
| 128 | * @return object |
||
| 129 | */ |
||
| 130 | View Code Duplication | public function assignRoles($userId, $roleIds) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Handle the login request to the application. |
||
| 144 | * |
||
| 145 | * @param string $email |
||
| 146 | * @param string $password |
||
| 147 | * @param string $role |
||
| 148 | * @return object |
||
| 149 | */ |
||
| 150 | public function login($email, $password, $role = false) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Handle the social login request to the application. |
||
| 165 | * |
||
| 166 | * @param string $authCode |
||
| 167 | * @param string $accessToken |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | public function loginSocial($authCode, $accessToken, $type) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Handle the registration request. |
||
| 188 | * |
||
| 189 | * @param string $name |
||
| 190 | * @param string $email |
||
| 191 | * @param string $password |
||
| 192 | * @param boolean $skipConfirmEmail |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | public function register($name, $email, $password, $skipConfirmEmail = false) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Block the user. |
||
| 213 | * |
||
| 214 | * @param integer $userId |
||
| 215 | * @return object |
||
| 216 | */ |
||
| 217 | public function block($userId) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Unblock the user. |
||
| 228 | * |
||
| 229 | * @param integer $userId |
||
| 230 | * @return object |
||
| 231 | */ |
||
| 232 | public function unblock($userId) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Send a reset link to the given user. |
||
| 239 | * |
||
| 240 | * @param string $email |
||
| 241 | * @return void |
||
| 242 | */ |
||
| 243 | public function sendReset($email) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Reset the given user's password. |
||
| 255 | * |
||
| 256 | * @param string $email |
||
| 257 | * @param string $password |
||
| 258 | * @param string $passwordConfirmation |
||
| 259 | * @param string $token |
||
| 260 | * @return string|void |
||
| 261 | */ |
||
| 262 | public function resetPassword($email, $password, $passwordConfirmation, $token) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Change the logged in user password. |
||
| 294 | * |
||
| 295 | * @param string $password |
||
| 296 | * @param string $oldPassword |
||
| 297 | * @return void |
||
| 298 | */ |
||
| 299 | public function changePassword($password, $oldPassword) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Confirm email using the confirmation code. |
||
| 311 | * |
||
| 312 | * @param string $confirmationCode |
||
| 313 | * @return void |
||
| 314 | */ |
||
| 315 | public function confirmEmail($confirmationCode) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Send the confirmation mail. |
||
| 326 | * |
||
| 327 | * @param string $email |
||
| 328 | * @return void |
||
| 329 | */ |
||
| 330 | public function sendConfirmationEmail($email) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Save the given data to the logged in user. |
||
| 343 | * |
||
| 344 | * @param string $name |
||
| 345 | * @param string $email |
||
| 346 | * @param string $profilePicture |
||
| 347 | * @return void |
||
| 348 | */ |
||
| 349 | public function saveProfile($name, $email, $profilePicture = false) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Logs out the user, revoke access token and refresh token. |
||
| 366 | * |
||
| 367 | * @return void |
||
| 368 | */ |
||
| 369 | public function logout() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Attempt to refresh the access token using the given refresh token. |
||
| 376 | * |
||
| 377 | * @param string $refreshToken |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | public function refreshToken($refreshToken) |
||
| 384 | } |
||
| 385 |
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.