| Conditions | 13 |
| Paths | 360 |
| Total Lines | 49 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 93 | public function setData(Order $order, array $data) |
||
| 94 | { |
||
| 95 | $address = $this->getAddress($order); |
||
| 96 | //if the value matches the current address then unset |
||
| 97 | //this is to fix issues with blank fields & the readonly Country field |
||
| 98 | $addressFields = DataObject::getSchema()->databaseFields(\SilverShop\Model\Address::class); |
||
| 99 | foreach ($data as $key => $value) { |
||
| 100 | if (!isset($addressFields[$key]) || (!$value && !$address->{$key})) { |
||
| 101 | unset($data[$key]); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | $address->update($data); |
||
| 105 | //if only one country is available, then set it |
||
| 106 | if ($country = SiteConfig::current_site_config()->getSingleCountry()) { |
||
| 107 | $address->Country = $country; |
||
| 108 | } |
||
| 109 | //write new address, or duplicate if changed |
||
| 110 | if (!$address->isInDB()) { |
||
| 111 | $address->write(); |
||
| 112 | } elseif ($address->isChanged()) { |
||
| 113 | $address = $address->duplicate(); |
||
| 114 | } |
||
| 115 | //set billing address, if not already set |
||
| 116 | $order->{$this->addresstype . 'AddressID'} = $address->ID; |
||
| 117 | if (!$order->BillingAddressID) { |
||
| 118 | $order->BillingAddressID = $address->ID; |
||
| 119 | } |
||
| 120 | $order->write(); |
||
| 121 | |||
| 122 | if ($this->addresstype === 'Shipping') { |
||
| 123 | ShopUserInfo::singleton()->setAddress($address); |
||
| 124 | |||
| 125 | $order->extend('onSetShippingAddress', $address); |
||
| 126 | } |
||
| 127 | |||
| 128 | //associate member to address |
||
| 129 | if ($member = Security::getCurrentUser()) { |
||
| 130 | $default = $member->{'Default' . $this->addresstype . 'Address'}(); |
||
| 131 | //set default address |
||
| 132 | if (!$default->exists()) { |
||
| 133 | $member->{'Default' . $this->addresstype . 'AddressID'} = $address->ID; |
||
| 134 | $member->write(); |
||
| 135 | } |
||
| 136 | if ($this->addtoaddressbook) { |
||
| 137 | $member->AddressBook()->add($address); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | //extension hooks |
||
| 141 | $order->extend('onSet' . $this->addresstype . 'Address', $address); |
||
| 142 | } |
||
| 169 |