| Total Complexity | 49 |
| Total Lines | 445 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like User 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.
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 User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | abstract class User implements UserInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var int |
||
| 18 | */ |
||
| 19 | protected $id; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $username; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $email; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var boolean |
||
| 33 | */ |
||
| 34 | protected $enabled; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var boolean |
||
| 38 | */ |
||
| 39 | protected $enforcePasswordChange; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The salt to use for hashing |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $salt; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Encrypted password. Must be persisted. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $password; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Plain password. Used for model validation. Must not be persisted. |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $plainPassword; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \DateTime |
||
| 64 | */ |
||
| 65 | protected $lastLogin; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var TokenInterface |
||
| 69 | */ |
||
| 70 | protected $activationToken; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var TokenInterface |
||
| 74 | */ |
||
| 75 | protected $passwordResetToken; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var boolean |
||
| 79 | */ |
||
| 80 | protected $locked; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var boolean |
||
| 84 | */ |
||
| 85 | protected $expired; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var \DateTime |
||
| 89 | */ |
||
| 90 | protected $expiresAt; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected $roles; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var boolean |
||
| 99 | */ |
||
| 100 | protected $credentialsExpired; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var \DateTime |
||
| 104 | */ |
||
| 105 | protected $credentialsExpireAt; |
||
| 106 | |||
| 107 | public function __construct() |
||
| 108 | { |
||
| 109 | $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36); |
||
|
|
|||
| 110 | $this->enabled = false; |
||
| 111 | $this->locked = false; |
||
| 112 | $this->enforcePasswordChange = false; |
||
| 113 | $this->expired = false; |
||
| 114 | $this->roles = []; |
||
| 115 | $this->credentialsExpired = false; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Serializes the user. |
||
| 120 | * |
||
| 121 | * The serialized data have to contain the fields used by the equals method and the username. |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function serialize() |
||
| 126 | { |
||
| 127 | return serialize([ |
||
| 128 | $this->password, |
||
| 129 | $this->salt, |
||
| 130 | $this->username, |
||
| 131 | $this->expired, |
||
| 132 | $this->locked, |
||
| 133 | $this->credentialsExpired, |
||
| 134 | $this->enabled, |
||
| 135 | $this->id |
||
| 136 | ]); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Unserializes the user. |
||
| 141 | * |
||
| 142 | * @param string $serialized |
||
| 143 | */ |
||
| 144 | public function unserialize($serialized) |
||
| 145 | { |
||
| 146 | $data = unserialize($serialized); |
||
| 147 | // add a few extra elements in the array to ensure that we have enough keys when unserializing |
||
| 148 | // older data which does not include all properties. |
||
| 149 | $data = array_merge($data, array_fill(0, 2, null)); |
||
| 150 | |||
| 151 | list( |
||
| 152 | $this->password, |
||
| 153 | $this->salt, |
||
| 154 | $this->username, |
||
| 155 | $this->expired, |
||
| 156 | $this->locked, |
||
| 157 | $this->credentialsExpired, |
||
| 158 | $this->enabled, |
||
| 159 | $this->id |
||
| 160 | ) = $data; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Removes sensitive data from the user. |
||
| 165 | */ |
||
| 166 | public function eraseCredentials() |
||
| 167 | { |
||
| 168 | $this->plainPassword = null; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Returns the user unique id. |
||
| 173 | * |
||
| 174 | * @return mixed |
||
| 175 | */ |
||
| 176 | public function getId() |
||
| 177 | { |
||
| 178 | return $this->id; |
||
| 179 | } |
||
| 180 | |||
| 181 | public function getUsername() |
||
| 182 | { |
||
| 183 | return $this->username; |
||
| 184 | } |
||
| 185 | |||
| 186 | public function getSalt() |
||
| 187 | { |
||
| 188 | return $this->salt; |
||
| 189 | } |
||
| 190 | |||
| 191 | public function getEmail() |
||
| 192 | { |
||
| 193 | return $this->email; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Gets the encrypted password. |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function getPassword() |
||
| 202 | { |
||
| 203 | return $this->password; |
||
| 204 | } |
||
| 205 | |||
| 206 | public function getPlainPassword() |
||
| 207 | { |
||
| 208 | return $this->plainPassword; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Gets the last login time. |
||
| 213 | * |
||
| 214 | * @return \DateTime |
||
| 215 | */ |
||
| 216 | public function getLastLogin() |
||
| 217 | { |
||
| 218 | return $this->lastLogin; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return TokenInterface |
||
| 223 | */ |
||
| 224 | public function getActivationToken() |
||
| 225 | { |
||
| 226 | return $this->activationToken; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param TokenInterface $activationToken |
||
| 231 | */ |
||
| 232 | public function setActivationToken(TokenInterface $activationToken) |
||
| 233 | { |
||
| 234 | $this->activationToken = $activationToken; |
||
| 235 | } |
||
| 236 | |||
| 237 | public function removeActivationToken() |
||
| 238 | { |
||
| 239 | $this->activationToken = null; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return TokenInterface |
||
| 244 | */ |
||
| 245 | public function getPasswordResetToken() |
||
| 246 | { |
||
| 247 | return $this->passwordResetToken; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param TokenInterface $passwordResetToken |
||
| 252 | */ |
||
| 253 | public function setPasswordResetToken(TokenInterface $passwordResetToken) |
||
| 254 | { |
||
| 255 | $this->passwordResetToken = $passwordResetToken; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function removePasswordResetToken() |
||
| 259 | { |
||
| 260 | $this->passwordResetToken = null; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Returns the user roles |
||
| 265 | * |
||
| 266 | * @return array The roles |
||
| 267 | */ |
||
| 268 | public function getRoles() |
||
| 269 | { |
||
| 270 | $roles = $this->roles; |
||
| 271 | |||
| 272 | return array_unique($roles); |
||
| 273 | } |
||
| 274 | |||
| 275 | public function isAccountNonExpired() |
||
| 276 | { |
||
| 277 | if (true === $this->expired) { |
||
| 278 | return false; |
||
| 279 | } |
||
| 280 | |||
| 281 | if (null !== $this->expiresAt && $this->expiresAt->getTimestamp() < time()) { |
||
| 282 | return false; |
||
| 283 | } |
||
| 284 | |||
| 285 | return true; |
||
| 286 | } |
||
| 287 | |||
| 288 | public function isAccountNonLocked() |
||
| 289 | { |
||
| 290 | return !$this->locked; |
||
| 291 | } |
||
| 292 | |||
| 293 | public function isCredentialsNonExpired() |
||
| 294 | { |
||
| 295 | if (true === $this->credentialsExpired) { |
||
| 296 | return false; |
||
| 297 | } |
||
| 298 | |||
| 299 | if (null !== $this->credentialsExpireAt && $this->credentialsExpireAt->getTimestamp() < time()) { |
||
| 300 | return false; |
||
| 301 | } |
||
| 302 | |||
| 303 | return true; |
||
| 304 | } |
||
| 305 | |||
| 306 | public function isCredentialsExpired() |
||
| 307 | { |
||
| 308 | return !$this->isCredentialsNonExpired(); |
||
| 309 | } |
||
| 310 | |||
| 311 | public function isEnabled() |
||
| 312 | { |
||
| 313 | return $this->enabled; |
||
| 314 | } |
||
| 315 | |||
| 316 | public function isExpired() |
||
| 317 | { |
||
| 318 | return !$this->isAccountNonExpired(); |
||
| 319 | } |
||
| 320 | |||
| 321 | public function isLocked() |
||
| 322 | { |
||
| 323 | return !$this->isAccountNonLocked(); |
||
| 324 | } |
||
| 325 | |||
| 326 | public function addRole($role) |
||
| 327 | { |
||
| 328 | $role = strtoupper($role); |
||
| 329 | |||
| 330 | if (!in_array($role, $this->roles, true)) { |
||
| 331 | $this->roles[] = $role; |
||
| 332 | } |
||
| 333 | |||
| 334 | return $this; |
||
| 335 | } |
||
| 336 | |||
| 337 | public function removeRole($role) |
||
| 338 | { |
||
| 339 | if (false !== $key = array_search(strtoupper($role), $this->roles, true)) { |
||
| 340 | unset($this->roles[$key]); |
||
| 341 | $this->roles = array_values($this->roles); |
||
| 342 | } |
||
| 343 | |||
| 344 | return $this; |
||
| 345 | } |
||
| 346 | |||
| 347 | public function setUsername($username) |
||
| 348 | { |
||
| 349 | $this->username = $username; |
||
| 350 | |||
| 351 | return $this; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param \DateTime $date |
||
| 356 | * |
||
| 357 | * @return User |
||
| 358 | */ |
||
| 359 | public function setCredentialsExpireAt(\DateTime $date) |
||
| 360 | { |
||
| 361 | $this->credentialsExpireAt = $date; |
||
| 362 | |||
| 363 | return $this; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param boolean $boolean |
||
| 368 | * |
||
| 369 | * @return User |
||
| 370 | */ |
||
| 371 | public function setCredentialsExpired($boolean) |
||
| 372 | { |
||
| 373 | $this->credentialsExpired = $boolean; |
||
| 374 | |||
| 375 | return $this; |
||
| 376 | } |
||
| 377 | |||
| 378 | public function setEmail($email) |
||
| 379 | { |
||
| 380 | $this->email = $email; |
||
| 381 | |||
| 382 | return $this; |
||
| 383 | } |
||
| 384 | |||
| 385 | public function setEnabled($boolean) |
||
| 386 | { |
||
| 387 | $this->enabled = (Boolean) $boolean; |
||
| 388 | |||
| 389 | return $this; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Sets this user to expired. |
||
| 394 | * |
||
| 395 | * @param Boolean $boolean |
||
| 396 | * |
||
| 397 | * @return User |
||
| 398 | */ |
||
| 399 | public function setExpired($boolean) |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param \DateTime $date |
||
| 408 | * |
||
| 409 | * @return User |
||
| 410 | */ |
||
| 411 | public function setExpiresAt(\DateTime $date) |
||
| 412 | { |
||
| 413 | $this->expiresAt = $date; |
||
| 414 | |||
| 415 | return $this; |
||
| 416 | } |
||
| 417 | |||
| 418 | public function setPassword($password) |
||
| 419 | { |
||
| 420 | $this->password = $password; |
||
| 421 | |||
| 422 | return $this; |
||
| 423 | } |
||
| 424 | |||
| 425 | public function setPlainPassword($password) |
||
| 426 | { |
||
| 427 | $this->plainPassword = $password; |
||
| 428 | |||
| 429 | return $this; |
||
| 430 | } |
||
| 431 | |||
| 432 | public function setLastLogin(\DateTime $time) |
||
| 433 | { |
||
| 434 | $this->lastLogin = $time; |
||
| 435 | |||
| 436 | return $this; |
||
| 437 | } |
||
| 438 | |||
| 439 | public function setLocked($boolean) |
||
| 440 | { |
||
| 441 | $this->locked = $boolean; |
||
| 442 | |||
| 443 | return $this; |
||
| 444 | } |
||
| 445 | |||
| 446 | public function isForcedToChangePassword() |
||
| 447 | { |
||
| 448 | return $this->enforcePasswordChange; |
||
| 449 | } |
||
| 450 | |||
| 451 | public function enforcePasswordChange($enforcePasswordChange) |
||
| 454 | } |
||
| 455 | |||
| 456 | public function __toString() |
||
| 457 | { |
||
| 458 | return (string) $this->getUsername(); |
||
| 459 | } |
||
| 460 | } |
||
| 461 |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.