| Conditions | 16 |
| Paths | 18 |
| Total Lines | 61 |
| 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 |
||
| 58 | public function create(EE_Base_Class $entity) |
||
| 59 | { |
||
| 60 | if (! $entity instanceof EE_Ticket) { |
||
| 61 | throw new InvalidEntityException($entity, 'EE_Ticket'); |
||
| 62 | } |
||
| 63 | $taxes = []; |
||
| 64 | $new_prices = []; |
||
| 65 | $is_free = true; |
||
| 66 | $has_base_price = false; |
||
| 67 | $default_prices = $this->price_model->get_all_default_prices(false, true); |
||
| 68 | if (is_array($default_prices)) { |
||
| 69 | foreach ($default_prices as $default_price) { |
||
| 70 | if (! $default_price instanceof EE_Price) { |
||
| 71 | throw new InvalidEntityException($default_price, 'EE_Price'); |
||
| 72 | } |
||
| 73 | // grab any taxes but don't do anything just yet |
||
| 74 | if ($default_price->is_tax()) { |
||
| 75 | $taxes[] = $default_price; |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | // duplicate the default price so that it does not get mutated |
||
| 79 | /** @var EE_Price $default_price_clone */ |
||
| 80 | $default_price_clone = clone $default_price; |
||
| 81 | if (( |
||
| 82 | // has non-zero base price |
||
| 83 | $default_price_clone->is_base_price() |
||
| 84 | && $default_price_clone->amount() > 0 |
||
| 85 | ) ||( |
||
| 86 | // or has fixed amount surcharge |
||
| 87 | $default_price_clone->is_surcharge() |
||
| 88 | && ! $default_price_clone->is_percent() |
||
| 89 | )) { |
||
| 90 | $is_free = false; |
||
| 91 | } |
||
| 92 | $default_price_clone->set('PRC_ID', null); |
||
| 93 | $default_price_clone->set('PRC_is_default', false); |
||
| 94 | $default_price_clone->save(); |
||
| 95 | $default_price_clone->_add_relation_to($entity, 'Ticket'); |
||
| 96 | // verify that a base price has been set |
||
| 97 | $has_base_price = $default_price_clone->is_base_price() ? true : $has_base_price; |
||
| 98 | $new_prices[ $default_price_clone->ID() ] = $default_price_clone; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | if (! $is_free && ! empty($taxes)) { |
||
| 102 | foreach ($taxes as $tax) { |
||
| 103 | // assign taxes but don't duplicate them because they operate globally |
||
| 104 | $entity->set_taxable(true); |
||
| 105 | $tax->_add_relation_to($entity, 'Ticket'); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | if (! $has_base_price) { |
||
| 109 | $new_base_price = $this->createNewBasePrice($entity); |
||
| 110 | $new_prices[ $new_base_price->ID() ] = $new_base_price; |
||
| 111 | } |
||
| 112 | $ticket_total = $entity->get_ticket_total_with_taxes(true); |
||
| 113 | if ($ticket_total !== $entity->ticket_price()) { |
||
| 114 | $entity->set_price($ticket_total); |
||
| 115 | $entity->save(); |
||
| 116 | } |
||
| 117 | return $new_prices; |
||
| 118 | } |
||
| 119 | |||
| 151 |