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 |
||
| 5 | class CdbXmlContactInfoImporter implements CdbXmlContactInfoImporterInterface |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @inheritdoc |
||
| 9 | */ |
||
| 10 | public function importBookingInfo( |
||
| 11 | \stdClass $jsonLD, |
||
| 12 | \CultureFeed_Cdb_Data_ContactInfo $contactInfo, |
||
| 13 | \CultureFeed_Cdb_Data_Price $price = null, |
||
| 14 | \CultureFeed_Cdb_Data_Calendar_BookingPeriod $bookingPeriod = null |
||
| 15 | ) { |
||
| 16 | $bookingInfo = array(); |
||
| 17 | |||
| 18 | if ($price) { |
||
| 19 | if ($price->getDescription()) { |
||
| 20 | $bookingInfo['description'] = $price->getDescription(); |
||
| 21 | } |
||
| 22 | if ($price->getTitle()) { |
||
| 23 | $bookingInfo['name'] = $price->getTitle(); |
||
| 24 | } |
||
| 25 | if ($price->getValue() !== null) { |
||
| 26 | $bookingInfo['priceCurrency'] = 'EUR'; |
||
| 27 | $bookingInfo['price'] = floatval($price->getValue()); |
||
| 28 | } |
||
| 29 | if ($bookingPeriod) { |
||
| 30 | $startDate = $this->dateFromUdb2UnixTime($bookingPeriod->getDateFrom()); |
||
| 31 | $endDate = $this->dateFromUdb2UnixTime($bookingPeriod->getDateTill()); |
||
| 32 | |||
| 33 | $bookingInfo['availabilityStarts'] = $startDate->format('c'); |
||
| 34 | $bookingInfo['availabilityEnds'] = $endDate->format('c'); |
||
| 35 | } |
||
| 36 | } |
||
| 37 | |||
| 38 | // Add reservation contact data. |
||
| 39 | /** @var \CultureFeed_Cdb_Data_Url[] $urls */ |
||
| 40 | $urls = $contactInfo->getUrls(); |
||
| 41 | foreach ($urls as $url) { |
||
| 42 | if ($url->isForReservations()) { |
||
| 43 | $bookingInfo['url'] = $url->getUrl(); |
||
| 44 | break; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | if (array_key_exists('url', $bookingInfo)) { |
||
| 49 | $bookingInfo['urlLabel'] = 'Reserveer plaatsen'; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** @var \CultureFeed_Cdb_Data_Phone[] $phones */ |
||
| 53 | $phones = $contactInfo->getPhones(); |
||
| 54 | foreach ($phones as $phone) { |
||
| 55 | if ($phone->isForReservations()) { |
||
| 56 | $bookingInfo['phone'] = $phone->getNumber(); |
||
| 57 | break; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | foreach ($contactInfo->getMails() as $mail) { |
||
| 62 | if ($mail->isForReservations()) { |
||
| 63 | $bookingInfo['email'] = $mail->getMailAddress(); |
||
| 64 | break; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | if (!empty($bookingInfo)) { |
||
| 69 | $jsonLD->bookingInfo = $bookingInfo; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @inheritdoc |
||
| 75 | */ |
||
| 76 | public function importContactPoint( |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param int $unixTime |
||
| 131 | * @return \DateTime |
||
| 132 | */ |
||
| 133 | private function dateFromUdb2UnixTime($unixTime) |
||
| 142 | } |
||
| 143 |
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.