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 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. 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 User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class User implements UserInterface |
||
29 | { |
||
30 | const ROLE_ADMIN = 'ROLE_ADMIN'; |
||
31 | |||
32 | /** |
||
33 | * @var integer |
||
34 | * |
||
35 | * @ORM\Column(name="id", type="integer") |
||
36 | * @ORM\Id |
||
37 | * @ORM\GeneratedValue(strategy="AUTO") |
||
38 | */ |
||
39 | protected $id; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | * |
||
44 | * @ORM\Column(name="username", type="string", length=255) |
||
45 | * @Assert\NotBlank(message="user_admin.assert.username.empty", groups={"Profile","Adding","Editing"}) |
||
46 | * @Assert\Length(min=2, minMessage="fos_user.username.short", max=255, maxMessage="fos_user.username.long", groups={"Profile","Adding","Editing"}) |
||
47 | */ |
||
48 | protected $username; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | * |
||
53 | * @ORM\Column(name="username_canonical", type="string", length=255, unique=true) |
||
54 | */ |
||
55 | protected $usernameCanonical; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | * |
||
60 | * @ORM\Column(name="email", type="string", length=255) |
||
61 | * @Assert\NotBlank(message="user_admin.assert.email.empty", groups={"Adding","Editing","Profile"}) |
||
62 | * @Assert\Length(min=2, minMessage="fos_user.email.short", max=255, maxMessage="fos_user.email.long", groups={"Adding","Editing","Profile"}) |
||
63 | * @Assert\Email(message="user_admin.assert.email.valid", groups={"Adding","Editing","Profile"}) |
||
64 | */ |
||
65 | protected $email; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | * |
||
70 | * @ORM\Column(name="email_canonical", type="string", length=255, unique=true) |
||
71 | */ |
||
72 | protected $emailCanonical; |
||
73 | |||
74 | /** |
||
75 | * @var boolean |
||
76 | * |
||
77 | * @ORM\Column(name="enabled", type="boolean") |
||
78 | */ |
||
79 | protected $enabled; |
||
80 | |||
81 | /** |
||
82 | * The salt to use for hashing |
||
83 | * |
||
84 | * @var string |
||
85 | * |
||
86 | * @ORM\Column(name="salt", type="string") |
||
87 | */ |
||
88 | protected $salt; |
||
89 | |||
90 | /** |
||
91 | * Encrypted password. Must be persisted. |
||
92 | * |
||
93 | * @var string |
||
94 | * |
||
95 | * @ORM\Column(name="password", type="string") |
||
96 | */ |
||
97 | protected $password; |
||
98 | |||
99 | /** |
||
100 | * Plain password. Used for model validation. Must not be persisted. |
||
101 | * |
||
102 | * @var string |
||
103 | * |
||
104 | * @Assert\NotBlank(message="user_admin.assert.password.empty", groups={"Adding"}) |
||
105 | * @Assert\Length(min=2, minMessage="user_admin.assert.password.short", max=4096, groups={"Adding","Profile"}) |
||
106 | */ |
||
107 | protected $plainPassword; |
||
108 | |||
109 | /** |
||
110 | * @var \DateTime |
||
111 | * |
||
112 | * @ORM\Column(name="last_login", type="datetime", nullable=true) |
||
113 | */ |
||
114 | protected $lastLogin; |
||
115 | |||
116 | /** |
||
117 | * Random string sent to the user email address in order to verify it |
||
118 | * |
||
119 | * @var string |
||
120 | * |
||
121 | * @ORM\Column(name="confirmation_token", type="string", nullable=true) |
||
122 | */ |
||
123 | protected $confirmationToken; |
||
124 | |||
125 | /** |
||
126 | * @var \DateTime |
||
127 | * |
||
128 | * @ORM\Column(name="password_requested_at", type="datetime", nullable=true) |
||
129 | */ |
||
130 | protected $passwordRequestedAt; |
||
131 | |||
132 | /** |
||
133 | * @var Group |
||
134 | * |
||
135 | * @ORM\ManyToOne(targetEntity="DP\Core\UserBundle\Entity\Group", inversedBy="users") |
||
136 | * @ORM\JoinColumn(name="group_id", referencedColumnName="id") |
||
137 | */ |
||
138 | protected $group; |
||
139 | |||
140 | /** |
||
141 | * @var boolean |
||
142 | * |
||
143 | * @ORM\Column(name="locked", type="boolean") |
||
144 | */ |
||
145 | protected $locked; |
||
146 | |||
147 | /** |
||
148 | * @var boolean |
||
149 | * |
||
150 | * @ORM\Column(name="expired", type="boolean") |
||
151 | */ |
||
152 | protected $expired; |
||
153 | |||
154 | /** |
||
155 | * @var \DateTime |
||
156 | * |
||
157 | * @ORM\Column(name="expires_at", type="datetime", nullable=true) |
||
158 | */ |
||
159 | protected $expiresAt; |
||
160 | |||
161 | /** |
||
162 | * @var array |
||
163 | * |
||
164 | * @ORM\Column(name="roles", type="array") |
||
165 | */ |
||
166 | protected $roles; |
||
167 | |||
168 | /** |
||
169 | * @var boolean |
||
170 | * |
||
171 | * @ORM\Column(name="credentials_expired", type="boolean") |
||
172 | */ |
||
173 | protected $credentialsExpired; |
||
174 | |||
175 | /** |
||
176 | * @var \DateTime |
||
177 | * |
||
178 | * @ORM\Column(name="credentials_expire_at", type="datetime", nullable=true) |
||
179 | */ |
||
180 | protected $credentialsExpireAt; |
||
181 | |||
182 | /** |
||
183 | * @var \DateTime |
||
184 | * |
||
185 | * @ORM\Column(name="createdAt", type="datetime", nullable=true) |
||
186 | */ |
||
187 | protected $createdAt; |
||
188 | |||
189 | public function __construct() |
||
198 | |||
199 | public function addRole($role) |
||
212 | |||
213 | /** |
||
214 | * Serializes the user. |
||
215 | * |
||
216 | * The serialized data have to contain the fields used by the equals method and the username. |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | public function serialize() |
||
234 | |||
235 | /** |
||
236 | * Unserializes the user. |
||
237 | * |
||
238 | * @param string $serialized |
||
239 | */ |
||
240 | public function unserialize($serialized) |
||
259 | |||
260 | /** |
||
261 | * Removes sensitive data from the user. |
||
262 | */ |
||
263 | public function eraseCredentials() |
||
267 | |||
268 | /** |
||
269 | * Returns the user unique id. |
||
270 | * |
||
271 | * @return mixed |
||
272 | */ |
||
273 | public function getId() |
||
277 | |||
278 | public function getUsername() |
||
282 | |||
283 | public function getUsernameCanonical() |
||
287 | |||
288 | public function getSalt() |
||
292 | |||
293 | public function getEmail() |
||
297 | |||
298 | public function getEmailCanonical() |
||
302 | |||
303 | /** |
||
304 | * Gets the encrypted password. |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | public function getPassword() |
||
312 | |||
313 | public function getPlainPassword() |
||
317 | |||
318 | /** |
||
319 | * Gets the last login time. |
||
320 | * |
||
321 | * @return \DateTime |
||
322 | */ |
||
323 | public function getLastLogin() |
||
327 | |||
328 | public function getConfirmationToken() |
||
332 | |||
333 | /** |
||
334 | * Returns the user roles |
||
335 | * |
||
336 | * @return array The roles |
||
337 | */ |
||
338 | public function getRoles() |
||
351 | |||
352 | /** |
||
353 | * Never use this to check if this user has access to anything! |
||
354 | * |
||
355 | * Use the SecurityContext, or an implementation of AccessDecisionManager |
||
356 | * instead, e.g. |
||
357 | * |
||
358 | * $securityContext->isGranted('ROLE_USER'); |
||
359 | * |
||
360 | * @param string $role |
||
361 | * |
||
362 | * @return boolean |
||
363 | */ |
||
364 | public function hasRole($role) |
||
368 | |||
369 | public function isAccountNonExpired() |
||
381 | |||
382 | public function isAccountNonLocked() |
||
386 | |||
387 | public function isCredentialsNonExpired() |
||
399 | |||
400 | public function isCredentialsExpired() |
||
404 | |||
405 | public function isEnabled() |
||
409 | |||
410 | public function isExpired() |
||
414 | |||
415 | public function isLocked() |
||
419 | |||
420 | public function isSuperAdmin() |
||
424 | |||
425 | public function isUser(UserInterface $user = null) |
||
429 | |||
430 | public function removeRole($role) |
||
439 | |||
440 | public function setUsername($username) |
||
446 | |||
447 | public function setUsernameCanonical($usernameCanonical) |
||
453 | |||
454 | /** |
||
455 | * @param \DateTime $date |
||
456 | * |
||
457 | * @return User |
||
458 | */ |
||
459 | public function setCredentialsExpireAt(\DateTime $date) |
||
465 | |||
466 | /** |
||
467 | * @param boolean $boolean |
||
468 | * |
||
469 | * @return User |
||
470 | */ |
||
471 | public function setCredentialsExpired($boolean) |
||
477 | |||
478 | public function setEmail($email) |
||
484 | |||
485 | public function setEmailCanonical($emailCanonical) |
||
491 | |||
492 | public function setEnabled($boolean) |
||
498 | |||
499 | /** |
||
500 | * Sets this user to expired. |
||
501 | * |
||
502 | * @param Boolean $boolean |
||
503 | * |
||
504 | * @return User |
||
505 | */ |
||
506 | public function setExpired($boolean) |
||
512 | |||
513 | /** |
||
514 | * @param \DateTime $date |
||
515 | * |
||
516 | * @return User |
||
517 | */ |
||
518 | public function setExpiresAt(\DateTime $date) |
||
524 | |||
525 | public function setPassword($password) |
||
531 | |||
532 | public function setSuperAdmin($boolean) |
||
542 | |||
543 | public function setPlainPassword($password) |
||
549 | |||
550 | public function setLastLogin(\DateTime $time) |
||
556 | |||
557 | public function setLocked($boolean) |
||
563 | |||
564 | public function setConfirmationToken($confirmationToken) |
||
570 | |||
571 | public function setPasswordRequestedAt(\DateTime $date = null) |
||
577 | |||
578 | /** |
||
579 | * Gets the timestamp that the user requested a password reset. |
||
580 | * |
||
581 | * @return null|\DateTime |
||
582 | */ |
||
583 | public function getPasswordRequestedAt() |
||
587 | |||
588 | public function isPasswordRequestNonExpired($ttl) |
||
593 | |||
594 | public function setRoles(array $roles) |
||
604 | |||
605 | /** |
||
606 | * Gets the group assigned to the user. |
||
607 | * |
||
608 | * @return Group |
||
609 | */ |
||
610 | public function getGroup() |
||
614 | |||
615 | public function setGroup(Group $group) |
||
621 | |||
622 | public function setCreatedAt(\DateTime $date) |
||
628 | |||
629 | public function getCreatedAt() |
||
633 | |||
634 | /** |
||
635 | * @param bool $admin |
||
636 | */ |
||
637 | View Code Duplication | public function setAdmin($admin) |
|
646 | |||
647 | /** |
||
648 | * Is current user an admin ? |
||
649 | * |
||
650 | * @return bool |
||
651 | */ |
||
652 | public function isAdmin() |
||
656 | |||
657 | /** |
||
658 | * @param bool $userAdmin |
||
659 | */ |
||
660 | View Code Duplication | public function setUserAdmin($userAdmin) |
|
669 | |||
670 | /** |
||
671 | * @Assert\Callback(groups={"Adding","Editing"}) |
||
672 | */ |
||
673 | public function validateGroup(ExecutionContextInterface $context) |
||
688 | |||
689 | public function __toString() |
||
693 | } |
||
694 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: