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 |
||
32 | View Code Duplication | class UserPhotos implements Proxy |
|
|
|||
33 | { |
||
34 | /** |
||
35 | * @ORM\ManyToOne(targetEntity="Bukashk0zzz\LiipImagineSerializationBundle\Tests\Fixtures\User", inversedBy="photos") |
||
36 | * @ORM\JoinColumn(name="user_id", referencedColumnName="id") |
||
37 | */ |
||
38 | protected $user; |
||
39 | |||
40 | /** |
||
41 | * @ORM\Column(type="integer") |
||
42 | */ |
||
43 | protected $userId; |
||
44 | |||
45 | /** |
||
46 | * @var string $coverUrl Cover url |
||
47 | * |
||
48 | * @ORM\Column(type="string", length=255) |
||
49 | * |
||
50 | * @JMS\Expose |
||
51 | * @JMS\SerializedName("cover") |
||
52 | * |
||
53 | * @Bukashk0zzz\LiipImagineSerializableField(filter={"big", "small"}) |
||
54 | */ |
||
55 | public $coverUrl; |
||
56 | |||
57 | /** |
||
58 | * @var string $imageUrl Image url |
||
59 | * |
||
60 | * @ORM\Column(type="string", length=255) |
||
61 | * |
||
62 | * @JMS\Expose |
||
63 | * @JMS\SerializedName("image") |
||
64 | * |
||
65 | * @Bukashk0zzz\LiipImagineSerializableField(filter="thumb_filter", virtualField="image_thumb") |
||
66 | */ |
||
67 | public $imageUrl; |
||
68 | |||
69 | /** |
||
70 | * @var string $photoName Photo name |
||
71 | * |
||
72 | * @ORM\Column(type="string", length=255) |
||
73 | * |
||
74 | * @JMS\Expose |
||
75 | * @JMS\SerializedName("photo") |
||
76 | * |
||
77 | * @Bukashk0zzz\LiipImagineSerializableField(filter={"thumb_big", "thumb_small"}, vichUploaderField="photoFile", virtualField="photoThumb") |
||
78 | */ |
||
79 | public $photoName; |
||
80 | |||
81 | /** |
||
82 | * @var File $photoFile Photo file |
||
83 | * |
||
84 | * @JMS\Exclude |
||
85 | * |
||
86 | * @Vich\UploadableField(mapping="user_photo_mapping", fileNameProperty="photoName") |
||
87 | */ |
||
88 | public $photoFile; |
||
89 | |||
90 | /** |
||
91 | * @var bool |
||
92 | */ |
||
93 | private $status = false; |
||
94 | |||
95 | /** |
||
96 | * @inheritdoc |
||
97 | */ |
||
98 | public function __load() |
||
104 | |||
105 | /** |
||
106 | * @inheritdoc |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function __isInitialized() |
||
113 | |||
114 | /** |
||
115 | * To string |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | public function __toString() |
||
123 | |||
124 | /** |
||
125 | * Set userId |
||
126 | * |
||
127 | * @param integer $userId |
||
128 | * |
||
129 | * @return $this |
||
130 | */ |
||
131 | public function setUserId($userId) |
||
137 | |||
138 | /** |
||
139 | * Get userId |
||
140 | * |
||
141 | * @return integer |
||
142 | */ |
||
143 | public function getUserId() |
||
147 | |||
148 | /** |
||
149 | * Set user |
||
150 | * |
||
151 | * @param User $user |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function setUser(User $user = null) |
||
161 | |||
162 | /** |
||
163 | * Get user |
||
164 | * |
||
165 | * @return User |
||
166 | */ |
||
167 | public function getUser() |
||
171 | |||
172 | /** |
||
173 | * @return string |
||
174 | */ |
||
175 | public function getCoverUrl() |
||
179 | |||
180 | /** |
||
181 | * @param string $coverUrl |
||
182 | * @return $this |
||
183 | */ |
||
184 | public function setCoverUrl($coverUrl) |
||
190 | |||
191 | /** |
||
192 | * @return string |
||
193 | */ |
||
194 | public function getImageUrl() |
||
198 | |||
199 | /** |
||
200 | * @param string $imageUrl |
||
201 | * @return $this |
||
202 | */ |
||
203 | public function setImageUrl($imageUrl) |
||
209 | |||
210 | /** |
||
211 | * @return mixed |
||
212 | */ |
||
213 | public function getPhotoName() |
||
217 | |||
218 | /** |
||
219 | * @param mixed $photoName |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function setPhotoName($photoName) |
||
228 | |||
229 | /** |
||
230 | * @return File |
||
231 | */ |
||
232 | public function getPhotoFile() |
||
236 | |||
237 | /** |
||
238 | * @param File $photoFile |
||
239 | * @return $this |
||
240 | */ |
||
241 | public function setPhotoFile($photoFile) |
||
247 | } |
||
248 |
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.