| Conditions | 11 | 
| Paths | 384 | 
| Total Lines | 101 | 
| 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 | ||
| 43 | public function documentWithCdbXML( | ||
| 44 | $base, | ||
| 45 | \CultureFeed_Cdb_Item_Base $item | ||
| 46 |     ) { | ||
| 47 | $jsonLD = clone $base; | ||
| 48 | |||
| 49 | $detail = null; | ||
| 50 | |||
| 51 | /** @var \CultureFeed_Cdb_Data_ActorDetail[] $details */ | ||
| 52 | $details = $item->getDetails(); | ||
| 53 | |||
| 54 |         foreach ($details as $languageDetail) { | ||
| 55 | // The first language detail found will be used to retrieve | ||
| 56 | // properties from which in UDB3 are not any longer considered | ||
| 57 | // to be language specific. | ||
| 58 |             if (!$detail) { | ||
| 59 | $detail = $languageDetail; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | // make sure the description is an object as well before trying to add | ||
| 64 | // translations | ||
| 65 |         if (empty($jsonLD->description)) { | ||
| 66 | $jsonLD->description = new \stdClass(); | ||
| 67 | } | ||
| 68 | |||
| 69 | $descriptions = [ | ||
| 70 | trim($detail->getShortDescription()), | ||
| 71 | trim($detail->getLongDescription()) | ||
| 72 | ]; | ||
| 73 | $descriptions = array_filter($descriptions); | ||
| 74 |         if (count($descriptions) > 0) { | ||
| 75 |             $jsonLD->description->nl = implode('<br/>', $descriptions); | ||
| 76 | } | ||
| 77 | |||
| 78 | // make sure the name is an object as well before trying to add | ||
| 79 | // translations | ||
| 80 |         if (empty($jsonLD->name)) { | ||
| 81 | $jsonLD->name = new \stdClass(); | ||
| 82 | } | ||
| 83 | $jsonLD->name->nl = $detail->getTitle(); | ||
| 84 | |||
| 85 | $this->cdbXMLItemBaseImporter->importPublicationInfo($item, $jsonLD); | ||
| 86 | $this->cdbXMLItemBaseImporter->importAvailable($item, $jsonLD); | ||
| 87 | $this->cdbXMLItemBaseImporter->importExternalId($item, $jsonLD); | ||
| 88 | $this->cdbXMLItemBaseImporter->importWorkflowStatus($item, $jsonLD); | ||
| 89 | |||
| 90 | // Address | ||
| 91 | $contact_cdb = $item->getContactInfo(); | ||
|  | |||
| 92 |         if ($contact_cdb) { | ||
| 93 | $addresses = $contact_cdb->getAddresses(); | ||
| 94 | |||
| 95 |             foreach ($addresses as $address) { | ||
| 96 | $address = $address->getPhysicalAddress(); | ||
| 97 | |||
| 98 |                 if ($address) { | ||
| 99 | $jsonLD->address = array( | ||
| 100 | 'addressCountry' => $address->getCountry(), | ||
| 101 | 'addressLocality' => $address->getCity(), | ||
| 102 | 'postalCode' => $address->getZip(), | ||
| 103 | 'streetAddress' => | ||
| 104 | $address->getStreet() . ' ' . | ||
| 105 | $address->getHouseNumber(), | ||
| 106 | ); | ||
| 107 | |||
| 108 | break; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | // Booking info. | ||
| 114 | $bookingInfo = array( | ||
| 115 | 'description' => '', | ||
| 116 | 'name' => 'standard price', | ||
| 117 | 'price' => 0.0, | ||
| 118 | 'priceCurrency' => 'EUR', | ||
| 119 | ); | ||
| 120 | $price = $detail->getPrice(); | ||
| 121 | |||
| 122 |         if ($price) { | ||
| 123 | $bookingInfo['description'] = floatval($price->getDescription()); | ||
| 124 | $bookingInfo['name'] = floatval($price->getTitle()); | ||
| 125 | $bookingInfo['price'] = floatval($price->getValue()); | ||
| 126 | } | ||
| 127 | $jsonLD->bookingInfo = $bookingInfo; | ||
| 128 | |||
| 129 | $this->importPicture($detail, $jsonLD); | ||
| 130 | |||
| 131 | $labelImporter = new LabelImporter(); | ||
| 132 | $labelImporter->importLabels($item, $jsonLD); | ||
| 133 | |||
| 134 | $this->importTerms($item, $jsonLD); | ||
| 135 | |||
| 136 |         if ($item instanceof \CultureFeed_Cdb_Item_Actor) { | ||
| 137 | $calendarFactory = new CalendarFactory(); | ||
| 138 | $calendar = $calendarFactory->createFromWeekScheme($item->getWeekScheme()); | ||
| 139 | $jsonLD = (object)array_merge((array)$jsonLD, $calendar->toJsonLd()); | ||
| 140 | } | ||
| 141 | |||
| 142 | return $jsonLD; | ||
| 143 | } | ||
| 144 | |||
| 222 | 
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: