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 |
||
59 | class Place extends Offer implements UpdateableWithCdbXmlInterface |
||
60 | { |
||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | private $placeId; |
||
65 | |||
66 | /** |
||
67 | * @var Language |
||
68 | */ |
||
69 | private $mainLanguage; |
||
70 | |||
71 | public function __construct() |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function getAggregateRootId() |
||
87 | |||
88 | /** |
||
89 | * Factory method to create a new Place. |
||
90 | * |
||
91 | * @todo Refactor this method so it can be called create. Currently the |
||
92 | * normal behavior for create is taken by the legacy udb2 logic. |
||
93 | * The PlaceImportedFromUDB2 could be a superclass of Place. |
||
94 | * |
||
95 | * @param string $id |
||
96 | * @param Title $title |
||
97 | * @param EventType $eventType |
||
98 | * @param Address $address |
||
99 | * @param CalendarInterface $calendar |
||
100 | * @param Theme|null $theme |
||
101 | * @param DateTimeImmutable|null $publicationDate |
||
102 | * |
||
103 | * @return self |
||
104 | */ |
||
105 | View Code Duplication | public static function createPlace( |
|
127 | |||
128 | /** |
||
129 | * Apply the place created event. |
||
130 | * @param PlaceCreated $placeCreated |
||
131 | */ |
||
132 | protected function applyPlaceCreated(PlaceCreated $placeCreated) |
||
137 | |||
138 | /** |
||
139 | * Update the facilities. |
||
140 | * |
||
141 | * @param array $facilities |
||
142 | */ |
||
143 | public function updateFacilities(array $facilities) |
||
147 | |||
148 | /** |
||
149 | * Update the major info. |
||
150 | * |
||
151 | * @param Title $title |
||
152 | * @param EventType $eventType |
||
153 | * @param Address $address |
||
154 | * @param CalendarInterface $calendar |
||
155 | * @param Theme $theme |
||
156 | */ |
||
157 | public function updateMajorInfo( |
||
175 | |||
176 | /** |
||
177 | * @param Address $address |
||
178 | * @param Language $language |
||
179 | */ |
||
180 | public function updateAddress(Address $address, Language $language) |
||
190 | |||
191 | /** |
||
192 | * @param Coordinates $coordinates |
||
193 | */ |
||
194 | public function updateGeoCoordinates( |
||
203 | |||
204 | /** |
||
205 | * Import from UDB2. |
||
206 | * |
||
207 | * @param string $actorId |
||
208 | * The actor id. |
||
209 | * @param string $cdbXml |
||
210 | * The cdb xml. |
||
211 | * @param string $cdbXmlNamespaceUri |
||
212 | * The cdb xml namespace uri. |
||
213 | * |
||
214 | * @return Place |
||
215 | */ |
||
216 | public static function importFromUDB2Actor( |
||
232 | |||
233 | /** |
||
234 | * @param PlaceImportedFromUDB2 $placeImported |
||
235 | */ |
||
236 | View Code Duplication | public function applyPlaceImportedFromUDB2( |
|
249 | |||
250 | /** |
||
251 | * @param PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2 |
||
252 | */ |
||
253 | View Code Duplication | public function applyPlaceUpdatedFromUDB2( |
|
264 | |||
265 | /** |
||
266 | * @inheritdoc |
||
267 | */ |
||
268 | public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri) |
||
280 | |||
281 | /** |
||
282 | * @param Label $label |
||
283 | * @return LabelAdded |
||
284 | */ |
||
285 | protected function createLabelAddedEvent(Label $label) |
||
289 | |||
290 | /** |
||
291 | * @param Label $label |
||
292 | * @return LabelRemoved |
||
293 | */ |
||
294 | protected function createLabelRemovedEvent(Label $label) |
||
298 | |||
299 | protected function createImageAddedEvent(Image $image) |
||
303 | |||
304 | protected function createImageRemovedEvent(Image $image) |
||
308 | |||
309 | protected function createImageUpdatedEvent( |
||
319 | |||
320 | protected function createMainImageSelectedEvent(Image $image) |
||
324 | |||
325 | /** |
||
326 | * @param Language $language |
||
327 | * @param StringLiteral $title |
||
328 | * @return TitleTranslated |
||
329 | */ |
||
330 | protected function createTitleTranslatedEvent(Language $language, StringLiteral $title) |
||
334 | |||
335 | /** |
||
336 | * @param Language $language |
||
337 | * @param StringLiteral $description |
||
338 | * @return DescriptionTranslated |
||
339 | */ |
||
340 | protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description) |
||
344 | |||
345 | /** |
||
346 | * @param string $description |
||
347 | * @return DescriptionUpdated |
||
348 | */ |
||
349 | protected function createDescriptionUpdatedEvent($description) |
||
353 | |||
354 | /** |
||
355 | * @param string $typicalAgeRange |
||
356 | * @return TypicalAgeRangeUpdated |
||
357 | */ |
||
358 | protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange) |
||
362 | |||
363 | /** |
||
364 | * @return TypicalAgeRangeDeleted |
||
365 | */ |
||
366 | protected function createTypicalAgeRangeDeletedEvent() |
||
370 | |||
371 | /** |
||
372 | * @param string $organizerId |
||
373 | * @return OrganizerUpdated |
||
374 | */ |
||
375 | protected function createOrganizerUpdatedEvent($organizerId) |
||
379 | |||
380 | /** |
||
381 | * @param string $organizerId |
||
382 | * @return OrganizerDeleted |
||
383 | */ |
||
384 | protected function createOrganizerDeletedEvent($organizerId) |
||
388 | |||
389 | /** |
||
390 | * @param ContactPoint $contactPoint |
||
391 | * @return ContactPointUpdated |
||
392 | */ |
||
393 | protected function createContactPointUpdatedEvent(ContactPoint $contactPoint) |
||
397 | |||
398 | /** |
||
399 | * @param BookingInfo $bookingInfo |
||
400 | * @return BookingInfoUpdated |
||
401 | */ |
||
402 | protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo) |
||
406 | |||
407 | /** |
||
408 | * @param PriceInfo $priceInfo |
||
409 | * @return PriceInfoUpdated |
||
410 | */ |
||
411 | protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo) |
||
415 | |||
416 | /** |
||
417 | * @return PlaceDeleted |
||
418 | */ |
||
419 | protected function createOfferDeletedEvent() |
||
423 | |||
424 | /** |
||
425 | * @inheritDoc |
||
426 | */ |
||
427 | protected function createPublishedEvent(\DateTimeInterface $publicationDate) |
||
431 | |||
432 | /** |
||
433 | * @inheritDoc |
||
434 | */ |
||
435 | protected function createApprovedEvent() |
||
439 | |||
440 | /** |
||
441 | * @inheritDoc |
||
442 | */ |
||
443 | protected function createRejectedEvent(StringLiteral $reason) |
||
447 | |||
448 | /** |
||
449 | * @inheritDoc |
||
450 | */ |
||
451 | protected function createFlaggedAsDuplicate() |
||
455 | |||
456 | /** |
||
457 | * @inheritDoc |
||
458 | */ |
||
459 | protected function createFlaggedAsInappropriate() |
||
463 | |||
464 | /** |
||
465 | * @inheritDoc |
||
466 | * @return ImagesImportedFromUDB2 |
||
467 | */ |
||
468 | protected function createImagesImportedFromUDB2(ImageCollection $images) |
||
472 | |||
473 | /** |
||
474 | * @inheritDoc |
||
475 | * @return ImagesUpdatedFromUDB2 |
||
476 | */ |
||
477 | protected function createImagesUpdatedFromUDB2(ImageCollection $images) |
||
481 | } |
||
482 |
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.