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 |
||
18 | class Registration extends AggregateResource |
||
19 | { |
||
20 | const FOOD_VEGAN = 'vegan'; |
||
21 | |||
22 | const FOOD_VEGETARIAN = 'vegetarian'; |
||
23 | |||
24 | const FOOD_DEFAULT = 'default'; |
||
25 | |||
26 | const TYPE_NORMAL = 1; |
||
27 | |||
28 | const TYPE_VIP = 2; |
||
29 | |||
30 | const TYPE_SPONSOR = 3; |
||
31 | |||
32 | /** |
||
33 | * @var integer |
||
34 | * @ORM\Column(name="id", type="integer") |
||
35 | * @ORM\Id |
||
36 | * @ORM\GeneratedValue(strategy="AUTO") |
||
37 | */ |
||
38 | protected $id; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | * @ORM\Column(type="string", nullable=false) |
||
43 | */ |
||
44 | protected $uuid; |
||
45 | |||
46 | /** |
||
47 | * @Assert\NotBlank() |
||
48 | * @ORM\ManyToOne(targetEntity="BCRM\BackendBundle\Entity\Event\Event", inversedBy="registrations") |
||
49 | * @ORM\JoinColumn(name="event_id", referencedColumnName="id", nullable=false) |
||
50 | * @var Event |
||
51 | */ |
||
52 | protected $event; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | * @Assert\NotBlank() |
||
57 | * @ORM\Column(type="text") |
||
58 | */ |
||
59 | protected $name; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | * @ORM\Column(type="string", nullable=true) |
||
64 | * @Assert\Regex("/^@[a-zA-Z0-9_]{1,15}$/") |
||
65 | */ |
||
66 | protected $twitter; |
||
67 | |||
68 | /** |
||
69 | * @var string |
||
70 | * @Assert\Regex(pattern="/^#[^\s]{1,25}( #[^\s]{1,25}){0,2}$/") |
||
71 | * @ORM\Column(type="text", nullable=true) |
||
72 | */ |
||
73 | protected $tags; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | * @Assert\NotBlank() |
||
78 | * @Assert\Email() |
||
79 | * @ORM\Column(type="string") |
||
80 | */ |
||
81 | protected $email; |
||
82 | |||
83 | /** |
||
84 | * @var boolean |
||
85 | * @Assert\Type(type="boolean") |
||
86 | * @ORM\Column(type="boolean") |
||
87 | */ |
||
88 | protected $saturday = false; |
||
89 | |||
90 | /** |
||
91 | * @var boolean |
||
92 | * @Assert\Type(type="boolean") |
||
93 | * @ORM\Column(type="boolean") |
||
94 | */ |
||
95 | protected $sunday = false; |
||
96 | |||
97 | /** |
||
98 | * @var string |
||
99 | * @ORM\Column(type="string", nullable=true) |
||
100 | */ |
||
101 | protected $food; |
||
102 | |||
103 | /** |
||
104 | * @var boolean |
||
105 | * @Assert\Type(type="boolean") |
||
106 | * @ORM\Column(type="boolean") |
||
107 | */ |
||
108 | protected $participantList = false; |
||
109 | |||
110 | /** |
||
111 | * @var integer |
||
112 | * @Assert\NotBlank() |
||
113 | * @Assert\Type(type="integer") |
||
114 | * @ORM\Column(type="integer") |
||
115 | */ |
||
116 | protected $type = self::TYPE_NORMAL; |
||
117 | |||
118 | /** |
||
119 | * @var int Donation |
||
120 | * @Assert\Range(min=0) |
||
121 | * @ORM\Column(type="integer", nullable=false) |
||
122 | */ |
||
123 | protected $donation = 0; |
||
124 | |||
125 | /** |
||
126 | * @var string Payment method |
||
127 | * @ORM\Column(name="payment_method", type="string", nullable=true) |
||
128 | * @Assert\Choice(choices={"barzahlen.de", "paypal"}) |
||
129 | */ |
||
130 | protected $paymentMethod; |
||
131 | |||
132 | /** |
||
133 | * @ORM\ManyToOne(targetEntity="BCRM\BackendBundle\Entity\Payment") |
||
134 | * @ORM\JoinColumn(name="payment_id", referencedColumnName="id", nullable=true) |
||
135 | * @var Payment |
||
136 | */ |
||
137 | protected $payment; |
||
138 | |||
139 | /** |
||
140 | * @ORM\Column(name="payment_notified", type="datetime", nullable=true) |
||
141 | * @var \DateTime |
||
142 | */ |
||
143 | protected $paymentNotified; |
||
144 | |||
145 | /** |
||
146 | * @Gedmo\Timestampable(on="create") |
||
147 | * @ORM\Column(type="datetime") |
||
148 | * @var \DateTime |
||
149 | */ |
||
150 | protected $created; |
||
151 | |||
152 | /** |
||
153 | * @Gedmo\Timestampable(on="update") |
||
154 | * @ORM\Column(type="datetime", nullable=true) |
||
155 | * @var \DateTime |
||
156 | */ |
||
157 | protected $updated; |
||
158 | |||
159 | /** |
||
160 | * Get id |
||
161 | * |
||
162 | * @return integer |
||
163 | */ |
||
164 | public function getId() |
||
168 | |||
169 | /** |
||
170 | * @param string $uuid |
||
171 | */ |
||
172 | public function setUuid($uuid) |
||
173 | { |
||
174 | $this->uuid = $uuid; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @return string |
||
179 | */ |
||
180 | public function getUuid() |
||
181 | { |
||
182 | return $this->uuid; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @return string |
||
187 | */ |
||
188 | public function getEmail() |
||
192 | |||
193 | /** |
||
194 | * @param string $email |
||
195 | */ |
||
196 | public function setEmail($email) |
||
200 | |||
201 | /** |
||
202 | * @return string |
||
203 | */ |
||
204 | public function getName() |
||
208 | |||
209 | /** |
||
210 | * @param string $name |
||
211 | */ |
||
212 | public function setName($name) |
||
213 | { |
||
214 | $this->name = $name; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @return string |
||
219 | */ |
||
220 | View Code Duplication | public function __toString() |
|
|
|||
221 | { |
||
222 | $str = $this->email; |
||
223 | $days = array(); |
||
224 | if ($this->saturday) { |
||
225 | $days[] = 'SA'; |
||
226 | } |
||
227 | if ($this->sunday) { |
||
228 | $days[] = 'SU'; |
||
229 | } |
||
230 | $str .= ' (' . join('+', $days) . ')'; |
||
231 | return $str; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @return boolean |
||
236 | */ |
||
237 | public function getSunday() |
||
241 | |||
242 | /** |
||
243 | * @param boolean $sunday |
||
244 | */ |
||
245 | public function setSunday($sunday) |
||
249 | |||
250 | /** |
||
251 | * @return boolean |
||
252 | */ |
||
253 | public function getSaturday() |
||
257 | |||
258 | /** |
||
259 | * @param boolean $saturday |
||
260 | */ |
||
261 | public function setSaturday($saturday) |
||
265 | |||
266 | /** |
||
267 | * @return string |
||
268 | */ |
||
269 | public function getFood() |
||
273 | |||
274 | /** |
||
275 | * @param string $food |
||
276 | */ |
||
277 | View Code Duplication | public function setFood($food) |
|
284 | |||
285 | /** |
||
286 | * @param \BCRM\BackendBundle\Entity\Event\Event $event |
||
287 | */ |
||
288 | public function setEvent(Event $event) |
||
292 | |||
293 | /** |
||
294 | * @return Event |
||
295 | */ |
||
296 | public function getEvent() |
||
300 | |||
301 | /** |
||
302 | * @return string |
||
303 | */ |
||
304 | public function getTags() |
||
308 | |||
309 | /** |
||
310 | * @param string $tags |
||
311 | */ |
||
312 | public function setTags($tags) |
||
316 | |||
317 | /** |
||
318 | * @param int $type |
||
319 | */ |
||
320 | View Code Duplication | public function setType($type) |
|
327 | |||
328 | /** |
||
329 | * @return int |
||
330 | */ |
||
331 | public function getType() |
||
335 | |||
336 | /** |
||
337 | * @return string|null |
||
338 | */ |
||
339 | public function getTwitter() |
||
343 | |||
344 | /** |
||
345 | * @param string|null $twitter |
||
346 | */ |
||
347 | public function setTwitter($twitter = null) |
||
351 | |||
352 | /** |
||
353 | * @return boolean |
||
354 | */ |
||
355 | public function isParticipantList() |
||
359 | |||
360 | /** |
||
361 | * @param boolean $participantList |
||
362 | */ |
||
363 | public function setParticipantList($participantList) |
||
367 | |||
368 | /** |
||
369 | * @return mixed |
||
370 | */ |
||
371 | public function getPaymentMethod() |
||
375 | |||
376 | /** |
||
377 | * @param mixed $paymentMethod |
||
378 | */ |
||
379 | public function setPaymentMethod($paymentMethod) |
||
383 | |||
384 | /** |
||
385 | * @return boolean |
||
386 | */ |
||
387 | public function isPaid() |
||
391 | |||
392 | /** |
||
393 | * @param Payment $payment |
||
394 | */ |
||
395 | public function setPayment(Payment $payment) |
||
399 | |||
400 | /** |
||
401 | * @return Payment |
||
402 | */ |
||
403 | public function getPayment() |
||
407 | |||
408 | /** |
||
409 | * @return \DateTime |
||
410 | */ |
||
411 | public function getPaymentNotified() |
||
415 | |||
416 | /** |
||
417 | * @return bool |
||
418 | */ |
||
419 | public function isPaymentNotified() |
||
423 | |||
424 | /** |
||
425 | * @param \DateTime $paymentNotified |
||
426 | */ |
||
427 | public function setPaymentNotified($paymentNotified) |
||
431 | |||
432 | /** |
||
433 | * @return int |
||
434 | */ |
||
435 | public function getDonation() |
||
439 | |||
440 | /** |
||
441 | * @param int $donation |
||
442 | */ |
||
443 | public function setDonation($donation) |
||
447 | } |
||
448 |
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.