Conditions | 5 |
Paths | 8 |
Total Lines | 55 |
Code Lines | 40 |
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 |
||
50 | public function cancel( |
||
51 | \EE_Transaction $transaction, |
||
52 | \EE_Ticket $ticket, |
||
53 | $quantity = 1, |
||
54 | \EE_Line_Item $ticket_line_item = null |
||
55 | ) { |
||
56 | $ticket_line_item = $ticket_line_item instanceof \EE_Line_Item |
||
57 | ? $ticket_line_item |
||
58 | : $this->getTicketLineItem($transaction, $ticket); |
||
59 | // first we need to decrement the ticket quantity |
||
60 | \EEH_Line_Item::decrement_quantity($ticket_line_item, $quantity); |
||
61 | // no tickets left for this line item ? |
||
62 | if ((int)$ticket_line_item->quantity() === 0) { |
||
63 | // then just set this line item as cancelled, save, and get out |
||
64 | $ticket_line_item->set_type(\EEM_Line_Item::type_cancellation); |
||
65 | $success = $ticket_line_item->save(); |
||
66 | } else { |
||
67 | // otherwise create a new cancelled line item, so that we have a record of the cancellation |
||
68 | $items_subtotal = \EEH_Line_Item::get_pre_tax_subtotal( |
||
69 | \EEH_Line_Item::get_event_line_item_for_ticket( |
||
70 | $transaction->total_line_item(), |
||
71 | $ticket |
||
72 | ) |
||
73 | ); |
||
74 | $cancelled_line_item = \EE_Line_Item::new_instance( |
||
75 | array( |
||
76 | 'LIN_name' => $ticket_line_item->name(), |
||
77 | 'LIN_desc' => sprintf( |
||
78 | __('%1$s Cancelled: %2$s', 'event_espresso'), |
||
79 | $ticket_line_item->desc(), |
||
80 | date('Y-m-d h:i a') |
||
81 | ), |
||
82 | 'LIN_unit_price' => (float)$ticket_line_item->unit_price(), |
||
83 | 'LIN_quantity' => $quantity, |
||
84 | 'LIN_percent' => null, |
||
85 | 'LIN_is_taxable' => false, |
||
86 | 'LIN_order' => $items_subtotal instanceof \EE_Line_Item |
||
87 | ? count($items_subtotal->children()) |
||
88 | : 0, |
||
89 | 'LIN_total' => (float)$ticket_line_item->unit_price(), |
||
90 | 'LIN_type' => \EEM_Line_Item::type_cancellation |
||
91 | ) |
||
92 | ); |
||
93 | $success = \EEH_Line_Item::add_item($transaction->total_line_item(), $cancelled_line_item); |
||
94 | } |
||
95 | if ( ! $success) { |
||
96 | throw new \RuntimeException( |
||
97 | sprintf( |
||
98 | __('An error occurred while attempting to cancel ticket line item %1$s', 'event_espresso'), |
||
99 | $ticket_line_item->ID() |
||
100 | ) |
||
101 | ); |
||
102 | } |
||
103 | return $success; |
||
104 | } |
||
105 | |||
142 | // Location: /CancelTicketLineItemService.php |