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 Folder 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 Folder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Folder extends AbstractEntity implements GedmoNode |
||
25 | { |
||
26 | const TYPE_FILES = 'files'; |
||
27 | const TYPE_IMAGE = 'image'; |
||
28 | const TYPE_MEDIA = 'media'; |
||
29 | const TYPE_SLIDESHOW = 'slideshow'; |
||
30 | const TYPE_VIDEO = 'video'; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | * |
||
35 | * @Gedmo\Translatable |
||
36 | * @ORM\Column(type="string") |
||
37 | * @Assert\NotBlank() |
||
38 | */ |
||
39 | protected $name; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | * |
||
44 | * @Gedmo\Locale |
||
45 | * Used locale to override Translation listener`s locale |
||
46 | * this is not a mapped field of entity metadata, just a simple property |
||
47 | */ |
||
48 | protected $locale; |
||
49 | |||
50 | /** |
||
51 | * @var Folder |
||
52 | * |
||
53 | * @ORM\ManyToOne(targetEntity="Folder", inversedBy="children", fetch="LAZY") |
||
54 | * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true) |
||
55 | * @Gedmo\TreeParent |
||
56 | */ |
||
57 | protected $parent; |
||
58 | |||
59 | /** |
||
60 | * @var ArrayCollection |
||
61 | * |
||
62 | * @ORM\OneToMany(targetEntity="Folder", mappedBy="parent", fetch="LAZY") |
||
63 | * @ORM\OrderBy({"lft" = "ASC"}) |
||
64 | */ |
||
65 | protected $children; |
||
66 | |||
67 | /** |
||
68 | * @var ArrayCollection |
||
69 | * |
||
70 | * @ORM\OneToMany(targetEntity="Media", mappedBy="folder", fetch="LAZY") |
||
71 | * @ORM\OrderBy({"name" = "ASC"}) |
||
72 | */ |
||
73 | protected $media; |
||
74 | |||
75 | /** |
||
76 | * @var \DateTime |
||
77 | * |
||
78 | * @ORM\Column(type="datetime", name="created_at") |
||
79 | */ |
||
80 | protected $createdAt; |
||
81 | |||
82 | /** |
||
83 | * @var \DateTime |
||
84 | * |
||
85 | * @ORM\Column(type="datetime", name="updated_at") |
||
86 | */ |
||
87 | protected $updatedAt; |
||
88 | |||
89 | /** |
||
90 | * @var string |
||
91 | * |
||
92 | * @ORM\Column(type="string", nullable=true) |
||
93 | */ |
||
94 | protected $rel; |
||
95 | |||
96 | /** |
||
97 | * @var string |
||
98 | * |
||
99 | * @ORM\Column(type="string", name="internal_name", nullable=true) |
||
100 | */ |
||
101 | protected $internalName; |
||
102 | |||
103 | /** |
||
104 | * @var int |
||
105 | * |
||
106 | * @ORM\Column(name="lft", type="integer", nullable=true) |
||
107 | * @Gedmo\TreeLeft |
||
108 | */ |
||
109 | protected $lft; |
||
110 | |||
111 | /** |
||
112 | * @var int |
||
113 | * |
||
114 | * @ORM\Column(name="lvl", type="integer", nullable=true) |
||
115 | * @Gedmo\TreeLevel |
||
116 | */ |
||
117 | protected $lvl; |
||
118 | |||
119 | /** |
||
120 | * @var int |
||
121 | * |
||
122 | * @ORM\Column(name="rgt", type="integer", nullable=true) |
||
123 | * @Gedmo\TreeRight |
||
124 | */ |
||
125 | protected $rgt; |
||
126 | |||
127 | /** |
||
128 | * @var bool |
||
129 | * |
||
130 | * @ORM\Column(type="boolean") |
||
131 | */ |
||
132 | protected $deleted; |
||
133 | |||
134 | /** |
||
135 | * constructor |
||
136 | */ |
||
137 | 24 | View Code Duplication | public function __construct() |
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | public static function allTypes() |
||
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | 1 | public function getTranslatableLocale() |
|
167 | |||
168 | /** |
||
169 | * @param string $locale |
||
170 | * |
||
171 | * @return Folder |
||
172 | */ |
||
173 | 1 | public function setTranslatableLocale($locale) |
|
179 | |||
180 | /** |
||
181 | * @return string |
||
182 | */ |
||
183 | 1 | public function getRel() |
|
187 | |||
188 | /** |
||
189 | * @param string $rel |
||
190 | * |
||
191 | * @return Folder |
||
192 | */ |
||
193 | 1 | public function setRel($rel) |
|
199 | |||
200 | /** |
||
201 | * Get createdAd |
||
202 | * |
||
203 | * @return \DateTime |
||
204 | */ |
||
205 | 1 | public function getCreatedAt() |
|
209 | |||
210 | /** |
||
211 | * Set createdAd |
||
212 | * |
||
213 | * @param \DateTime $createdAt |
||
214 | * |
||
215 | * @return Folder |
||
216 | */ |
||
217 | 24 | public function setCreatedAt($createdAt) |
|
223 | |||
224 | /** |
||
225 | * Get updatedAt |
||
226 | * |
||
227 | * @return \DateTime |
||
228 | */ |
||
229 | 1 | public function getUpdatedAt() |
|
233 | |||
234 | /** |
||
235 | * Set updatedAt |
||
236 | * |
||
237 | * @param \DateTime $updatedAt |
||
238 | * |
||
239 | * @return Folder |
||
240 | */ |
||
241 | 24 | public function setUpdatedAt($updatedAt) |
|
247 | |||
248 | /** |
||
249 | * @return Folder[]: |
||
250 | */ |
||
251 | 1 | View Code Duplication | public function getParents() |
262 | |||
263 | /** |
||
264 | * Get parent |
||
265 | * |
||
266 | * @return Folder |
||
267 | */ |
||
268 | 3 | public function getParent() |
|
272 | |||
273 | /** |
||
274 | * Set parent |
||
275 | * |
||
276 | * @param Folder $parent |
||
277 | * |
||
278 | * @return Folder |
||
279 | */ |
||
280 | 4 | public function setParent(Folder $parent = null) |
|
286 | |||
287 | /** |
||
288 | * Add a child |
||
289 | * |
||
290 | * @param Folder $child |
||
291 | * |
||
292 | * @return Folder |
||
293 | */ |
||
294 | 2 | public function addChild(Folder $child) |
|
301 | |||
302 | /** |
||
303 | * Add file |
||
304 | * |
||
305 | * @param Media $media |
||
306 | * |
||
307 | * @return Folder |
||
308 | */ |
||
309 | 2 | public function addMedia(Media $media) |
|
315 | |||
316 | /** |
||
317 | * Get media |
||
318 | * |
||
319 | * @param bool $includeDeleted |
||
320 | * |
||
321 | * @return ArrayCollection |
||
322 | */ |
||
323 | 2 | View Code Duplication | public function getMedia($includeDeleted = false) |
339 | |||
340 | /** |
||
341 | * @param int $id |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | 1 | public function hasActive($id) |
|
355 | |||
356 | /** |
||
357 | * Get child folders |
||
358 | * |
||
359 | * @param bool $includeDeleted |
||
360 | * |
||
361 | * @return ArrayCollection |
||
362 | */ |
||
363 | 3 | View Code Duplication | public function getChildren($includeDeleted = false) |
379 | |||
380 | /** |
||
381 | * @param ArrayCollection $children |
||
382 | * |
||
383 | * @return Folder |
||
384 | */ |
||
385 | 1 | public function setChildren($children) |
|
391 | |||
392 | /** |
||
393 | * @return bool |
||
394 | */ |
||
395 | 4 | public function isDeleted() |
|
399 | |||
400 | /** |
||
401 | * @param bool $deleted |
||
402 | * |
||
403 | * @return Folder |
||
404 | */ |
||
405 | 2 | public function setDeleted($deleted) |
|
411 | |||
412 | /** |
||
413 | * @return string |
||
414 | */ |
||
415 | 1 | public function getInternalName() |
|
419 | |||
420 | /** |
||
421 | * @param string $internalName |
||
422 | * |
||
423 | * @return Folder |
||
424 | */ |
||
425 | 1 | public function setInternalName($internalName) |
|
431 | |||
432 | /** |
||
433 | * @return string |
||
434 | */ |
||
435 | 1 | public function __toString() |
|
439 | |||
440 | /** |
||
441 | * @return string |
||
442 | */ |
||
443 | 2 | public function getName() |
|
447 | |||
448 | /** |
||
449 | * @param string $name |
||
450 | * |
||
451 | * @return Folder |
||
452 | */ |
||
453 | 2 | public function setName($name) |
|
459 | |||
460 | /** |
||
461 | * @param int $lft |
||
462 | * |
||
463 | * @return Folder |
||
464 | */ |
||
465 | 1 | public function setLeft($lft) |
|
471 | |||
472 | /** |
||
473 | * @return int |
||
474 | */ |
||
475 | 1 | public function getLeft() |
|
479 | |||
480 | /** |
||
481 | * @param int $lvl |
||
482 | * |
||
483 | * @return Folder |
||
484 | */ |
||
485 | 2 | public function setLevel($lvl) |
|
491 | |||
492 | /** |
||
493 | * @param int $rgt |
||
494 | * |
||
495 | * @return Folder |
||
496 | */ |
||
497 | 1 | public function setRight($rgt) |
|
503 | |||
504 | /** |
||
505 | * @return int |
||
506 | */ |
||
507 | 1 | public function getRight() |
|
511 | |||
512 | /** |
||
513 | * @return string |
||
514 | */ |
||
515 | 1 | public function getOptionLabel() |
|
522 | |||
523 | /** |
||
524 | * @return int |
||
525 | */ |
||
526 | 2 | public function getLevel() |
|
530 | |||
531 | /** |
||
532 | * @ORM\PreUpdate |
||
533 | */ |
||
534 | 1 | public function preUpdate() |
|
538 | } |
||
539 |
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.