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 |
||
17 | class Unregistration extends AggregateResource |
||
18 | { |
||
19 | /** |
||
20 | * @var integer |
||
21 | * @ORM\Column(name="id", type="integer") |
||
22 | * @ORM\Id |
||
23 | * @ORM\GeneratedValue(strategy="AUTO") |
||
24 | */ |
||
25 | protected $id; |
||
26 | |||
27 | /** |
||
28 | * @Assert\NotBlank() |
||
29 | * @ORM\ManyToOne(targetEntity="BCRM\BackendBundle\Entity\Event\Event", inversedBy="unregistrations") |
||
30 | * @ORM\JoinColumn(name="event_id", referencedColumnName="id", nullable=false) |
||
31 | * @var Event |
||
32 | */ |
||
33 | protected $event; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | * @Assert\NotBlank() |
||
38 | * @Assert\Email() |
||
39 | * @ORM\Column(type="string") |
||
40 | */ |
||
41 | protected $email; |
||
42 | |||
43 | /** |
||
44 | * @var boolean |
||
45 | * @Assert\NotBlank() |
||
46 | * @Assert\Type(type="boolean") |
||
47 | * @ORM\Column(type="boolean") |
||
48 | */ |
||
49 | protected $saturday = false; |
||
50 | |||
51 | /** |
||
52 | * @var boolean |
||
53 | * @Assert\NotBlank() |
||
54 | * @Assert\Type(type="boolean") |
||
55 | * @ORM\Column(type="boolean") |
||
56 | */ |
||
57 | protected $sunday = false; |
||
58 | |||
59 | /** |
||
60 | * @ORM\Column(type="string", nullable=true, name="confirmation_key") |
||
61 | * @var string Login-Key |
||
62 | */ |
||
63 | protected $confirmationKey; |
||
64 | |||
65 | /** |
||
66 | * @var boolean |
||
67 | * @Assert\NotBlank() |
||
68 | * @Assert\Type(type="boolean") |
||
69 | * @ORM\Column(type="boolean") |
||
70 | */ |
||
71 | protected $confirmed = false; |
||
72 | |||
73 | /** |
||
74 | * @var boolean |
||
75 | * @Assert\NotBlank() |
||
76 | * @Assert\Type(type="boolean") |
||
77 | * @ORM\Column(type="boolean") |
||
78 | */ |
||
79 | protected $processed = false; |
||
80 | |||
81 | /** |
||
82 | * @Gedmo\Timestampable(on="create") |
||
83 | * @ORM\Column(type="datetime") |
||
84 | * @var \DateTime |
||
85 | */ |
||
86 | protected $created; |
||
87 | |||
88 | /** |
||
89 | * @Gedmo\Timestampable(on="update") |
||
90 | * @ORM\Column(type="datetime", nullable=true) |
||
91 | * @var \DateTime |
||
92 | */ |
||
93 | protected $updated; |
||
94 | |||
95 | /** |
||
96 | * Get id |
||
97 | * |
||
98 | * @return integer |
||
99 | */ |
||
100 | public function getId() |
||
104 | |||
105 | /** |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getEmail() |
||
112 | |||
113 | /** |
||
114 | * @param string $email |
||
115 | */ |
||
116 | public function setEmail($email) |
||
120 | |||
121 | /** |
||
122 | * @return string |
||
123 | */ |
||
124 | public function getConfirmationKey() |
||
128 | |||
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | View Code Duplication | public function __toString() |
|
145 | |||
146 | /** |
||
147 | * @return boolean |
||
148 | */ |
||
149 | public function getSaturday() |
||
153 | |||
154 | /** |
||
155 | * @param boolean $saturday |
||
156 | */ |
||
157 | public function setSaturday($saturday) |
||
158 | { |
||
159 | $this->saturday = (bool)$saturday; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return boolean |
||
164 | */ |
||
165 | public function getSunday() |
||
169 | |||
170 | /** |
||
171 | * @param boolean $sunday |
||
172 | */ |
||
173 | public function setSunday($sunday) |
||
177 | |||
178 | /** |
||
179 | * @param boolean $confirmed |
||
180 | */ |
||
181 | public function setConfirmed($confirmed) |
||
185 | |||
186 | /** |
||
187 | * @param \BCRM\BackendBundle\Entity\Event\Event $event |
||
188 | */ |
||
189 | public function setEvent(Event $event) |
||
193 | |||
194 | /** |
||
195 | * @return \DateTime |
||
196 | */ |
||
197 | public function getCreated() |
||
201 | |||
202 | } |
||
203 | |||
204 |
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.