| Conditions | 11 |
| Paths | 384 |
| Total Lines | 103 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 52 | public function documentWithCdbXML( |
||
| 53 | $base, |
||
| 54 | \CultureFeed_Cdb_Item_Base $item |
||
| 55 | ) { |
||
| 56 | $jsonLD = clone $base; |
||
| 57 | |||
| 58 | $detail = null; |
||
| 59 | |||
| 60 | /** @var \CultureFeed_Cdb_Data_ActorDetail[] $details */ |
||
| 61 | $details = $item->getDetails(); |
||
| 62 | |||
| 63 | foreach ($details as $languageDetail) { |
||
| 64 | // The first language detail found will be used to retrieve |
||
| 65 | // properties from which in UDB3 are not any longer considered |
||
| 66 | // to be language specific. |
||
| 67 | if (!$detail) { |
||
| 68 | $detail = $languageDetail; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | // make sure the description is an object as well before trying to add |
||
| 73 | // translations |
||
| 74 | if (empty($jsonLD->description)) { |
||
| 75 | $jsonLD->description = new \stdClass(); |
||
| 76 | } |
||
| 77 | |||
| 78 | $descriptions = [ |
||
| 79 | trim($detail->getShortDescription()), |
||
| 80 | trim($detail->getLongDescription()) |
||
| 81 | ]; |
||
| 82 | $descriptions = array_filter($descriptions); |
||
| 83 | if (count($descriptions) > 0) { |
||
| 84 | $jsonLD->description->nl = implode('<br/>', $descriptions); |
||
| 85 | } |
||
| 86 | |||
| 87 | // make sure the name is an object as well before trying to add |
||
| 88 | // translations |
||
| 89 | if (empty($jsonLD->name)) { |
||
| 90 | $jsonLD->name = new \stdClass(); |
||
| 91 | } |
||
| 92 | $jsonLD->name->nl = $detail->getTitle(); |
||
| 93 | |||
| 94 | $this->cdbXMLItemBaseImporter->importPublicationInfo($item, $jsonLD); |
||
| 95 | $this->cdbXMLItemBaseImporter->importAvailable($item, $jsonLD); |
||
| 96 | $this->cdbXMLItemBaseImporter->importExternalId($item, $jsonLD); |
||
| 97 | $this->cdbXMLItemBaseImporter->importWorkflowStatus($item, $jsonLD); |
||
| 98 | |||
| 99 | // Address |
||
| 100 | $contact_cdb = $item->getContactInfo(); |
||
|
|
|||
| 101 | if ($contact_cdb) { |
||
| 102 | /** @var \CultureFeed_Cdb_Data_Address[] $addresses */ |
||
| 103 | $addresses = $contact_cdb->getAddresses(); |
||
| 104 | |||
| 105 | foreach ($addresses as $address) { |
||
| 106 | $address = $address->getPhysicalAddress(); |
||
| 107 | |||
| 108 | if ($address) { |
||
| 109 | $jsonLD->address = array( |
||
| 110 | 'addressCountry' => $address->getCountry(), |
||
| 111 | 'addressLocality' => $address->getCity(), |
||
| 112 | 'postalCode' => $address->getZip(), |
||
| 113 | 'streetAddress' => |
||
| 114 | $address->getStreet() . ' ' . |
||
| 115 | $address->getHouseNumber(), |
||
| 116 | ); |
||
| 117 | |||
| 118 | break; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | // Booking info. |
||
| 124 | $bookingInfo = array( |
||
| 125 | 'description' => '', |
||
| 126 | 'name' => 'standard price', |
||
| 127 | 'price' => 0.0, |
||
| 128 | 'priceCurrency' => 'EUR', |
||
| 129 | ); |
||
| 130 | $price = $detail->getPrice(); |
||
| 131 | |||
| 132 | if ($price) { |
||
| 133 | $bookingInfo['description'] = floatval($price->getDescription()); |
||
| 134 | $bookingInfo['name'] = floatval($price->getTitle()); |
||
| 135 | $bookingInfo['price'] = floatval($price->getValue()); |
||
| 136 | } |
||
| 137 | $jsonLD->bookingInfo = $bookingInfo; |
||
| 138 | |||
| 139 | $this->importPicture($detail, $jsonLD); |
||
| 140 | |||
| 141 | $labelImporter = new LabelImporter(); |
||
| 142 | $labelImporter->importLabels($item, $jsonLD); |
||
| 143 | |||
| 144 | $this->importTerms($item, $jsonLD); |
||
| 145 | |||
| 146 | if ($item instanceof \CultureFeed_Cdb_Item_Actor) { |
||
| 147 | $calendar = $this->calendarFactory->createFromWeekScheme( |
||
| 148 | $item->getWeekScheme() |
||
| 149 | ); |
||
| 150 | $jsonLD = (object)array_merge((array)$jsonLD, $calendar->toJsonLd()); |
||
| 151 | } |
||
| 152 | |||
| 153 | return $jsonLD; |
||
| 154 | } |
||
| 155 | |||
| 233 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: