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 | class Card |
||
33 | { |
||
34 | /** |
||
35 | * @var int |
||
36 | * |
||
37 | * @ORM\Column(name="id", type="integer", nullable=false) |
||
38 | * @ORM\Id |
||
39 | * @ORM\GeneratedValue(strategy="IDENTITY") |
||
40 | */ |
||
41 | private $id; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | * |
||
46 | * @Gedmo\Slug(fields={"title"}, separator="-") |
||
47 | * @ORM\Column(name="slug", type="string", length=100, nullable=false, unique=true) |
||
48 | */ |
||
49 | private $slug; |
||
50 | |||
51 | /** |
||
52 | * @var bool |
||
53 | * |
||
54 | * @ORM\Column(name="active", type="boolean", nullable=false) |
||
55 | */ |
||
56 | private $active = false; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | * |
||
61 | * @ORM\Column(name="title", type="string", length=255, nullable=false, unique=true) |
||
62 | * @Assert\NotBlank() |
||
63 | * @Assert\Length( |
||
64 | * min = "5", |
||
65 | * max = "255", |
||
66 | * minMessage = "Card title must be at least {{ limit }} characters length", |
||
67 | * maxMessage = "Card title cannot be longer than {{ limit }} characters length" |
||
68 | * ) |
||
69 | */ |
||
70 | private $title; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | * |
||
75 | * @ORM\Column(name="content", type="text", nullable=false) |
||
76 | * @Assert\NotBlank() |
||
77 | */ |
||
78 | private $content; |
||
79 | |||
80 | /** |
||
81 | * @var \DateTime |
||
82 | * |
||
83 | * @ORM\Column(name="created", type="datetime", nullable=false) |
||
84 | */ |
||
85 | private $created; |
||
86 | |||
87 | /** |
||
88 | * @var \DateTime |
||
89 | * |
||
90 | * @ORM\Column(name="updated", type="datetime", nullable=false) |
||
91 | */ |
||
92 | private $updated; |
||
93 | |||
94 | /** |
||
95 | * @var string |
||
96 | * |
||
97 | * @ORM\Column(name="meta_keywords", type="string", length=255, nullable=true) |
||
98 | */ |
||
99 | private $metaKeywords; |
||
100 | |||
101 | /** |
||
102 | * @var string |
||
103 | * |
||
104 | * @ORM\Column(name="meta_description", type="string", length=255, nullable=true) |
||
105 | */ |
||
106 | private $metaDescription; |
||
107 | |||
108 | /** |
||
109 | * @var int |
||
110 | * |
||
111 | * @ORM\Column(name="views", type="integer", nullable=false) |
||
112 | */ |
||
113 | private $views = '0'; |
||
114 | |||
115 | /** |
||
116 | * @var Category |
||
117 | * |
||
118 | * @ORM\ManyToOne(targetEntity="Category", inversedBy="cards", fetch="EAGER") |
||
119 | * @ORM\JoinColumns({ |
||
120 | * @ORM\JoinColumn(name="category_id", referencedColumnName="id") |
||
121 | * }) |
||
122 | * @Assert\NotBlank() |
||
123 | */ |
||
124 | private $category; |
||
125 | |||
126 | /** |
||
127 | * @ORM\OneToMany(targetEntity="CardView", mappedBy="card") |
||
128 | */ |
||
129 | private $cardViews; |
||
130 | |||
131 | 10 | public function __construct() |
|
135 | |||
136 | /** |
||
137 | * Get id |
||
138 | * |
||
139 | * @return int |
||
140 | */ |
||
141 | 3 | public function getId() |
|
145 | |||
146 | /** |
||
147 | * Set slug |
||
148 | * |
||
149 | * @param string $slug |
||
150 | * |
||
151 | * @return Card |
||
152 | */ |
||
153 | 1 | public function setSlug($slug) |
|
154 | { |
||
155 | 1 | $this->slug = $slug; |
|
156 | |||
157 | 1 | return $this; |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * Get slug |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 4 | public function getSlug() |
|
169 | |||
170 | /** |
||
171 | * Set isActive |
||
172 | * |
||
173 | * @param bool $isActive |
||
174 | * |
||
175 | * @return Card |
||
176 | */ |
||
177 | 7 | public function setActive($isActive) |
|
178 | { |
||
179 | 7 | $this->active = $isActive; |
|
180 | |||
181 | 7 | return $this; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Get isActive |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | 5 | public function isActive() |
|
193 | |||
194 | /** |
||
195 | * Set title |
||
196 | * |
||
197 | * @param string $title |
||
198 | * |
||
199 | * @return Card |
||
200 | */ |
||
201 | 9 | public function setTitle($title) |
|
202 | { |
||
203 | 9 | $this->title = $title; |
|
204 | |||
205 | 9 | return $this; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Get title |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | 4 | public function getTitle() |
|
217 | |||
218 | /** |
||
219 | * Set content |
||
220 | * |
||
221 | * @param string $content |
||
222 | * |
||
223 | * @return Card |
||
224 | */ |
||
225 | 8 | public function setContent($content) |
|
226 | { |
||
227 | 8 | $this->content = $content; |
|
228 | |||
229 | 8 | return $this; |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * Get content |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | 4 | public function getContent() |
|
241 | |||
242 | /** |
||
243 | * Set created |
||
244 | * |
||
245 | * @param \DateTime $created |
||
246 | * |
||
247 | * @return Card |
||
248 | */ |
||
249 | 5 | View Code Duplication | public function setCreated($created = null) |
|
|||
250 | { |
||
251 | 5 | if ($created === null) { |
|
252 | 5 | $created = new \DateTime(); |
|
253 | 5 | $this->setUpdated($created); |
|
254 | } |
||
255 | 5 | $this->created = $created; |
|
256 | |||
257 | 5 | return $this; |
|
258 | } |
||
259 | |||
260 | /** |
||
261 | * Get created |
||
262 | * |
||
263 | * @return \DateTime |
||
264 | */ |
||
265 | 1 | public function getCreated() |
|
269 | |||
270 | /** |
||
271 | * Set updated |
||
272 | * |
||
273 | * @param \DateTime $updated |
||
274 | * |
||
275 | * @return Card |
||
276 | */ |
||
277 | 5 | View Code Duplication | public function setUpdated($updated = null) |
278 | { |
||
279 | 5 | if ($updated === null) { |
|
280 | $updated = new \DateTime(); |
||
281 | } |
||
282 | 5 | $this->updated = $updated; |
|
283 | |||
284 | 5 | return $this; |
|
285 | } |
||
286 | |||
287 | /** |
||
288 | * Get updated |
||
289 | * |
||
290 | * @return \DateTime |
||
291 | */ |
||
292 | 4 | public function getUpdated() |
|
296 | |||
297 | /** |
||
298 | * Set meta keywords |
||
299 | * |
||
300 | * @param string $keywords |
||
301 | * |
||
302 | * @return Card |
||
303 | */ |
||
304 | 5 | public function setMetaKeywords($keywords) |
|
305 | { |
||
306 | 5 | $this->metaKeywords = $keywords; |
|
307 | |||
308 | 5 | return $this; |
|
309 | } |
||
310 | |||
311 | /** |
||
312 | * Get Meta keywords |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | 2 | public function getMetaKeywords() |
|
320 | |||
321 | /** |
||
322 | * Set meta description |
||
323 | * |
||
324 | * @param string $description |
||
325 | * |
||
326 | * @return Card |
||
327 | */ |
||
328 | 5 | public function setMetaDescription($description) |
|
329 | { |
||
330 | 5 | $this->metaDescription = $description; |
|
331 | |||
332 | 5 | return $this; |
|
333 | } |
||
334 | |||
335 | /** |
||
336 | * Get meta description |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | 1 | public function getMetaDescription() |
|
344 | |||
345 | /** |
||
346 | * Set views |
||
347 | * |
||
348 | * @param int $views |
||
349 | * |
||
350 | * @return Card |
||
351 | */ |
||
352 | 5 | public function setViews($views) |
|
353 | { |
||
354 | 5 | $this->views = $views; |
|
355 | |||
356 | 5 | return $this; |
|
357 | } |
||
358 | |||
359 | /** |
||
360 | * Get views |
||
361 | * |
||
362 | * @return int |
||
363 | */ |
||
364 | 3 | public function getViews() |
|
368 | |||
369 | /** |
||
370 | * Set category |
||
371 | * |
||
372 | * @param Category $category |
||
373 | * |
||
374 | * @return Card |
||
375 | */ |
||
376 | 5 | public function setCategory(Category $category = null) |
|
377 | { |
||
378 | 5 | $this->category = $category; |
|
379 | |||
380 | 5 | return $this; |
|
381 | } |
||
382 | |||
383 | /** |
||
384 | * Get category |
||
385 | * |
||
386 | * @return Category |
||
387 | */ |
||
388 | 5 | public function getCategory() |
|
392 | |||
393 | /** |
||
394 | * @ORM\PrePersist |
||
395 | */ |
||
396 | public function prePersist() |
||
400 | |||
401 | /** |
||
402 | * @ORM\PreUpdate |
||
403 | */ |
||
404 | public function preUpdate() |
||
408 | |||
409 | public function __toString() |
||
413 | } |
||
414 |
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.