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 |
||
19 | class Ticket extends AggregateResource |
||
20 | { |
||
21 | const DAY_SATURDAY = 1; |
||
22 | |||
23 | const DAY_SUNDAY = 2; |
||
24 | |||
25 | /** |
||
26 | * @var integer |
||
27 | * @ORM\Column(name="id", type="integer") |
||
28 | * @ORM\Id |
||
29 | * @ORM\GeneratedValue(strategy="AUTO") |
||
30 | */ |
||
31 | protected $id; |
||
32 | |||
33 | /** |
||
34 | * @Assert\NotBlank() |
||
35 | * @ORM\ManyToOne(targetEntity="BCRM\BackendBundle\Entity\Event\Event", inversedBy="tickets") |
||
36 | * @ORM\JoinColumn(name="event_id", referencedColumnName="id", nullable=false) |
||
37 | * @var Event |
||
38 | */ |
||
39 | protected $event; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | * @Assert\NotBlank() |
||
44 | * @ORM\Column(type="text") |
||
45 | */ |
||
46 | protected $name; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | * @Assert\NotBlank() |
||
51 | * @Assert\Email() |
||
52 | * @ORM\Column(type="string") |
||
53 | */ |
||
54 | protected $email; |
||
55 | |||
56 | /** |
||
57 | * @var integer |
||
58 | * @Assert\NotBlank() |
||
59 | * @Assert\Type(type="integer") |
||
60 | * @Assert\Range(min=1,max=2) |
||
61 | * @ORM\Column(type="integer") |
||
62 | */ |
||
63 | protected $day; |
||
64 | |||
65 | /** |
||
66 | * @ORM\Column(type="string", nullable=false, name="code") |
||
67 | * @var string Ticket code |
||
68 | */ |
||
69 | protected $code; |
||
70 | |||
71 | /** |
||
72 | * @ORM\ManyToOne(targetEntity="BCRM\BackendBundle\Entity\Payment") |
||
73 | * @ORM\JoinColumn(name="payment_id", referencedColumnName="id", nullable=true) |
||
74 | * @var Payment |
||
75 | */ |
||
76 | protected $payment; |
||
77 | |||
78 | /** |
||
79 | * @var boolean |
||
80 | * @Assert\NotBlank() |
||
81 | * @Assert\Type(type="boolean") |
||
82 | * @ORM\Column(type="boolean") |
||
83 | */ |
||
84 | protected $notified = 0; |
||
85 | |||
86 | /** |
||
87 | * @var boolean |
||
88 | * @ORM\Column(type="datetime", name="checked_in", nullable=true) |
||
89 | * @var \DateTime |
||
90 | * @Assert\Type(type="\DateTime") |
||
91 | */ |
||
92 | protected $checkedIn; |
||
93 | |||
94 | /** |
||
95 | * @var boolean |
||
96 | * @Assert\Type(type="boolean") |
||
97 | * @ORM\Column(type="boolean") |
||
98 | */ |
||
99 | protected $printed = 0; |
||
100 | |||
101 | /** |
||
102 | * @var integer |
||
103 | * @Assert\NotBlank() |
||
104 | * @Assert\Type(type="integer") |
||
105 | * @ORM\Column(type="integer") |
||
106 | */ |
||
107 | protected $type = Registration::TYPE_NORMAL; |
||
108 | |||
109 | /** |
||
110 | * @Gedmo\Timestampable(on="create") |
||
111 | * @ORM\Column(type="datetime") |
||
112 | * @var \DateTime |
||
113 | */ |
||
114 | protected $created; |
||
115 | |||
116 | /** |
||
117 | * @Gedmo\Timestampable(on="update") |
||
118 | * @ORM\Column(type="datetime", nullable=true) |
||
119 | * @var \DateTime |
||
120 | */ |
||
121 | protected $updated; |
||
122 | |||
123 | /** |
||
124 | * Get id |
||
125 | * |
||
126 | * @return integer |
||
127 | */ |
||
128 | public function getId() |
||
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getEmail() |
||
140 | |||
141 | /** |
||
142 | * @param string $email |
||
143 | */ |
||
144 | public function setEmail($email) |
||
145 | { |
||
146 | $this->email = $email; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @return string |
||
151 | */ |
||
152 | View Code Duplication | public function __toString() |
|
164 | |||
165 | /** |
||
166 | * @return bool |
||
167 | */ |
||
168 | public function isSaturday() |
||
172 | |||
173 | /** |
||
174 | * @return int |
||
175 | */ |
||
176 | public function getDay() |
||
180 | |||
181 | /** |
||
182 | * @param int $day |
||
183 | */ |
||
184 | public function setDay($day) |
||
185 | { |
||
186 | if (!in_array($day, array(static::DAY_SATURDAY, static::DAY_SUNDAY))) { |
||
187 | throw new InvalidArgumentException(sprintf('Invalid day: %d', $day)); |
||
188 | } |
||
189 | $this->day = $day; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function isSunday() |
||
199 | |||
200 | /** |
||
201 | * @return \BCRM\BackendBundle\Entity\Event\Event |
||
202 | */ |
||
203 | public function getEvent() |
||
204 | { |
||
205 | return $this->event; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param \BCRM\BackendBundle\Entity\Event\Event $event |
||
210 | */ |
||
211 | public function setEvent(Event $event) |
||
212 | { |
||
213 | $this->event = $event; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return string |
||
218 | */ |
||
219 | public function getCode() |
||
223 | |||
224 | /** |
||
225 | * @param string $code |
||
226 | */ |
||
227 | public function setCode($code) |
||
231 | |||
232 | /** |
||
233 | * @return string |
||
234 | */ |
||
235 | public function getName() |
||
239 | |||
240 | /** |
||
241 | * @param string $name |
||
242 | */ |
||
243 | public function setName($name) |
||
247 | |||
248 | public function isCheckedIn() |
||
252 | |||
253 | /** |
||
254 | * @return \DateTime|null |
||
255 | */ |
||
256 | public function getCheckinTime() |
||
260 | |||
261 | /** |
||
262 | * @param boolean $checkedIn |
||
263 | */ |
||
264 | public function setCheckedIn($checkedIn) |
||
268 | |||
269 | /** |
||
270 | * @param int $type |
||
271 | */ |
||
272 | View Code Duplication | public function setType($type) |
|
279 | |||
280 | /** |
||
281 | * @return int |
||
282 | */ |
||
283 | public function getType() |
||
287 | |||
288 | /** |
||
289 | * @return string |
||
290 | */ |
||
291 | public function getLabel() |
||
301 | |||
302 | /** |
||
303 | * @param boolean $notified |
||
304 | */ |
||
305 | public function setNotified($notified) |
||
309 | |||
310 | /** |
||
311 | * @return bool |
||
312 | */ |
||
313 | public function isNotified() |
||
317 | |||
318 | /** |
||
319 | * @return bool |
||
320 | */ |
||
321 | public function isPrinted() |
||
325 | |||
326 | /** |
||
327 | * @return boolean |
||
328 | */ |
||
329 | public function isPaid() |
||
333 | |||
334 | /** |
||
335 | * @param Payment $payment |
||
336 | */ |
||
337 | public function setPayment(Payment $payment) |
||
341 | |||
342 | /** |
||
343 | * @return Payment |
||
344 | */ |
||
345 | public function getPayment() |
||
349 | } |
||
350 | |||
352 |
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.