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 |
||
18 | class User |
||
19 | { |
||
20 | /** |
||
21 | * @var int |
||
22 | * |
||
23 | * @ORM\Column(name="id", type="integer") |
||
24 | * @ORM\Id |
||
25 | * @ORM\GeneratedValue(strategy="AUTO") |
||
26 | */ |
||
27 | private $id; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | * |
||
32 | * @ORM\Column(name="name", type="string", length=50, unique=true) |
||
33 | * |
||
34 | * @Assert\NotBlank() |
||
35 | * @Assert\Length(max = 50) |
||
36 | */ |
||
37 | private $name; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | * |
||
42 | * @Gedmo\Slug(fields={"name"}, updatable=true, separator="_") |
||
43 | * @ORM\Column(name="slug", type="string", length=50, unique=true) |
||
44 | * |
||
45 | * @Assert\Length(max = 50) |
||
46 | */ |
||
47 | private $slug; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | * |
||
52 | * @ORM\Column(name="photo", type="string", length=150, nullable=true) |
||
53 | * |
||
54 | * @Assert\Length(max = 150) |
||
55 | */ |
||
56 | private $photo; |
||
57 | |||
58 | /** |
||
59 | * @Assert\File(maxSize="6000000", |
||
60 | * mimeTypes={ |
||
61 | * "image/jpeg", |
||
62 | * "image/jpg", |
||
63 | * "image/png"}) |
||
64 | */ |
||
65 | private $file; |
||
66 | |||
67 | private $temp; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | * |
||
72 | * @ORM\Column(name="email", type="string", length=50) |
||
73 | * |
||
74 | * @Assert\NotBlank() |
||
75 | * @Assert\Email() |
||
76 | */ |
||
77 | private $email; |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | * |
||
82 | * @ORM\Column(name="password", type="string", length=100) |
||
83 | * |
||
84 | * @Assert\NotBlank() |
||
85 | * @Assert\Length(max = 100) |
||
86 | */ |
||
87 | private $password; |
||
88 | |||
89 | /** |
||
90 | * @ORM\OneToMany(targetEntity="Article", mappedBy="user") |
||
91 | */ |
||
92 | private $articles; |
||
93 | |||
94 | /** |
||
95 | * @ORM\OneToMany(targetEntity="Comment", mappedBy="user") |
||
96 | */ |
||
97 | private $comments; |
||
98 | |||
99 | /** |
||
100 | * @ORM\ManyToOne(targetEntity="Role", inversedBy="users") |
||
101 | * @ORM\JoinColumn(name="role_id", referencedColumnName="id") |
||
102 | */ |
||
103 | private $role; |
||
104 | |||
105 | /** |
||
106 | * @var \DateTime |
||
107 | * |
||
108 | * @Gedmo\Timestampable(on="create") |
||
109 | * @ORM\Column(name="created_at", type="datetime") |
||
110 | * |
||
111 | * @Assert\DateTime() |
||
112 | */ |
||
113 | private $createdAt; |
||
114 | |||
115 | /** |
||
116 | * @var \DateTime |
||
117 | * |
||
118 | * @Gedmo\Timestampable(on="change", field={"user", "role", "email", "password"}) |
||
119 | * @ORM\Column(name="updated_at", type="datetime", nullable=true) |
||
120 | * |
||
121 | * @Assert\DateTime() |
||
122 | */ |
||
123 | private $updatedAt; |
||
124 | |||
125 | /** |
||
126 | * @var \DateTime |
||
127 | * |
||
128 | * @ORM\Column(name="deleted_at", type="datetime", nullable=true) |
||
129 | * |
||
130 | * @Assert\DateTime() |
||
131 | */ |
||
132 | private $deletedAt; |
||
133 | |||
134 | |||
135 | /** |
||
136 | * Get id |
||
137 | * |
||
138 | * @return int |
||
139 | */ |
||
140 | public function getId() |
||
141 | { |
||
142 | return $this->id; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Set user |
||
147 | * |
||
148 | * @param string $name |
||
149 | * |
||
150 | * @return User |
||
151 | */ |
||
152 | public function setName($name) |
||
158 | |||
159 | /** |
||
160 | * Get user |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | public function getName() |
||
168 | |||
169 | /** |
||
170 | * Set slug |
||
171 | * |
||
172 | * @param string $slug |
||
173 | * |
||
174 | * @return User |
||
175 | */ |
||
176 | public function setSlug($slug) |
||
182 | |||
183 | /** |
||
184 | * Get slug |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | public function getSlug() |
||
192 | |||
193 | /** |
||
194 | * Set email |
||
195 | * |
||
196 | * @param string $email |
||
197 | * |
||
198 | * @return User |
||
199 | */ |
||
200 | public function setEmail($email) |
||
201 | { |
||
202 | $this->email = $email; |
||
203 | |||
204 | return $this; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Get email |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | public function getEmail() |
||
216 | |||
217 | /** |
||
218 | * Set createdAt |
||
219 | * |
||
220 | * @param \DateTime $createdAt |
||
221 | * |
||
222 | * @return User |
||
223 | */ |
||
224 | public function setCreatedAt($createdAt) |
||
230 | |||
231 | /** |
||
232 | * Get createdAt |
||
233 | * |
||
234 | * @return \DateTime |
||
235 | */ |
||
236 | public function getCreatedAt() |
||
240 | |||
241 | /** |
||
242 | * Set updatedAt |
||
243 | * |
||
244 | * @param \DateTime $updatedAt |
||
245 | * |
||
246 | * @return User |
||
247 | */ |
||
248 | public function setUpdatedAt($updatedAt) |
||
254 | |||
255 | /** |
||
256 | * Get updatedAt |
||
257 | * |
||
258 | * @return \DateTime |
||
259 | */ |
||
260 | public function getUpdatedAt() |
||
264 | |||
265 | /** |
||
266 | * Set deletedAt |
||
267 | * |
||
268 | * @param \DateTime $deletedAt |
||
269 | * |
||
270 | * @return User |
||
271 | */ |
||
272 | public function setDeletedAt($deletedAt) |
||
278 | |||
279 | /** |
||
280 | * Get deletedAt |
||
281 | * |
||
282 | * @return \DateTime |
||
283 | */ |
||
284 | public function getDeletedAt() |
||
288 | /** |
||
289 | * Constructor |
||
290 | */ |
||
291 | public function __construct() |
||
296 | |||
297 | /** |
||
298 | * Add article |
||
299 | * |
||
300 | * @param \AppBundle\Entity\Article $article |
||
301 | * |
||
302 | * @return User |
||
303 | */ |
||
304 | public function addArticle(Article $article) |
||
310 | |||
311 | /** |
||
312 | * Remove article |
||
313 | * |
||
314 | * @param \AppBundle\Entity\Article $article |
||
315 | */ |
||
316 | public function removeArticle(Article $article) |
||
320 | |||
321 | /** |
||
322 | * Get articles |
||
323 | * |
||
324 | * @return \Doctrine\Common\Collections\Collection |
||
325 | */ |
||
326 | public function getArticles() |
||
330 | |||
331 | /** |
||
332 | * Add comment |
||
333 | * |
||
334 | * @param \AppBundle\Entity\Comment $comment |
||
335 | * |
||
336 | * @return User |
||
337 | */ |
||
338 | public function addComment(Comment $comment) |
||
344 | |||
345 | /** |
||
346 | * Remove comment |
||
347 | * |
||
348 | * @param \AppBundle\Entity\Comment $comment |
||
349 | */ |
||
350 | public function removeComment(Comment $comment) |
||
354 | |||
355 | /** |
||
356 | * Get comments |
||
357 | * |
||
358 | * @return \Doctrine\Common\Collections\Collection |
||
359 | */ |
||
360 | public function getComments() |
||
364 | |||
365 | /** |
||
366 | * Set role |
||
367 | * |
||
368 | * @param \AppBundle\Entity\Role $role |
||
369 | * |
||
370 | * @return User |
||
371 | */ |
||
372 | public function setRole(Role $role = null) |
||
378 | |||
379 | /** |
||
380 | * Get role |
||
381 | * |
||
382 | * @return \AppBundle\Entity\Role |
||
383 | */ |
||
384 | public function getRole() |
||
388 | |||
389 | /** |
||
390 | * Set password |
||
391 | * |
||
392 | * @param string $password |
||
393 | * |
||
394 | * @return User |
||
395 | */ |
||
396 | public function setPassword($password) |
||
402 | |||
403 | /** |
||
404 | * Get password |
||
405 | * |
||
406 | * @return string |
||
407 | */ |
||
408 | public function getPassword() |
||
412 | |||
413 | /** |
||
414 | * Set photo |
||
415 | * |
||
416 | * @param string $photo |
||
417 | * |
||
418 | * @return User |
||
419 | */ |
||
420 | public function setPhoto($photo) |
||
426 | |||
427 | /** |
||
428 | * Get photo |
||
429 | * |
||
430 | * @return string |
||
431 | */ |
||
432 | public function getPhoto() |
||
436 | |||
437 | |||
438 | /** |
||
439 | * *********************** |
||
440 | * Upload images start |
||
441 | * *********************** |
||
442 | */ |
||
443 | |||
444 | |||
445 | /** |
||
446 | * Sets file. |
||
447 | * |
||
448 | * @param UploadedFile $file |
||
449 | */ |
||
450 | View Code Duplication | public function setFile(UploadedFile $file = null) |
|
460 | |||
461 | /** |
||
462 | * Get file. |
||
463 | * |
||
464 | * @return UploadedFile |
||
465 | */ |
||
466 | public function getFile() |
||
470 | |||
471 | public function getAbsolutePath() |
||
477 | |||
478 | public function getWebPath() |
||
484 | |||
485 | protected function getUploadRootDir() |
||
489 | |||
490 | protected function getUploadDir() |
||
494 | |||
495 | /** |
||
496 | * @ORM\PrePersist() |
||
497 | * @ORM\PreUpdate() |
||
498 | */ |
||
499 | View Code Duplication | public function preUpload() |
|
506 | |||
507 | /** |
||
508 | * @ORM\PostPersist() |
||
509 | * @ORM\PostUpdate() |
||
510 | */ |
||
511 | View Code Duplication | public function upload() |
|
528 | |||
529 | /** |
||
530 | * @ORM\PostRemove() |
||
531 | */ |
||
532 | public function removeUpload() |
||
541 | |||
542 | public function clearCache($path) |
||
548 | |||
549 | /** |
||
550 | * *********************** |
||
551 | * Upload images end |
||
552 | * *********************** |
||
553 | */ |
||
554 | } |
||
555 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.