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 |
||
18 | class User implements AdvancedUserInterface, \JsonSerializable |
||
19 | { |
||
20 | const ROLE_ADMIN = 'ROLE_ADMIN'; |
||
21 | const ROLE_USER = 'ROLE_USER'; |
||
22 | |||
23 | /** |
||
24 | * @ORM\Id |
||
25 | * @ORM\GeneratedValue |
||
26 | * @ORM\Column(type="integer") |
||
27 | */ |
||
28 | private $id; |
||
29 | |||
30 | /** |
||
31 | * @ORM\Column(type="string", unique=true) |
||
32 | * @Assert\NotBlank() |
||
33 | * @Assert\Email() |
||
34 | */ |
||
35 | private $email; |
||
36 | |||
37 | /** |
||
38 | * @ORM\Column(type="string", length=40) |
||
39 | * @Assert\Length(min="1", minMessage="This field can not be less than 1 character") |
||
40 | */ |
||
41 | private $firstName; |
||
42 | |||
43 | /** |
||
44 | * @ORM\Column(type="string", length=40) |
||
45 | * @Assert\Length(min="1", minMessage="This field can not be less than 1 character") |
||
46 | */ |
||
47 | private $lastName; |
||
48 | |||
49 | /** |
||
50 | * @ORM\Column(type="string", length=64, nullable=true) |
||
51 | */ |
||
52 | private $password; |
||
53 | |||
54 | /** |
||
55 | * @ORM\Column(type="string", length=64, nullable=true) |
||
56 | */ |
||
57 | private $tmpPassword; |
||
58 | |||
59 | /** |
||
60 | * @Assert\Length(max=4096) |
||
61 | */ |
||
62 | private $plainPassword; |
||
63 | |||
64 | /** |
||
65 | * @ORM\Column(type="string", length=64) |
||
66 | */ |
||
67 | private $role; |
||
68 | |||
69 | /** |
||
70 | * @ORM\Column(name="is_active", type="boolean") |
||
71 | */ |
||
72 | private $isActive; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | * |
||
77 | * @ORM\Column(name="fb_token", type="string", nullable=true) |
||
78 | */ |
||
79 | protected $facebookToken; |
||
80 | /** |
||
81 | * @var string |
||
82 | * |
||
83 | * @ORM\Column(name="fb_id", type="string", nullable=true) |
||
84 | */ |
||
85 | protected $facebookId; |
||
86 | /** |
||
87 | * @var string |
||
88 | * |
||
89 | * @ORM\Column(name="g_token", type="string", nullable=true) |
||
90 | */ |
||
91 | protected $googleToken; |
||
92 | /** |
||
93 | * @var string |
||
94 | * |
||
95 | * @ORM\Column(name="g_id", type="string", nullable=true) |
||
96 | */ |
||
97 | protected $googleId; |
||
98 | |||
99 | /** |
||
100 | * @ORM\Column(name="is_locked", type="boolean") |
||
101 | */ |
||
102 | protected $isLocked; |
||
103 | |||
104 | /** |
||
105 | * @ORM\Column(name="hash", type="string", nullable=true) |
||
106 | */ |
||
107 | protected $hash; |
||
108 | |||
109 | /** |
||
110 | * @ORM\OneToMany(targetEntity="AppBundle\Entity\ModuleUser", mappedBy="user", cascade={"remove"}) |
||
111 | */ |
||
112 | private $modulesUser; |
||
113 | |||
114 | /** |
||
115 | * @ORM\Column(name="chosen_module", type="array") |
||
116 | */ |
||
117 | private $chosenModule; |
||
118 | |||
119 | public function jsonSerialize() |
||
128 | |||
129 | 27 | public function __construct() |
|
137 | |||
138 | /** |
||
139 | * @return mixed |
||
140 | */ |
||
141 | public function getModulesUser() |
||
145 | |||
146 | /** |
||
147 | * @param ModuleUser $moduleUser |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function addModuleUser(ModuleUser $moduleUser) |
||
156 | |||
157 | /** |
||
158 | * @param ModuleUser $moduleUser |
||
159 | */ |
||
160 | public function removeModuleUser(ModuleUser $moduleUser) |
||
164 | |||
165 | /** |
||
166 | * @return mixed |
||
167 | */ |
||
168 | 6 | public function getId() |
|
169 | { |
||
170 | 6 | return $this->id; |
|
171 | } |
||
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | public function getUsername() |
||
180 | |||
181 | /** |
||
182 | * @return mixed |
||
183 | */ |
||
184 | 4 | public function getEmail() |
|
188 | |||
189 | /** |
||
190 | * @param $email |
||
191 | */ |
||
192 | 27 | public function setEmail($email) |
|
193 | { |
||
194 | 27 | $this->email = $email; |
|
195 | |||
196 | 27 | return $this; |
|
197 | } |
||
198 | |||
199 | /** |
||
200 | * @return mixed |
||
201 | */ |
||
202 | 5 | public function getFirstName() |
|
203 | { |
||
204 | 5 | return $this->firstName; |
|
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param mixed $firstName |
||
209 | */ |
||
210 | 27 | public function setFirstName($firstName) |
|
211 | { |
||
212 | 27 | $this->firstName = $firstName; |
|
213 | |||
214 | 27 | return $this; |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * @return mixed |
||
219 | */ |
||
220 | 5 | public function getLastName() |
|
221 | { |
||
222 | 5 | return $this->lastName; |
|
223 | } |
||
224 | |||
225 | /** |
||
226 | * @param mixed $lastName |
||
227 | */ |
||
228 | 27 | public function setLastName($lastName) |
|
229 | { |
||
230 | 27 | $this->lastName = $lastName; |
|
231 | |||
232 | 27 | return $this; |
|
233 | } |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | 24 | public function getPassword() |
|
242 | |||
243 | /** |
||
244 | * @param $password |
||
245 | */ |
||
246 | 27 | public function setPassword($password) |
|
250 | |||
251 | /** |
||
252 | * @param $role |
||
253 | * @return $this |
||
254 | */ |
||
255 | 27 | public function setRole($role) |
|
261 | |||
262 | /** |
||
263 | * @return mixed |
||
264 | */ |
||
265 | public function getRole() |
||
269 | |||
270 | /** |
||
271 | * Returns the roles or permissions granted to the user for security. |
||
272 | */ |
||
273 | 24 | public function getRoles() |
|
278 | |||
279 | /** |
||
280 | * @return mixed |
||
281 | */ |
||
282 | 1 | public function getPlainPassword() |
|
283 | { |
||
284 | 1 | return $this->plainPassword; |
|
285 | } |
||
286 | |||
287 | /** |
||
288 | * @param $password |
||
289 | */ |
||
290 | public function setPlainPassword($password) |
||
291 | { |
||
292 | $this->plainPassword = $password; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Returns the salt that was originally used to encode the password. |
||
297 | */ |
||
298 | 27 | public function getSalt() |
|
306 | |||
307 | /** |
||
308 | * Removes sensitive data from the user. |
||
309 | */ |
||
310 | 24 | public function eraseCredentials() |
|
315 | |||
316 | /** |
||
317 | * @return bool |
||
318 | */ |
||
319 | 24 | public function isAccountNonExpired() |
|
323 | |||
324 | /** |
||
325 | * @return bool |
||
326 | */ |
||
327 | 24 | public function isAccountNonLocked() |
|
331 | |||
332 | /** |
||
333 | * @return bool |
||
334 | */ |
||
335 | 24 | public function isCredentialsNonExpired() |
|
339 | |||
340 | /** |
||
341 | * @return bool |
||
342 | */ |
||
343 | 24 | public function isEnabled() |
|
347 | |||
348 | /** |
||
349 | * @return bool |
||
350 | */ |
||
351 | public function getIsActive() |
||
355 | |||
356 | /** |
||
357 | * @param $active |
||
358 | * @return mixed |
||
359 | */ |
||
360 | 27 | public function setIsActive($active) |
|
364 | |||
365 | /** |
||
366 | * @return bool |
||
367 | */ |
||
368 | 2 | public function getIsLocked() |
|
372 | |||
373 | /** |
||
374 | * @param $isLocked |
||
375 | * @return mixed |
||
376 | */ |
||
377 | public function setIsLocked($isLocked) |
||
381 | |||
382 | /** |
||
383 | * @return string |
||
384 | */ |
||
385 | public function getFacebookToken() |
||
389 | |||
390 | /** |
||
391 | * @param string $facebookToken |
||
392 | * @return $this |
||
393 | */ |
||
394 | public function setFacebookToken($facebookToken) |
||
400 | |||
401 | /** |
||
402 | * @return string |
||
403 | */ |
||
404 | public function getFacebookId() |
||
405 | { |
||
406 | return $this->facebookId; |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * @param string $facebookId |
||
411 | * @return $this |
||
412 | */ |
||
413 | public function setFacebookId($facebookId) |
||
414 | { |
||
415 | $this->facebookId = $facebookId; |
||
416 | |||
417 | return $this; |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * @return string |
||
422 | */ |
||
423 | public function getGoogleToken() |
||
427 | |||
428 | /** |
||
429 | * @param string $googleToken |
||
430 | * @return $this |
||
431 | */ |
||
432 | public function setGoogleToken($googleToken) |
||
438 | |||
439 | /** |
||
440 | * @return string |
||
441 | */ |
||
442 | public function getGoogleId() |
||
443 | { |
||
444 | return $this->googleId; |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * @param string $googleId |
||
449 | * @return $this |
||
450 | */ |
||
451 | public function setGoogleId($googleId) |
||
452 | { |
||
453 | $this->googleId = $googleId; |
||
454 | |||
455 | return $this; |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * @return mixed |
||
460 | */ |
||
461 | public function getHash() |
||
465 | |||
466 | /** |
||
467 | * @param mixed $hash |
||
468 | * @return $this |
||
469 | */ |
||
470 | public function setHash($hash) |
||
476 | |||
477 | /** |
||
478 | * @return mixed |
||
479 | */ |
||
480 | public function getChosenModule() |
||
484 | |||
485 | /** |
||
486 | * @param mixed $chosenModule |
||
487 | * @return $this |
||
488 | */ |
||
489 | public function setChosenModule($chosenModule) |
||
495 | |||
496 | |||
497 | public function removeChosenModule($chosenModule) |
||
505 | |||
506 | 1 | public function getCountModules() |
|
510 | |||
511 | /** |
||
512 | * @return mixed |
||
513 | */ |
||
514 | public function getTmpPassword() |
||
518 | |||
519 | /** |
||
520 | * @param mixed $tmpPassword |
||
521 | * @return $this |
||
522 | */ |
||
523 | public function setTmpPassword($tmpPassword) |
||
528 | |||
529 | } |
||
530 | |||
531 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.