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 |
||
27 | class User implements UserInterface |
||
28 | { |
||
29 | use SoftDeletableTrait, TimestampableTrait, ToggleableTrait; |
||
30 | |||
31 | /** |
||
32 | * @var mixed |
||
33 | */ |
||
34 | protected $id; |
||
35 | |||
36 | /** |
||
37 | * @var CustomerInterface |
||
38 | */ |
||
39 | protected $customer; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $username; |
||
45 | |||
46 | /** |
||
47 | * Normalized representation of a username. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $usernameCanonical; |
||
52 | |||
53 | /** |
||
54 | * Random data that is used as an additional input to a function that hashes a password. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $salt; |
||
59 | |||
60 | /** |
||
61 | * Encrypted password. Must be persisted. |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $password; |
||
66 | |||
67 | /** |
||
68 | * Password before encryption. Used for model validation. Must not be persisted. |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $plainPassword; |
||
73 | |||
74 | /** |
||
75 | * @var \DateTime |
||
76 | */ |
||
77 | protected $lastLogin; |
||
78 | |||
79 | /** |
||
80 | * Random string sent to the user email address in order to verify it |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $confirmationToken; |
||
85 | |||
86 | /** |
||
87 | * @var \DateTime |
||
88 | */ |
||
89 | protected $passwordRequestedAt; |
||
90 | |||
91 | /** |
||
92 | * @var boolean |
||
93 | */ |
||
94 | protected $locked = false; |
||
95 | |||
96 | /** |
||
97 | * @var \DateTime |
||
98 | */ |
||
99 | protected $expiresAt; |
||
100 | |||
101 | /** |
||
102 | * @var \DateTime |
||
103 | */ |
||
104 | protected $credentialsExpireAt; |
||
105 | |||
106 | /** |
||
107 | * We need at least one role to be able to authenticate |
||
108 | * |
||
109 | * @var array |
||
110 | */ |
||
111 | protected $roles = array(UserInterface::DEFAULT_ROLE); |
||
112 | |||
113 | /** |
||
114 | * @var Collection|UserOAuth[] |
||
115 | */ |
||
116 | protected $oauthAccounts; |
||
117 | |||
118 | public function __construct() |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | public function getId() |
||
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | public function getCustomer() |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function setCustomer(CustomerInterface $customer = null) |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function getUsername() |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function setUsername($username) |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | public function getUsernameCanonical() |
||
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | public function setUsernameCanonical($usernameCanonical) |
||
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | public function getSalt() |
||
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | public function getPlainPassword() |
||
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | public function setPlainPassword($password) |
||
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | public function getPassword() |
||
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | public function setPassword($password) |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | public function getExpiresAt() |
||
234 | |||
235 | /** |
||
236 | * @param \DateTime $date |
||
237 | */ |
||
238 | public function setExpiresAt(\DateTime $date = null) |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | public function getCredentialsExpireAt() |
||
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | */ |
||
254 | public function setCredentialsExpireAt(\DateTime $date = null) |
||
258 | |||
259 | /** |
||
260 | * {@inheritdoc} |
||
261 | */ |
||
262 | public function getLastLogin() |
||
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | */ |
||
270 | public function setLastLogin(\DateTime $time = null) |
||
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | public function getConfirmationToken() |
||
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | public function setConfirmationToken($confirmationToken) |
||
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | public function isCredentialsNonExpired() |
||
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | */ |
||
302 | public function isAccountNonExpired() |
||
306 | |||
307 | /** |
||
308 | * {@inheritdoc} |
||
309 | */ |
||
310 | public function setLocked($locked) |
||
314 | |||
315 | /** |
||
316 | * {@inheritdoc} |
||
317 | */ |
||
318 | public function isAccountNonLocked() |
||
322 | |||
323 | /** |
||
324 | * {@inheritdoc} |
||
325 | */ |
||
326 | public function hasRole($role) |
||
330 | |||
331 | /** |
||
332 | * {@inheritdoc} |
||
333 | */ |
||
334 | public function addRole($role) |
||
341 | |||
342 | /** |
||
343 | * {@inheritdoc} |
||
344 | */ |
||
345 | public function removeRole($role) |
||
352 | |||
353 | /** |
||
354 | * {@inheritdoc} |
||
355 | */ |
||
356 | public function getRoles() |
||
360 | |||
361 | /** |
||
362 | * {@inheritdoc} |
||
363 | */ |
||
364 | public function setRoles(array $roles) |
||
372 | |||
373 | /** |
||
374 | * {@inheritdoc} |
||
375 | */ |
||
376 | public function getEmail() |
||
380 | |||
381 | /** |
||
382 | * {@inheritdoc} |
||
383 | */ |
||
384 | public function setEmail($email) |
||
388 | |||
389 | /** |
||
390 | * {@inheritdoc} |
||
391 | */ |
||
392 | public function getEmailCanonical() |
||
396 | |||
397 | /** |
||
398 | * {@inheritdoc} |
||
399 | */ |
||
400 | public function setEmailCanonical($emailCanonical) |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | */ |
||
408 | public function isPasswordRequestNonExpired(\DateInterval $ttl) |
||
412 | |||
413 | /** |
||
414 | * Gets the timestamp that the user requested a password reset. |
||
415 | * |
||
416 | * @return null|\DateTime |
||
417 | */ |
||
418 | public function getPasswordRequestedAt() |
||
422 | |||
423 | /** |
||
424 | * {@inheritdoc} |
||
425 | */ |
||
426 | public function setPasswordRequestedAt(\DateTime $date = null) |
||
430 | |||
431 | /** |
||
432 | * {@inheritdoc} |
||
433 | */ |
||
434 | public function eraseCredentials() |
||
438 | |||
439 | /** |
||
440 | * {@inheritdoc} |
||
441 | */ |
||
442 | public function getOAuthAccounts() |
||
446 | |||
447 | /** |
||
448 | * {@inheritdoc} |
||
449 | */ |
||
450 | public function getOAuthAccount($provider) |
||
466 | |||
467 | /** |
||
468 | * {@inheritdoc} |
||
469 | */ |
||
470 | public function addOAuthAccount(UserOAuthInterface $oauth) |
||
477 | |||
478 | /** |
||
479 | * Returns username. |
||
480 | * |
||
481 | * @return string |
||
482 | */ |
||
483 | public function __toString() |
||
487 | |||
488 | /** |
||
489 | * The serialized data have to contain the fields used by the equals method and the username. |
||
490 | * |
||
491 | * @return string |
||
492 | */ |
||
493 | public function serialize() |
||
505 | |||
506 | /** |
||
507 | * @param string $serialized |
||
508 | */ |
||
509 | public function unserialize($serialized) |
||
526 | |||
527 | /** |
||
528 | * @param CustomerInterface $customer |
||
529 | */ |
||
530 | protected function assignUser(CustomerInterface $customer = null) |
||
536 | |||
537 | /** |
||
538 | * @param \DateTime $date |
||
539 | * @return bool |
||
540 | */ |
||
541 | protected function hasExpired(\DateTime $date = null) |
||
545 | } |
||
546 |