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 |
||
54 | class Place extends Offer implements UpdateableWithCdbXmlInterface |
||
55 | { |
||
56 | /** |
||
57 | * The actor id. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $actorId; |
||
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | public function getAggregateRootId() |
||
70 | |||
71 | /** |
||
72 | * Factory method to create a new Place. |
||
73 | * |
||
74 | * @todo Refactor this method so it can be called create. Currently the |
||
75 | * normal behavior for create is taken by the legacy udb2 logic. |
||
76 | * The PlaceImportedFromUDB2 could be a superclass of Place. |
||
77 | * |
||
78 | * @param string $id |
||
79 | * @param Title $title |
||
80 | * @param EventType $eventType |
||
81 | * @param Address $address |
||
82 | * @param CalendarInterface $calendar |
||
83 | * @param Theme|null $theme |
||
84 | * @param DateTimeImmutable|null $publicationDate |
||
85 | * |
||
86 | * @return self |
||
87 | */ |
||
88 | public static function createPlace( |
||
110 | |||
111 | /** |
||
112 | * Apply the place created event. |
||
113 | * @param PlaceCreated $placeCreated |
||
114 | */ |
||
115 | protected function applyPlaceCreated(PlaceCreated $placeCreated) |
||
120 | |||
121 | /** |
||
122 | * Update the facilities. |
||
123 | * |
||
124 | * @param array $facilities |
||
125 | */ |
||
126 | public function updateFacilities(array $facilities) |
||
130 | |||
131 | /** |
||
132 | * Update the major info. |
||
133 | * |
||
134 | * @param Title $title |
||
135 | * @param EventType $eventType |
||
136 | * @param Address $address |
||
137 | * @param CalendarInterface $calendar |
||
138 | * @param Theme $theme |
||
139 | */ |
||
140 | public function updateMajorInfo( |
||
149 | |||
150 | /** |
||
151 | * Import from UDB2. |
||
152 | * |
||
153 | * @param string $actorId |
||
154 | * The actor id. |
||
155 | * @param string $cdbXml |
||
156 | * The cdb xml. |
||
157 | * @param string $cdbXmlNamespaceUri |
||
158 | * The cdb xml namespace uri. |
||
159 | * |
||
160 | * @return Place |
||
161 | */ |
||
162 | public static function importFromUDB2Actor( |
||
178 | |||
179 | /** |
||
180 | * Import from UDB2. |
||
181 | * |
||
182 | * @param string $placeId |
||
183 | * The actor id. |
||
184 | * @param string $cdbXml |
||
185 | * The cdb xml. |
||
186 | * @param string $cdbXmlNamespaceUri |
||
187 | * The cdb xml namespace uri. |
||
188 | * |
||
189 | * @return Place |
||
190 | */ |
||
191 | public static function importFromUDB2Event( |
||
207 | |||
208 | /** |
||
209 | * @param PlaceImportedFromUDB2 $placeImported |
||
210 | */ |
||
211 | View Code Duplication | public function applyPlaceImportedFromUDB2( |
|
224 | |||
225 | /** |
||
226 | * @param PlaceImportedFromUDB2Event $placeImported |
||
227 | */ |
||
228 | public function applyPlaceImportedFromUDB2Event( |
||
241 | |||
242 | /** |
||
243 | * @param PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2 |
||
244 | */ |
||
245 | public function applyPlaceUpdatedFromUDB2Event( |
||
256 | |||
257 | /** |
||
258 | * @inheritdoc |
||
259 | */ |
||
260 | public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri) |
||
282 | |||
283 | /** |
||
284 | * @param Label $label |
||
285 | * @return LabelAdded |
||
286 | */ |
||
287 | protected function createLabelAddedEvent(Label $label) |
||
291 | |||
292 | /** |
||
293 | * @param Label $label |
||
294 | * @return LabelDeleted |
||
295 | */ |
||
296 | protected function createLabelDeletedEvent(Label $label) |
||
300 | |||
301 | protected function createImageAddedEvent(Image $image) |
||
305 | |||
306 | protected function createImageRemovedEvent(Image $image) |
||
310 | |||
311 | protected function createImageUpdatedEvent( |
||
321 | |||
322 | protected function createMainImageSelectedEvent(Image $image) |
||
326 | |||
327 | /** |
||
328 | * @param Language $language |
||
329 | * @param StringLiteral $title |
||
330 | * @return TitleTranslated |
||
331 | */ |
||
332 | protected function createTitleTranslatedEvent(Language $language, StringLiteral $title) |
||
336 | |||
337 | /** |
||
338 | * @param Language $language |
||
339 | * @param StringLiteral $description |
||
340 | * @return DescriptionTranslated |
||
341 | */ |
||
342 | protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description) |
||
346 | |||
347 | /** |
||
348 | * @param string $description |
||
349 | * @return DescriptionUpdated |
||
350 | */ |
||
351 | protected function createDescriptionUpdatedEvent($description) |
||
355 | |||
356 | /** |
||
357 | * @param string $typicalAgeRange |
||
358 | * @return TypicalAgeRangeUpdated |
||
359 | */ |
||
360 | protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange) |
||
364 | |||
365 | /** |
||
366 | * @return TypicalAgeRangeDeleted |
||
367 | */ |
||
368 | protected function createTypicalAgeRangeDeletedEvent() |
||
372 | |||
373 | /** |
||
374 | * @param string $organizerId |
||
375 | * @return OrganizerUpdated |
||
376 | */ |
||
377 | protected function createOrganizerUpdatedEvent($organizerId) |
||
381 | |||
382 | /** |
||
383 | * @param string $organizerId |
||
384 | * @return OrganizerDeleted |
||
385 | */ |
||
386 | protected function createOrganizerDeletedEvent($organizerId) |
||
390 | |||
391 | /** |
||
392 | * @param ContactPoint $contactPoint |
||
393 | * @return ContactPointUpdated |
||
394 | */ |
||
395 | protected function createContactPointUpdatedEvent(ContactPoint $contactPoint) |
||
399 | |||
400 | /** |
||
401 | * @param BookingInfo $bookingInfo |
||
402 | * @return BookingInfoUpdated |
||
403 | */ |
||
404 | protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo) |
||
408 | |||
409 | /** |
||
410 | * @param PriceInfo $priceInfo |
||
411 | * @return PriceInfoUpdated |
||
412 | */ |
||
413 | protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo) |
||
417 | |||
418 | /** |
||
419 | * @return PlaceDeleted |
||
420 | */ |
||
421 | protected function createOfferDeletedEvent() |
||
425 | |||
426 | /** |
||
427 | * @inheritDoc |
||
428 | */ |
||
429 | protected function createPublishedEvent(\DateTimeInterface $publicationDate) |
||
433 | |||
434 | /** |
||
435 | * @inheritDoc |
||
436 | */ |
||
437 | protected function createApprovedEvent() |
||
441 | |||
442 | /** |
||
443 | * @inheritDoc |
||
444 | */ |
||
445 | protected function createRejectedEvent(StringLiteral $reason) |
||
449 | |||
450 | /** |
||
451 | * @inheritDoc |
||
452 | */ |
||
453 | protected function createFlaggedAsDuplicate() |
||
457 | |||
458 | /** |
||
459 | * @inheritDoc |
||
460 | */ |
||
461 | protected function createFlaggedAsInappropriate() |
||
465 | } |
||
466 |
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.