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 |
||
21 | class User implements UserInterface |
||
22 | { |
||
23 | use TimestampableTrait, ToggleableTrait; |
||
24 | |||
25 | /** |
||
26 | * @var mixed |
||
27 | */ |
||
28 | protected $id; |
||
29 | |||
30 | /** |
||
31 | * @var string|null |
||
32 | */ |
||
33 | protected $username; |
||
34 | |||
35 | /** |
||
36 | * Normalized representation of a username. |
||
37 | * |
||
38 | * @var string|null |
||
39 | */ |
||
40 | protected $usernameCanonical; |
||
41 | |||
42 | /** |
||
43 | * Random data that is used as an additional input to a function that hashes a password. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $salt; |
||
48 | |||
49 | /** |
||
50 | * Encrypted password. Must be persisted. |
||
51 | * |
||
52 | * @var string|null |
||
53 | */ |
||
54 | protected $password; |
||
55 | |||
56 | /** |
||
57 | * Password before encryption. Used for model validation. Must not be persisted. |
||
58 | * |
||
59 | * @var string|null |
||
60 | */ |
||
61 | protected $plainPassword; |
||
62 | |||
63 | /** |
||
64 | * @var \DateTimeInterface|null |
||
65 | */ |
||
66 | protected $lastLogin; |
||
67 | |||
68 | /** |
||
69 | * Random string sent to the user email address in order to verify it |
||
70 | * |
||
71 | * @var string|null |
||
72 | */ |
||
73 | protected $emailVerificationToken; |
||
74 | |||
75 | /** |
||
76 | * Random string sent to the user email address in order to verify the password resetting request |
||
77 | * |
||
78 | * @var string|null |
||
79 | */ |
||
80 | protected $passwordResetToken; |
||
81 | |||
82 | /** |
||
83 | * @var \DateTimeInterface|null |
||
84 | */ |
||
85 | protected $passwordRequestedAt; |
||
86 | |||
87 | /** |
||
88 | * @var \DateTimeInterface|null |
||
89 | */ |
||
90 | protected $verifiedAt; |
||
91 | |||
92 | /** |
||
93 | * @var bool |
||
94 | */ |
||
95 | protected $locked = false; |
||
96 | |||
97 | /** |
||
98 | * @var \DateTimeInterface|null |
||
99 | */ |
||
100 | protected $expiresAt; |
||
101 | |||
102 | /** |
||
103 | * @var \DateTimeInterface|null |
||
104 | */ |
||
105 | protected $credentialsExpireAt; |
||
106 | |||
107 | /** |
||
108 | * We need at least one role to be able to authenticate |
||
109 | * |
||
110 | * @var array |
||
111 | */ |
||
112 | protected $roles = [UserInterface::DEFAULT_ROLE]; |
||
113 | |||
114 | /** |
||
115 | * @var Collection|UserOAuth[] |
||
116 | */ |
||
117 | protected $oauthAccounts; |
||
118 | |||
119 | /** |
||
120 | * @var string|null |
||
121 | */ |
||
122 | protected $email; |
||
123 | |||
124 | /** |
||
125 | * @var string|null |
||
126 | */ |
||
127 | protected $emailCanonical; |
||
128 | |||
129 | public function __construct() |
||
138 | |||
139 | /** |
||
140 | * @return string |
||
141 | */ |
||
142 | public function __toString(): string |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | public function getId() |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function getEmail(): ?string |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function setEmail(?string $email): void |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | public function getEmailCanonical(): ?string |
||
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | public function setEmailCanonical(?string $emailCanonical): void |
||
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | public function getUsername(): ?string |
||
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | public function setUsername(?string $username): void |
||
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | public function getUsernameCanonical(): ?string |
||
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | public function setUsernameCanonical(?string $usernameCanonical): void |
||
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | public function getSalt(): string |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | public function getPlainPassword(): ?string |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | public function setPlainPassword(?string $password): void |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | public function getPassword(): ?string |
||
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | */ |
||
254 | public function setPassword(?string $password): void |
||
258 | |||
259 | /** |
||
260 | * {@inheritdoc} |
||
261 | */ |
||
262 | public function getExpiresAt(): ?\DateTimeInterface |
||
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | */ |
||
270 | public function setExpiresAt(?\DateTimeInterface $date): void |
||
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | public function getCredentialsExpireAt(): ?\DateTimeInterface |
||
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | public function setCredentialsExpireAt(?\DateTimeInterface $date): void |
||
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | public function getLastLogin(): ?\DateTimeInterface |
||
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | */ |
||
302 | public function setLastLogin(?\DateTimeInterface $time): void |
||
306 | |||
307 | /** |
||
308 | * {@inheritdoc} |
||
309 | */ |
||
310 | public function getEmailVerificationToken(): ?string |
||
314 | |||
315 | /** |
||
316 | * {@inheritdoc} |
||
317 | */ |
||
318 | public function setEmailVerificationToken(?string $verificationToken): void |
||
322 | |||
323 | /** |
||
324 | * {@inheritdoc} |
||
325 | */ |
||
326 | public function getPasswordResetToken(): ?string |
||
330 | |||
331 | /** |
||
332 | * {@inheritdoc} |
||
333 | */ |
||
334 | public function setPasswordResetToken(?string $passwordResetToken): void |
||
338 | |||
339 | /** |
||
340 | * {@inheritdoc} |
||
341 | */ |
||
342 | public function isCredentialsNonExpired(): bool |
||
346 | |||
347 | /** |
||
348 | * {@inheritdoc} |
||
349 | */ |
||
350 | public function isAccountNonExpired(): bool |
||
354 | |||
355 | /** |
||
356 | * {@inheritdoc} |
||
357 | */ |
||
358 | public function setLocked(bool $locked): void |
||
362 | |||
363 | /** |
||
364 | * {@inheritdoc} |
||
365 | */ |
||
366 | public function isAccountNonLocked(): bool |
||
370 | |||
371 | /** |
||
372 | * {@inheritdoc} |
||
373 | */ |
||
374 | public function hasRole(string $role): bool |
||
378 | |||
379 | /** |
||
380 | * {@inheritdoc} |
||
381 | */ |
||
382 | public function addRole(string $role): void |
||
389 | |||
390 | /** |
||
391 | * {@inheritdoc} |
||
392 | */ |
||
393 | public function removeRole(string $role): void |
||
400 | |||
401 | /** |
||
402 | * {@inheritdoc} |
||
403 | */ |
||
404 | public function getRoles(): array |
||
408 | |||
409 | /** |
||
410 | * {@inheritdoc} |
||
411 | */ |
||
412 | public function isPasswordRequestNonExpired(\DateInterval $ttl): bool |
||
423 | |||
424 | /** |
||
425 | * {@inheritdoc} |
||
426 | */ |
||
427 | public function getPasswordRequestedAt(): ?\DateTimeInterface |
||
431 | |||
432 | /** |
||
433 | * {@inheritdoc} |
||
434 | */ |
||
435 | public function setPasswordRequestedAt(?\DateTimeInterface $date): void |
||
439 | |||
440 | /** |
||
441 | * {@inheritdoc} |
||
442 | */ |
||
443 | public function isVerified(): bool |
||
447 | |||
448 | /** |
||
449 | * {@inheritdoc} |
||
450 | */ |
||
451 | public function getVerifiedAt(): ?\DateTimeInterface |
||
455 | |||
456 | /** |
||
457 | * {@inheritdoc} |
||
458 | */ |
||
459 | public function setVerifiedAt(?\DateTimeInterface $verifiedAt): void |
||
463 | |||
464 | /** |
||
465 | * {@inheritdoc} |
||
466 | */ |
||
467 | public function eraseCredentials(): void |
||
471 | |||
472 | /** |
||
473 | * {@inheritdoc} |
||
474 | */ |
||
475 | public function getOAuthAccounts(): Collection |
||
479 | |||
480 | /** |
||
481 | * {@inheritdoc} |
||
482 | */ |
||
483 | public function getOAuthAccount(string $provider): ?UserOAuthInterface |
||
499 | |||
500 | /** |
||
501 | * {@inheritdoc} |
||
502 | */ |
||
503 | public function addOAuthAccount(UserOAuthInterface $oauth): void |
||
510 | |||
511 | /** |
||
512 | * The serialized data have to contain the fields used by the equals method and the username. |
||
513 | * |
||
514 | * @return string |
||
515 | */ |
||
516 | public function serialize(): string |
||
528 | |||
529 | /** |
||
530 | * @param string $serialized |
||
531 | */ |
||
532 | public function unserialize($serialized): void |
||
549 | |||
550 | /** |
||
551 | * @param \DateTimeInterface|null $date |
||
552 | * |
||
553 | * @return bool |
||
554 | */ |
||
555 | protected function hasExpired(?\DateTimeInterface $date): bool |
||
559 | } |
||
560 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..