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 Event 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 Event, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
57 | class Event extends Offer implements UpdateableWithCdbXmlInterface |
||
58 | { |
||
59 | protected $eventId; |
||
60 | |||
61 | /** |
||
62 | * @var Audience |
||
63 | */ |
||
64 | private $audience; |
||
65 | |||
66 | const MAIN_LANGUAGE_CODE = 'nl'; |
||
67 | |||
68 | public function __construct() |
||
72 | |||
73 | /** |
||
74 | * Factory method to create a new event. |
||
75 | * |
||
76 | * @param $eventId |
||
77 | * @param Title $title |
||
78 | * @param EventType $eventType |
||
79 | * @param Location $location |
||
80 | * @param CalendarInterface $calendar |
||
81 | * @param Theme|null $theme |
||
82 | * @param \DateTimeImmutable|null $publicationDate |
||
83 | * @return Event |
||
84 | */ |
||
85 | View Code Duplication | public static function create( |
|
110 | |||
111 | /** |
||
112 | * @param string $eventId |
||
113 | * @param string $originalEventId |
||
114 | * @param CalendarInterface $calendar |
||
115 | * @return Event |
||
116 | */ |
||
117 | public static function copyEvent( |
||
134 | |||
135 | /** |
||
136 | * @param string $eventId |
||
137 | * @param string $cdbXml |
||
138 | * @param string $cdbXmlNamespaceUri |
||
139 | * @return Event |
||
140 | */ |
||
141 | public static function importFromUDB2( |
||
157 | |||
158 | /** |
||
159 | * @param ImageCollection $images |
||
160 | */ |
||
161 | public function updateImagesFromUDB2(ImageCollection $images) |
||
165 | |||
166 | /** |
||
167 | * @param ImageCollection $images |
||
168 | */ |
||
169 | public function importImagesFromUDB2(ImageCollection $images) |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | public function getAggregateRootId() |
||
181 | |||
182 | /** |
||
183 | * @return UUID[] |
||
184 | */ |
||
185 | public function getMediaObjects() |
||
189 | |||
190 | protected function applyEventCreated(EventCreated $eventCreated) |
||
195 | |||
196 | /** |
||
197 | * @param EventCopied $eventCopied |
||
198 | */ |
||
199 | protected function applyEventCopied(EventCopied $eventCopied) |
||
204 | |||
205 | protected function applyEventImportedFromUDB2( |
||
211 | |||
212 | /** |
||
213 | * @param EventUpdatedFromUDB2 $eventUpdated |
||
214 | */ |
||
215 | protected function applyEventUpdatedFromUDB2( |
||
220 | |||
221 | /** |
||
222 | * @param EventCdbXMLInterface $eventCdbXML |
||
223 | */ |
||
224 | protected function setUDB2Data( |
||
235 | |||
236 | /** |
||
237 | * Update the major info. |
||
238 | * |
||
239 | * @param Title $title |
||
240 | * @param EventType $eventType |
||
241 | * @param Location $location |
||
242 | * @param CalendarInterface $calendar |
||
243 | * @param Theme|null $theme |
||
244 | */ |
||
245 | public function updateMajorInfo( |
||
254 | |||
255 | /** |
||
256 | * @param Audience $audience |
||
257 | */ |
||
258 | public function updateAudience( |
||
268 | |||
269 | /** |
||
270 | * @param AudienceUpdated $audienceUpdated |
||
271 | */ |
||
272 | public function applyAudienceUpdated(AudienceUpdated $audienceUpdated) |
||
276 | |||
277 | /** |
||
278 | * @inheritDoc |
||
279 | * @return ImagesImportedFromUDB2 |
||
280 | */ |
||
281 | protected function createImagesImportedFromUDB2(ImageCollection $images) |
||
285 | |||
286 | /** |
||
287 | * @inheritDoc |
||
288 | * @return ImagesUpdatedFromUDB2 |
||
289 | */ |
||
290 | protected function createImagesUpdatedFromUDB2(ImageCollection $images) |
||
294 | |||
295 | /** |
||
296 | * @inheritdoc |
||
297 | */ |
||
298 | public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri) |
||
308 | |||
309 | /** |
||
310 | * @param Label $label |
||
311 | * @return LabelAdded |
||
312 | */ |
||
313 | protected function createLabelAddedEvent(Label $label) |
||
317 | |||
318 | /** |
||
319 | * @param Label $label |
||
320 | * @return LabelRemoved |
||
321 | */ |
||
322 | protected function createLabelRemovedEvent(Label $label) |
||
326 | |||
327 | /** |
||
328 | * @param Image $image |
||
329 | * @return ImageAdded |
||
330 | */ |
||
331 | protected function createImageAddedEvent(Image $image) |
||
335 | |||
336 | /** |
||
337 | * @param Image $image |
||
338 | * @return ImageRemoved |
||
339 | */ |
||
340 | protected function createImageRemovedEvent(Image $image) |
||
344 | |||
345 | /** |
||
346 | * @param AbstractUpdateImage $updateImageCommand |
||
347 | * @return ImageUpdated |
||
348 | */ |
||
349 | protected function createImageUpdatedEvent( |
||
359 | |||
360 | /** |
||
361 | * @param Image $image |
||
362 | * @return MainImageSelected |
||
363 | */ |
||
364 | protected function createMainImageSelectedEvent(Image $image) |
||
368 | |||
369 | /** |
||
370 | * @param Language $language |
||
371 | * @param StringLiteral $title |
||
372 | * @return TitleTranslated |
||
373 | */ |
||
374 | protected function createTitleTranslatedEvent(Language $language, StringLiteral $title) |
||
378 | |||
379 | /** |
||
380 | * @param Language $language |
||
381 | * @param StringLiteral $description |
||
382 | * @return DescriptionTranslated |
||
383 | */ |
||
384 | protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description) |
||
388 | |||
389 | /** |
||
390 | * @param string $description |
||
391 | * @return DescriptionUpdated |
||
392 | */ |
||
393 | protected function createDescriptionUpdatedEvent($description) |
||
397 | |||
398 | /** |
||
399 | * @param string $typicalAgeRange |
||
400 | * @return TypicalAgeRangeUpdated |
||
401 | */ |
||
402 | protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange) |
||
406 | |||
407 | /** |
||
408 | * @return TypicalAgeRangeDeleted |
||
409 | */ |
||
410 | protected function createTypicalAgeRangeDeletedEvent() |
||
414 | |||
415 | /** |
||
416 | * @param string $organizerId |
||
417 | * @return OrganizerUpdated |
||
418 | */ |
||
419 | protected function createOrganizerUpdatedEvent($organizerId) |
||
423 | |||
424 | /** |
||
425 | * @param string $organizerId |
||
426 | * @return OrganizerDeleted |
||
427 | */ |
||
428 | protected function createOrganizerDeletedEvent($organizerId) |
||
432 | |||
433 | /** |
||
434 | * @param ContactPoint $contactPoint |
||
435 | * @return ContactPointUpdated |
||
436 | */ |
||
437 | protected function createContactPointUpdatedEvent(ContactPoint $contactPoint) |
||
441 | |||
442 | /** |
||
443 | * @param BookingInfo $bookingInfo |
||
444 | * @return BookingInfoUpdated |
||
445 | */ |
||
446 | protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo) |
||
450 | |||
451 | /** |
||
452 | * @param PriceInfo $priceInfo |
||
453 | * @return PriceInfoUpdated |
||
454 | */ |
||
455 | protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo) |
||
459 | |||
460 | /** |
||
461 | * @return EventDeleted |
||
462 | */ |
||
463 | protected function createOfferDeletedEvent() |
||
467 | |||
468 | /** |
||
469 | * @inheritdoc |
||
470 | */ |
||
471 | protected function createPublishedEvent(\DateTimeInterface $publicationDate) |
||
475 | |||
476 | /** |
||
477 | * @inheritdoc |
||
478 | */ |
||
479 | protected function createApprovedEvent() |
||
483 | |||
484 | /** |
||
485 | * @inheritdoc |
||
486 | */ |
||
487 | protected function createRejectedEvent(StringLiteral $reason) |
||
491 | |||
492 | /** |
||
493 | * @inheritDoc |
||
494 | */ |
||
495 | protected function createFlaggedAsDuplicate() |
||
499 | |||
500 | /** |
||
501 | * @inheritDoc |
||
502 | */ |
||
503 | protected function createFlaggedAsInappropriate() |
||
507 | } |
||
508 |
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.