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 |
||
24 | class Organizer extends EventSourcedAggregateRoot implements UpdateableWithCdbXmlInterface |
||
25 | { |
||
26 | /** |
||
27 | * The actor id. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $actorId; |
||
32 | |||
33 | /** |
||
34 | * @var Address|null |
||
35 | */ |
||
36 | private $address; |
||
37 | |||
38 | /** |
||
39 | * @var ContactPoint |
||
40 | */ |
||
41 | private $contactPoint; |
||
42 | |||
43 | /** |
||
44 | * @var Label[] |
||
45 | */ |
||
46 | private $labels = []; |
||
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | public function getAggregateRootId() |
||
55 | |||
56 | public function __construct() |
||
64 | |||
65 | /** |
||
66 | * Import from UDB2. |
||
67 | * |
||
68 | * @param string $actorId |
||
69 | * The actor id. |
||
70 | * @param string $cdbXml |
||
71 | * The cdb xml. |
||
72 | * @param string $cdbXmlNamespaceUri |
||
73 | * The cdb xml namespace uri. |
||
74 | * |
||
75 | * @return Organizer |
||
76 | * The actor. |
||
77 | */ |
||
78 | public static function importFromUDB2( |
||
94 | |||
95 | /** |
||
96 | * Factory method to create a new Organizer. |
||
97 | * |
||
98 | * @param string $id |
||
99 | * @param Url $website |
||
100 | * @param Title $title |
||
101 | * @return Organizer |
||
102 | */ |
||
103 | public static function create( |
||
116 | |||
117 | /** |
||
118 | * @inheritdoc |
||
119 | */ |
||
120 | public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri) |
||
130 | |||
131 | /** |
||
132 | * @param Address $address |
||
133 | */ |
||
134 | public function updateAddress(Address $address) |
||
142 | |||
143 | /** |
||
144 | * @param ContactPoint $contactPoint |
||
145 | */ |
||
146 | public function updateContactPoint(ContactPoint $contactPoint) |
||
154 | |||
155 | /** |
||
156 | * @param Label $label |
||
157 | */ |
||
158 | public function addLabel(Label $label) |
||
164 | |||
165 | /** |
||
166 | * @param Label $label |
||
167 | */ |
||
168 | public function removeLabel(Label $label) |
||
174 | |||
175 | public function delete() |
||
181 | |||
182 | /** |
||
183 | * Apply the organizer created event. |
||
184 | * @param OrganizerCreated $organizerCreated |
||
185 | */ |
||
186 | protected function applyOrganizerCreated(OrganizerCreated $organizerCreated) |
||
190 | |||
191 | /** |
||
192 | * Apply the organizer created event. |
||
193 | * @param OrganizerCreatedWithUniqueWebsite $organizerCreated |
||
194 | */ |
||
195 | protected function applyOrganizerCreatedWithUniqueWebsite(OrganizerCreatedWithUniqueWebsite $organizerCreated) |
||
199 | |||
200 | /** |
||
201 | * @param OrganizerImportedFromUDB2 $organizerImported |
||
202 | */ |
||
203 | View Code Duplication | protected function applyOrganizerImportedFromUDB2( |
|
215 | |||
216 | /** |
||
217 | * @param OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2 |
||
218 | */ |
||
219 | protected function applyOrganizerUpdatedFromUDB2( |
||
229 | |||
230 | /** |
||
231 | * @param AddressUpdated $addressUpdated |
||
232 | */ |
||
233 | protected function applyAddressUpdated(AddressUpdated $addressUpdated) |
||
237 | |||
238 | /** |
||
239 | * @param ContactPointUpdated $contactPointUpdated |
||
240 | */ |
||
241 | protected function applyContactPointUpdated(ContactPointUpdated $contactPointUpdated) |
||
245 | |||
246 | /** |
||
247 | * @param LabelAdded $labelAdded |
||
248 | */ |
||
249 | protected function applyLabelAdded(LabelAdded $labelAdded) |
||
253 | |||
254 | /** |
||
255 | * @param LabelRemoved $labelRemoved |
||
256 | */ |
||
257 | protected function applyLabelRemoved(LabelRemoved $labelRemoved) |
||
262 | |||
263 | /** |
||
264 | * @param \CultureFeed_Cdb_Item_Base $udb2Item |
||
265 | */ |
||
266 | View Code Duplication | protected function setLabelsFromUDB2Item(\CultureFeed_Cdb_Item_Base $udb2Item) |
|
278 | } |
||
279 |
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.