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:
1 | <?php |
||
30 | View Code Duplication | class UserA |
|
|
|||
31 | { |
||
32 | /** |
||
33 | * @var string $photoName Photo name |
||
34 | * |
||
35 | * @ORM\Column(type="string", length=255) |
||
36 | * |
||
37 | * @JMS\Expose |
||
38 | * @JMS\SerializedName("photo") |
||
39 | * |
||
40 | * @Fresh\VichSerializableField("photoFile") |
||
41 | */ |
||
42 | private $photoName; |
||
43 | |||
44 | /** |
||
45 | * @var File $photoFile Photo file |
||
46 | * |
||
47 | * @JMS\Exclude |
||
48 | * |
||
49 | * @Vich\UploadableField(mapping="user_photo_mapping", fileNameProperty="photoName") |
||
50 | */ |
||
51 | private $photoFile; |
||
52 | |||
53 | /** |
||
54 | * @var string $coverName Cover name |
||
55 | * |
||
56 | * @ORM\Column(type="string", length=255) |
||
57 | * |
||
58 | * @JMS\Expose |
||
59 | * @JMS\SerializedName("cover") |
||
60 | * |
||
61 | * @Fresh\VichSerializableField("coverFile") |
||
62 | */ |
||
63 | private $coverName; |
||
64 | |||
65 | /** |
||
66 | * @var File $coverFile Cover file |
||
67 | * |
||
68 | * @JMS\Exclude |
||
69 | * |
||
70 | * @Vich\UploadableField(mapping="user_cover_mapping", fileNameProperty="coverName") |
||
71 | */ |
||
72 | private $coverFile; |
||
73 | |||
74 | /** |
||
75 | * To string |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | public function __toString() |
||
85 | |||
86 | /** |
||
87 | * Get photo name |
||
88 | * |
||
89 | * @return string Photo name |
||
90 | */ |
||
91 | public function getPhotoName() |
||
95 | |||
96 | /** |
||
97 | * Set photo name |
||
98 | * |
||
99 | * @param string $photoName Photo name |
||
100 | * |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function setPhotoName($photoName) |
||
109 | |||
110 | /** |
||
111 | * Get photo file |
||
112 | * |
||
113 | * @return File Photo file |
||
114 | */ |
||
115 | public function getPhotoFile() |
||
119 | |||
120 | /** |
||
121 | * Set photo file |
||
122 | * |
||
123 | * @param File $photoFile Photo file |
||
124 | * |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function setPhotoFile(File $photoFile) |
||
133 | |||
134 | /** |
||
135 | * Get cover name |
||
136 | * |
||
137 | * @return string Cover name |
||
138 | */ |
||
139 | public function getCoverName() |
||
143 | |||
144 | /** |
||
145 | * Set cover name |
||
146 | * |
||
147 | * @param string $coverName Cover name |
||
148 | * |
||
149 | * @return $this |
||
150 | */ |
||
151 | public function setCoverName($coverName) |
||
157 | |||
158 | /** |
||
159 | * Get cover file |
||
160 | * |
||
161 | * @return File Cover file |
||
162 | */ |
||
163 | public function getCoverFile() |
||
167 | |||
168 | /** |
||
169 | * Set cover file |
||
170 | * |
||
171 | * @param File $coverFile Cover file |
||
172 | * |
||
173 | * @return $this |
||
174 | */ |
||
175 | public function setCoverFile(File $coverFile) |
||
181 | } |
||
182 |
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.