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:
Complex classes like Place often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Place, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
64 | class Place extends Offer implements UpdateableWithCdbXmlInterface |
||
65 | { |
||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | private $placeId; |
||
70 | |||
71 | /** |
||
72 | * @var Address[] |
||
73 | */ |
||
74 | private $addresses; |
||
75 | |||
76 | public function __construct() |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function getAggregateRootId() |
||
90 | |||
91 | /** |
||
92 | * Factory method to create a new Place. |
||
93 | * |
||
94 | * @todo Refactor this method so it can be called create. Currently the |
||
95 | * normal behavior for create is taken by the legacy udb2 logic. |
||
96 | * The PlaceImportedFromUDB2 could be a superclass of Place. |
||
97 | * |
||
98 | * @param string $id |
||
99 | * @param Language $mainLanguage |
||
100 | * @param Title $title |
||
101 | * @param EventType $eventType |
||
102 | * @param Address $address |
||
103 | * @param CalendarInterface $calendar |
||
104 | * @param Theme|null $theme |
||
105 | * @param DateTimeImmutable|null $publicationDate |
||
106 | * |
||
107 | * @return self |
||
108 | */ |
||
109 | View Code Duplication | public static function createPlace( |
|
133 | |||
134 | /** |
||
135 | * Apply the place created event. |
||
136 | * @param PlaceCreated $placeCreated |
||
137 | */ |
||
138 | protected function applyPlaceCreated(PlaceCreated $placeCreated) |
||
145 | |||
146 | /** |
||
147 | * Update the major info. |
||
148 | * |
||
149 | * @param Title $title |
||
150 | * @param EventType $eventType |
||
151 | * @param Address $address |
||
152 | * @param CalendarInterface $calendar |
||
153 | * @param Theme $theme |
||
154 | */ |
||
155 | public function updateMajorInfo( |
||
173 | |||
174 | /** |
||
175 | * @param MajorInfoUpdated $majorInfoUpdated |
||
176 | */ |
||
177 | public function applyMajorInfoUpdated(MajorInfoUpdated $majorInfoUpdated) |
||
181 | |||
182 | /** |
||
183 | * @param Address $address |
||
184 | * @param Language $language |
||
185 | */ |
||
186 | public function updateAddress(Address $address, Language $language) |
||
198 | |||
199 | /** |
||
200 | * @param AddressUpdated $addressUpdated |
||
201 | */ |
||
202 | protected function applyAddressUpdated(AddressUpdated $addressUpdated) |
||
206 | |||
207 | /** |
||
208 | * @param AddressTranslated $addressTranslated |
||
209 | */ |
||
210 | protected function applyAddressTranslated(AddressTranslated $addressTranslated) |
||
214 | |||
215 | /** |
||
216 | * @param Address $address |
||
217 | * @param Language $language |
||
218 | * @return bool |
||
219 | */ |
||
220 | private function allowAddressUpdate(Address $address, Language $language) |
||
234 | |||
235 | /** |
||
236 | * Import from UDB2. |
||
237 | * |
||
238 | * @param string $actorId |
||
239 | * The actor id. |
||
240 | * @param string $cdbXml |
||
241 | * The cdb xml. |
||
242 | * @param string $cdbXmlNamespaceUri |
||
243 | * The cdb xml namespace uri. |
||
244 | * |
||
245 | * @return Place |
||
246 | */ |
||
247 | public static function importFromUDB2Actor( |
||
263 | |||
264 | /** |
||
265 | * @param PlaceImportedFromUDB2 $placeImported |
||
266 | */ |
||
267 | View Code Duplication | public function applyPlaceImportedFromUDB2( |
|
292 | |||
293 | /** |
||
294 | * @param PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2 |
||
295 | */ |
||
296 | View Code Duplication | public function applyPlaceUpdatedFromUDB2( |
|
320 | |||
321 | /** |
||
322 | * @inheritdoc |
||
323 | */ |
||
324 | public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri) |
||
336 | |||
337 | /** |
||
338 | * @param Label $label |
||
339 | * @return LabelAdded |
||
340 | */ |
||
341 | protected function createLabelAddedEvent(Label $label) |
||
345 | |||
346 | /** |
||
347 | * @param Label $label |
||
348 | * @return LabelRemoved |
||
349 | */ |
||
350 | protected function createLabelRemovedEvent(Label $label) |
||
354 | |||
355 | protected function createImageAddedEvent(Image $image) |
||
359 | |||
360 | protected function createImageRemovedEvent(Image $image) |
||
364 | |||
365 | protected function createImageUpdatedEvent( |
||
375 | |||
376 | protected function createMainImageSelectedEvent(Image $image) |
||
380 | |||
381 | /** |
||
382 | * @param Language $language |
||
383 | * @param StringLiteral $title |
||
384 | * @return TitleTranslated |
||
385 | */ |
||
386 | protected function createTitleTranslatedEvent(Language $language, StringLiteral $title) |
||
390 | |||
391 | /** |
||
392 | * @param Title $title |
||
393 | * @return TitleUpdated |
||
394 | */ |
||
395 | protected function createTitleUpdatedEvent(Title $title) |
||
399 | |||
400 | /** |
||
401 | * @param Language $language |
||
402 | * @param StringLiteral $description |
||
403 | * @return DescriptionTranslated |
||
404 | */ |
||
405 | protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description) |
||
409 | |||
410 | /** |
||
411 | * @param string $description |
||
412 | * @return DescriptionUpdated |
||
413 | */ |
||
414 | protected function createDescriptionUpdatedEvent($description) |
||
418 | |||
419 | /** |
||
420 | * @inheritdoc |
||
421 | */ |
||
422 | protected function createCalendarUpdatedEvent(Calendar $calendar) |
||
426 | |||
427 | /** |
||
428 | * @param string $typicalAgeRange |
||
429 | * @return TypicalAgeRangeUpdated |
||
430 | */ |
||
431 | protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange) |
||
435 | |||
436 | /** |
||
437 | * @return TypicalAgeRangeDeleted |
||
438 | */ |
||
439 | protected function createTypicalAgeRangeDeletedEvent() |
||
443 | |||
444 | /** |
||
445 | * @param string $organizerId |
||
446 | * @return OrganizerUpdated |
||
447 | */ |
||
448 | protected function createOrganizerUpdatedEvent($organizerId) |
||
452 | |||
453 | /** |
||
454 | * @param string $organizerId |
||
455 | * @return OrganizerDeleted |
||
456 | */ |
||
457 | protected function createOrganizerDeletedEvent($organizerId) |
||
461 | |||
462 | /** |
||
463 | * @param ContactPoint $contactPoint |
||
464 | * @return ContactPointUpdated |
||
465 | */ |
||
466 | protected function createContactPointUpdatedEvent(ContactPoint $contactPoint) |
||
470 | |||
471 | /** |
||
472 | * @inheritdoc |
||
473 | */ |
||
474 | protected function createGeoCoordinatesUpdatedEvent(Coordinates $coordinates) |
||
478 | |||
479 | /** |
||
480 | * @param BookingInfo $bookingInfo |
||
481 | * @return BookingInfoUpdated |
||
482 | */ |
||
483 | protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo) |
||
487 | |||
488 | /** |
||
489 | * @param PriceInfo $priceInfo |
||
490 | * @return PriceInfoUpdated |
||
491 | */ |
||
492 | protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo) |
||
496 | |||
497 | /** |
||
498 | * @return PlaceDeleted |
||
499 | */ |
||
500 | protected function createOfferDeletedEvent() |
||
504 | |||
505 | /** |
||
506 | * @inheritDoc |
||
507 | */ |
||
508 | protected function createPublishedEvent(\DateTimeInterface $publicationDate) |
||
512 | |||
513 | /** |
||
514 | * @inheritDoc |
||
515 | */ |
||
516 | protected function createApprovedEvent() |
||
520 | |||
521 | /** |
||
522 | * @inheritDoc |
||
523 | */ |
||
524 | protected function createRejectedEvent(StringLiteral $reason) |
||
528 | |||
529 | /** |
||
530 | * @inheritDoc |
||
531 | */ |
||
532 | protected function createFlaggedAsDuplicate() |
||
536 | |||
537 | /** |
||
538 | * @inheritDoc |
||
539 | */ |
||
540 | protected function createFlaggedAsInappropriate() |
||
544 | |||
545 | /** |
||
546 | * @inheritDoc |
||
547 | * @return ImagesImportedFromUDB2 |
||
548 | */ |
||
549 | protected function createImagesImportedFromUDB2(ImageCollection $images) |
||
553 | |||
554 | /** |
||
555 | * @inheritDoc |
||
556 | * @return ImagesUpdatedFromUDB2 |
||
557 | */ |
||
558 | protected function createImagesUpdatedFromUDB2(ImageCollection $images) |
||
562 | |||
563 | protected function createTypeUpdatedEvent(EventType $type) |
||
567 | |||
568 | protected function createThemeUpdatedEvent(Theme $theme) |
||
572 | |||
573 | /** |
||
574 | * @inheritdoc |
||
575 | */ |
||
576 | protected function createFacilitiesUpdatedEvent(array $facilities) |
||
580 | } |
||
581 |
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.