| Conditions | 10 |
| Paths | 192 |
| Total Lines | 88 |
| Code Lines | 52 |
| Lines | 16 |
| Ratio | 18.18 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 40 | public function documentWithCdbXML( |
||
| 41 | $base, |
||
| 42 | \CultureFeed_Cdb_Item_Base $item |
||
| 43 | ) { |
||
| 44 | $jsonLD = clone $base; |
||
| 45 | |||
| 46 | $detail = null; |
||
| 47 | |||
| 48 | /** @var \CultureFeed_Cdb_Data_ActorDetail[] $details */ |
||
| 49 | $details = $item->getDetails(); |
||
| 50 | |||
| 51 | foreach ($details as $languageDetail) { |
||
| 52 | // The first language detail found will be used to retrieve |
||
| 53 | // properties from which in UDB3 are not any longer considered |
||
| 54 | // to be language specific. |
||
| 55 | if (!$detail) { |
||
| 56 | $detail = $languageDetail; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | $descriptions = [ |
||
| 61 | trim($detail->getShortDescription()), |
||
| 62 | trim($detail->getLongDescription()) |
||
| 63 | ]; |
||
| 64 | $descriptions = array_filter($descriptions); |
||
| 65 | if (count($descriptions) > 0) { |
||
| 66 | $jsonLD->description = implode('<br/>', $descriptions); |
||
| 67 | } |
||
| 68 | |||
| 69 | $jsonLD->name = $detail->getTitle(); |
||
| 70 | |||
| 71 | $this->cdbXMLItemBaseImporter->importPublicationInfo($item, $jsonLD); |
||
| 72 | $this->cdbXMLItemBaseImporter->importAvailable($item, $jsonLD); |
||
| 73 | $this->cdbXMLItemBaseImporter->importExternalId($item, $jsonLD); |
||
| 74 | |||
| 75 | // Address |
||
| 76 | $contact_cdb = $item->getContactInfo(); |
||
|
|
|||
| 77 | if ($contact_cdb) { |
||
| 78 | $addresses = $contact_cdb->getAddresses(); |
||
| 79 | |||
| 80 | View Code Duplication | foreach ($addresses as $address) { |
|
| 81 | $address = $address->getPhysicalAddress(); |
||
| 82 | |||
| 83 | if ($address) { |
||
| 84 | $jsonLD->address = array( |
||
| 85 | 'addressCountry' => $address->getCountry(), |
||
| 86 | 'addressLocality' => $address->getCity(), |
||
| 87 | 'postalCode' => $address->getZip(), |
||
| 88 | 'streetAddress' => |
||
| 89 | $address->getStreet() . ' ' . |
||
| 90 | $address->getHouseNumber(), |
||
| 91 | ); |
||
| 92 | |||
| 93 | break; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | // Booking info. |
||
| 99 | $bookingInfo = array( |
||
| 100 | 'description' => '', |
||
| 101 | 'name' => 'standard price', |
||
| 102 | 'price' => 0.0, |
||
| 103 | 'priceCurrency' => 'EUR', |
||
| 104 | ); |
||
| 105 | $price = $detail->getPrice(); |
||
| 106 | |||
| 107 | if ($price) { |
||
| 108 | $bookingInfo['description'] = floatval($price->getDescription()); |
||
| 109 | $bookingInfo['name'] = floatval($price->getTitle()); |
||
| 110 | $bookingInfo['price'] = floatval($price->getValue()); |
||
| 111 | } |
||
| 112 | $jsonLD->bookingInfo = $bookingInfo; |
||
| 113 | |||
| 114 | // Image. |
||
| 115 | $images = $detail->getMedia()->byMediaType( |
||
| 116 | \CultureFeed_Cdb_Data_File::MEDIA_TYPE_PHOTO |
||
| 117 | ); |
||
| 118 | $images->rewind(); |
||
| 119 | $image = count($images) > 0 ? $images->current() : null; |
||
| 120 | if ($image) { |
||
| 121 | $jsonLD->image = $image->getHLink(); |
||
| 122 | } |
||
| 123 | |||
| 124 | $this->importTerms($item, $jsonLD); |
||
| 125 | |||
| 126 | return $jsonLD; |
||
| 127 | } |
||
| 128 | |||
| 159 |
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: