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:
| 1 | <?php |
||
| 36 | class User extends UserAggregateRoot |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * The id. |
||
| 40 | * |
||
| 41 | * @var UserId |
||
| 42 | */ |
||
| 43 | protected $id; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The confirmation token. |
||
| 47 | * |
||
| 48 | * @var UserToken |
||
| 49 | */ |
||
| 50 | protected $confirmationToken; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Created on. |
||
| 54 | * |
||
| 55 | * @var \DateTimeInterface |
||
| 56 | */ |
||
| 57 | protected $createdOn; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The email. |
||
| 61 | * |
||
| 62 | * @var UserEmail |
||
| 63 | */ |
||
| 64 | protected $email; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The invitation token. |
||
| 68 | * |
||
| 69 | * @var UserToken |
||
| 70 | */ |
||
| 71 | protected $invitationToken; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The last login. |
||
| 75 | * |
||
| 76 | * @var \DateTimeInterface|null |
||
| 77 | */ |
||
| 78 | protected $lastLogin; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The password. |
||
| 82 | * |
||
| 83 | * @var UserPassword |
||
| 84 | */ |
||
| 85 | protected $password; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The remember password token. |
||
| 89 | * |
||
| 90 | * @var UserToken |
||
| 91 | */ |
||
| 92 | protected $rememberPasswordToken; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Array which contains roles. |
||
| 96 | * |
||
| 97 | * @var UserRole[] |
||
| 98 | */ |
||
| 99 | protected $roles; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Updated on. |
||
| 103 | * |
||
| 104 | * @var \DateTimeInterface |
||
| 105 | */ |
||
| 106 | protected $updatedOn; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Constructor. |
||
| 110 | * |
||
| 111 | * @param UserId $anId The id |
||
| 112 | * @param UserEmail $anEmail The email |
||
| 113 | * @param UserPassword $aPassword The encoded password |
||
|
|
|||
| 114 | * @param array $userRoles Array which contains the roles |
||
| 115 | */ |
||
| 116 | protected function __construct( |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Sign up user. |
||
| 137 | * |
||
| 138 | * @param UserId $anId The id |
||
| 139 | * @param UserEmail $anEmail The email |
||
| 140 | * @param UserPassword $aPassword The encoded password |
||
| 141 | * @param array $userRoles Array which contains the roles |
||
| 142 | * |
||
| 143 | * @return static |
||
| 144 | */ |
||
| 145 | View Code Duplication | public static function signUp(UserId $anId, UserEmail $anEmail, UserPassword $aPassword, array $userRoles) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Invites user. |
||
| 161 | * |
||
| 162 | * @param UserId $anId The id |
||
| 163 | * @param UserEmail $anEmail The email |
||
| 164 | * |
||
| 165 | * @return static |
||
| 166 | */ |
||
| 167 | View Code Duplication | public static function invite(UserId $anId, UserEmail $anEmail) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Gets the id. |
||
| 184 | * |
||
| 185 | * @return UserId |
||
| 186 | */ |
||
| 187 | public function id() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Updates the user password. |
||
| 194 | * |
||
| 195 | * @param UserPassword $aPassword The old password |
||
| 196 | */ |
||
| 197 | public function changePassword(UserPassword $aPassword) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Gets the confirmation token. |
||
| 205 | * |
||
| 206 | * @return UserToken |
||
| 207 | */ |
||
| 208 | public function confirmationToken() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Gets the created on. |
||
| 215 | * |
||
| 216 | * @return \DateTimeInterface |
||
| 217 | */ |
||
| 218 | public function createdOn() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Gets the email. |
||
| 225 | * |
||
| 226 | * @return UserEmail |
||
| 227 | */ |
||
| 228 | public function email() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Enables the user account. |
||
| 235 | */ |
||
| 236 | public function enableAccount() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Adds the given role. |
||
| 250 | * |
||
| 251 | * @param UserRole $aRole The user role |
||
| 252 | */ |
||
| 253 | public function grant(UserRole $aRole) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Gets the invitation token. |
||
| 274 | * |
||
| 275 | * @return UserToken |
||
| 276 | */ |
||
| 277 | public function invitationToken() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Checks if the user is enabled or not. |
||
| 284 | * |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | public function isEnabled() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Checks if the user has the given role. |
||
| 294 | * |
||
| 295 | * @param UserRole $aRole The user role |
||
| 296 | * |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | public function isGranted(UserRole $aRole) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Checks if the role given appears between allowed roles. |
||
| 312 | * |
||
| 313 | * @param UserRole $aRole The user role |
||
| 314 | * |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | public function isRoleAllowed(UserRole $aRole) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Gets the last login. |
||
| 324 | * |
||
| 325 | * @return \DateTimeInterface |
||
| 326 | */ |
||
| 327 | public function lastLogin() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Validates user login for the given password. |
||
| 334 | * |
||
| 335 | * @param string $aPlainPassword Plain password used to log in |
||
| 336 | * @param UserPasswordEncoder $anEncoder The encoder used to encode the password |
||
| 337 | * |
||
| 338 | * @throws UserInactiveException when the user is not enabled |
||
| 339 | * @throws UserPasswordInvalidException when the user password is invalid |
||
| 340 | */ |
||
| 341 | public function login($aPlainPassword, UserPasswordEncoder $anEncoder) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Updated the user state after logout. |
||
| 361 | * |
||
| 362 | * @throws UserInactiveException when the user is not enabled |
||
| 363 | */ |
||
| 364 | public function logout() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Gets the password. |
||
| 380 | * |
||
| 381 | * @return UserPassword |
||
| 382 | */ |
||
| 383 | public function password() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Updates the invitation token. |
||
| 390 | */ |
||
| 391 | public function regenerateInvitationToken() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Gets the remember password token. |
||
| 401 | * |
||
| 402 | * @return UserToken |
||
| 403 | */ |
||
| 404 | public function rememberPasswordToken() |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Remembers the password. |
||
| 411 | */ |
||
| 412 | public function rememberPassword() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Removes the given role. |
||
| 427 | * |
||
| 428 | * @param UserRole $aRole The user role |
||
| 429 | */ |
||
| 430 | public function revoke(UserRole $aRole) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Gets the roles. |
||
| 453 | * |
||
| 454 | * @return UserRole[] |
||
| 455 | */ |
||
| 456 | public function roles() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Gets the updated on. |
||
| 463 | * |
||
| 464 | * @return \DateTimeInterface |
||
| 465 | */ |
||
| 466 | public function updatedOn() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Gets the available roles in scalar type. |
||
| 473 | * |
||
| 474 | * This method is an extension point that it allows |
||
| 475 | * to add more roles easily in the domain. |
||
| 476 | * |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | public static function availableRoles() |
||
| 483 | } |
||
| 484 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.