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 CdbXMLImporter 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 CdbXMLImporter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class CdbXMLImporter |
||
20 | { |
||
21 | /** |
||
22 | * @var CdbXMLItemBaseImporter |
||
23 | */ |
||
24 | private $cdbXMLItemBaseImporter; |
||
25 | |||
26 | /** |
||
27 | * @var EventCdbIdExtractorInterface |
||
28 | */ |
||
29 | private $cdbIdExtractor; |
||
30 | |||
31 | /** |
||
32 | * @var PriceDescriptionParser |
||
33 | */ |
||
34 | private $priceDescriptionParser; |
||
35 | |||
36 | /** |
||
37 | * @param CdbXMLItemBaseImporter $dbXMLItemBaseImporter |
||
38 | * @param EventCdbIdExtractorInterface $cdbIdExtractor |
||
39 | */ |
||
40 | public function __construct( |
||
49 | |||
50 | /** |
||
51 | * @var StringFilterInterface[] |
||
52 | */ |
||
53 | private $descriptionFilters = []; |
||
54 | |||
55 | /** |
||
56 | * Imports a UDB2 event into a UDB3 JSON-LD document. |
||
57 | * |
||
58 | * @param \stdClass $base |
||
59 | * The JSON-LD document to start from. |
||
60 | * @param \CultureFeed_Cdb_Item_Event $event |
||
61 | * The cultural event data from UDB2 to import. |
||
62 | * @param PlaceServiceInterface $placeManager |
||
63 | * The manager from which to retrieve the JSON-LD of a place. |
||
64 | * @param OrganizerServiceInterface $organizerManager |
||
65 | * The manager from which to retrieve the JSON-LD of an organizer. |
||
66 | * @param SluggerInterface $slugger |
||
67 | * The slugger that's used to generate a sameAs reference. |
||
68 | * |
||
69 | * @return \stdClass |
||
70 | * The document with the UDB2 event data merged in. |
||
71 | */ |
||
72 | public function documentWithCdbXML( |
||
142 | |||
143 | /** |
||
144 | * @param StringFilterInterface $filter |
||
145 | */ |
||
146 | public function addDescriptionFilter(StringFilterInterface $filter) |
||
150 | |||
151 | /** |
||
152 | * @param int $unixTime |
||
153 | * @return \DateTime |
||
154 | */ |
||
155 | private function dateFromUdb2UnixTime($unixTime) |
||
164 | |||
165 | /** |
||
166 | * @param \CultureFeed_Cdb_Data_EventDetail $languageDetail |
||
167 | * @param \stdClass $jsonLD |
||
168 | * @param string $language |
||
169 | */ |
||
170 | private function importDescription($languageDetail, $jsonLD, $language) |
||
185 | |||
186 | /** |
||
187 | * @param \CultureFeed_Cdb_Item_Event $event |
||
188 | * @param $jsonLD |
||
189 | */ |
||
190 | private function importLabels(\CultureFeed_Cdb_Item_Event $event, $jsonLD) |
||
219 | |||
220 | /** |
||
221 | * @param object $jsonLD |
||
222 | * @param string $labelsPropertyName |
||
223 | * The property where the labels should be listed. Used the differentiate between visible and hidden labels. |
||
224 | * @param CultureFeed_Cdb_Data_Keyword[] $keywords |
||
225 | */ |
||
226 | private function addKeywordsAsLabelsProperty($jsonLD, $labelsPropertyName, array $keywords) |
||
243 | |||
244 | /** |
||
245 | * @param \CultureFeed_Cdb_Data_EventDetail $detail |
||
246 | * @param \stdClass $jsonLD |
||
247 | * |
||
248 | * This is based on code found in the culturefeed theme. |
||
249 | * @see https://github.com/cultuurnet/culturefeed/blob/master/culturefeed_agenda/theme/theme.inc#L266-L284 |
||
250 | */ |
||
251 | View Code Duplication | private function importPicture($detail, $jsonLD) |
|
290 | |||
291 | /** |
||
292 | * @param \CultureFeed_Cdb_Item_Event $event |
||
293 | * @param PlaceServiceInterface $placeManager |
||
294 | * @param \stdClass $jsonLD |
||
295 | */ |
||
296 | private function importLocation(\CultureFeed_Cdb_Item_Event $event, PlaceServiceInterface $placeManager, $jsonLD) |
||
322 | |||
323 | /** |
||
324 | * @param \CultureFeed_Cdb_Item_Event $event |
||
325 | * @param OrganizerServiceInterface $organizerManager |
||
326 | * @param \stdClass $jsonLD |
||
327 | */ |
||
328 | private function importOrganizer( |
||
366 | |||
367 | /** |
||
368 | * @param \CultureFeed_Cdb_Data_EventDetail $detail |
||
369 | * @param \stdClass $jsonLD |
||
370 | */ |
||
371 | private function importPriceInfo( |
||
418 | |||
419 | /** |
||
420 | * @param \CultureFeed_Cdb_Data_EventDetail $detail |
||
421 | * @param \stdClass $jsonLD |
||
422 | */ |
||
423 | private function importBookingInfo( |
||
424 | \CultureFeed_Cdb_Item_Event $event, |
||
425 | \CultureFeed_Cdb_Data_EventDetail $detail, |
||
426 | $jsonLD |
||
427 | ) { |
||
428 | $bookingInfo = array(); |
||
429 | |||
430 | $price = $detail->getPrice(); |
||
431 | if ($price) { |
||
432 | if ($price->getDescription()) { |
||
433 | $bookingInfo['description'] = $price->getDescription(); |
||
434 | } |
||
435 | if ($price->getTitle()) { |
||
436 | $bookingInfo['name'] = $price->getTitle(); |
||
437 | } |
||
438 | if ($price->getValue() !== null) { |
||
439 | $bookingInfo['priceCurrency'] = 'EUR'; |
||
440 | $bookingInfo['price'] = floatval($price->getValue()); |
||
441 | } |
||
442 | if ($bookingPeriod = $event->getBookingPeriod()) { |
||
443 | $startDate = $this->dateFromUdb2UnixTime($bookingPeriod->getDateFrom()); |
||
444 | $endDate = $this->dateFromUdb2UnixTime($bookingPeriod->getDateTill()); |
||
445 | |||
446 | $bookingInfo['availabilityStarts'] = $startDate->format('c'); |
||
447 | $bookingInfo['availabilityEnds'] = $endDate->format('c'); |
||
448 | } |
||
449 | } |
||
450 | |||
451 | // Add reservation contact data. |
||
452 | $contactInfo = $event->getContactInfo(); |
||
453 | if ($contactInfo) { |
||
454 | foreach ($contactInfo->getUrls() as $url) { |
||
455 | if ($url->isForReservations()) { |
||
456 | $bookingInfo['url'] = $url->getUrl(); |
||
457 | break; |
||
458 | } |
||
459 | } |
||
460 | |||
461 | if (array_key_exists('url', $bookingInfo)) { |
||
462 | $bookingInfo['urlLabel'] = 'Reserveer plaatsen'; |
||
463 | } |
||
464 | |||
465 | foreach ($contactInfo->getPhones() as $phone) { |
||
466 | if ($phone->isForReservations()) { |
||
467 | $bookingInfo['phone'] = $phone->getNumber(); |
||
468 | break; |
||
469 | } |
||
470 | } |
||
471 | |||
472 | foreach ($contactInfo->getMails() as $mail) { |
||
473 | if ($mail->isForReservations()) { |
||
474 | $bookingInfo['email'] = $mail->getMailAddress(); |
||
475 | break; |
||
476 | } |
||
477 | } |
||
478 | } |
||
479 | |||
480 | if (!empty($bookingInfo)) { |
||
481 | $jsonLD->bookingInfo = $bookingInfo; |
||
482 | } |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * @param \CultureFeed_Cdb_Item_Event $event |
||
487 | * @param \stdClass $jsonLD |
||
488 | */ |
||
489 | private function importContactPoint( |
||
490 | \CultureFeed_Cdb_Item_Event $event, |
||
491 | \stdClass $jsonLD |
||
492 | ) { |
||
493 | $contactInfo = $event->getContactInfo(); |
||
494 | |||
495 | $notForReservations = function ($item) { |
||
496 | /** @var \CultureFeed_Cdb_Data_Url|\CultureFeed_Cdb_Data_Phone|\CultureFeed_Cdb_Data_Mail $item */ |
||
497 | return !$item->isForReservations(); |
||
498 | }; |
||
499 | |||
500 | if ($contactInfo) { |
||
501 | $contactPoint = array(); |
||
502 | |||
503 | $emails = array_filter($contactInfo->getMails(), $notForReservations); |
||
504 | |||
505 | View Code Duplication | if (!empty($emails)) { |
|
506 | $contactPoint['email'] = array_map( |
||
507 | function (\CultureFeed_Cdb_Data_Mail $email) { |
||
508 | return $email->getMailAddress(); |
||
509 | }, |
||
510 | $emails |
||
511 | ); |
||
512 | $contactPoint['email'] = array_values($contactPoint['email']); |
||
513 | } |
||
514 | |||
515 | $phones = array_filter($contactInfo->getPhones(), $notForReservations); |
||
516 | |||
517 | View Code Duplication | if (!empty($phones)) { |
|
518 | $contactPoint['phone'] = array_map( |
||
519 | function (\CultureFeed_Cdb_Data_phone $phone) { |
||
520 | return $phone->getNumber(); |
||
521 | }, |
||
522 | $phones |
||
523 | ); |
||
524 | $contactPoint['phone'] = array_values($contactPoint['phone']); |
||
525 | } |
||
526 | |||
527 | $urls = array_filter($contactInfo->getUrls(), $notForReservations); |
||
528 | |||
529 | View Code Duplication | if (!empty($urls)) { |
|
530 | $contactPoint['url'] = array_map( |
||
531 | function (\CultureFeed_Cdb_Data_Url $url) { |
||
532 | return $url->getUrl(); |
||
533 | }, |
||
534 | $urls |
||
535 | ); |
||
536 | $contactPoint['url'] = array_values($contactPoint['url']); |
||
537 | } |
||
538 | |||
539 | array_filter($contactPoint); |
||
540 | if (count($contactPoint) > 0) { |
||
541 | $contactPoint['type'] = ''; |
||
542 | $jsonLD->contactPoint[] = $contactPoint; |
||
543 | } |
||
544 | } |
||
545 | } |
||
546 | |||
547 | /** |
||
548 | * @param \CultureFeed_Cdb_Item_Event $event |
||
549 | * @param \stdClass $jsonLD |
||
550 | */ |
||
551 | private function importTerms(\CultureFeed_Cdb_Item_Event $event, $jsonLD) |
||
571 | |||
572 | /** |
||
573 | * @param \CultureFeed_Cdb_Item_Event $event |
||
574 | * @param \stdClass $jsonLD |
||
575 | */ |
||
576 | private function importCalendar(\CultureFeed_Cdb_Item_Event $event, $jsonLD) |
||
650 | |||
651 | /** |
||
652 | * @param \CultureFeed_Cdb_Item_Event $event |
||
653 | * @param \stdClass $jsonLD |
||
654 | */ |
||
655 | private function importTypicalAgeRange(\CultureFeed_Cdb_Item_Event $event, $jsonLD) |
||
662 | |||
663 | /** |
||
664 | * @param \CultureFeed_Cdb_Data_EventDetail $detail |
||
665 | * @param \stdClass $jsonLD |
||
666 | */ |
||
667 | private function importPerformers(\CultureFeed_Cdb_Data_EventDetail $detail, $jsonLD) |
||
681 | |||
682 | /** |
||
683 | * @param \CultureFeed_Cdb_Item_Event $event |
||
684 | * @param \stdClass $jsonLD |
||
685 | */ |
||
686 | private function importLanguages(\CultureFeed_Cdb_Item_Event $event, $jsonLD) |
||
698 | |||
699 | /** |
||
700 | * @param \CultureFeed_Cdb_Item_Event $event |
||
701 | * @param \stdClass $jsonLD |
||
702 | */ |
||
703 | private function importSeeAlso( |
||
724 | |||
725 | /** |
||
726 | * @param \CultureFeed_Cdb_Item_Event $event |
||
727 | * @param SluggerInterface $slugger |
||
728 | * @param \stdClass $jsonLD |
||
729 | */ |
||
730 | private function importUitInVlaanderenReference( |
||
757 | } |
||
758 |
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.