Conditions | 14 |
Paths | 769 |
Total Lines | 72 |
Code Lines | 38 |
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 |
||
48 | function calculate() |
||
49 | { |
||
50 | $runningtotal = $this->order->SubTotal(); |
||
51 | $sort = 1; |
||
52 | $existingmodifiers = $this->order->Modifiers(); |
||
53 | $modifierclasses = array_unique(Order::config()->modifiers); |
||
54 | |||
55 | //check if modifiers are even in use |
||
56 | if (!is_array($modifierclasses) || empty($modifierclasses)) { |
||
57 | return $runningtotal; |
||
58 | } |
||
59 | |||
60 | if (DB::get_conn()->supportsTransactions()) { |
||
61 | DB::get_conn()->transactionStart(); |
||
62 | } |
||
63 | |||
64 | set_error_handler( |
||
65 | function ($severity, $message, $file, $line) { |
||
66 | throw new ErrorException($message, 0, $severity, $file, $line); |
||
67 | }, |
||
68 | E_ALL & ~(E_STRICT | E_NOTICE) |
||
69 | ); |
||
70 | |||
71 | try { |
||
72 | foreach ($modifierclasses as $ClassName) { |
||
73 | if ($modifier = $this->getModifier($ClassName)) { |
||
74 | $modifier->Sort = $sort; |
||
75 | $runningtotal = $modifier->modify($runningtotal); |
||
76 | if ($modifier->isChanged()) { |
||
77 | $modifier->write(); |
||
78 | } |
||
79 | } |
||
80 | $sort++; |
||
81 | } |
||
82 | //clear old modifiers out |
||
83 | if ($existingmodifiers) { |
||
84 | foreach ($existingmodifiers as $modifier) { |
||
85 | if (!in_array($modifier->ClassName, $modifierclasses)) { |
||
86 | $modifier->delete(); |
||
87 | $modifier->destroy(); |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | } catch (Exception $ex) { |
||
92 | // Rollback the transaction if an error occurred |
||
93 | if (DB::get_conn()->supportsTransactions()) { |
||
94 | DB::get_conn()->transactionRollback(); |
||
95 | } |
||
96 | // throw the exception after rollback |
||
97 | throw $ex; |
||
98 | } finally { |
||
99 | // restore the error handler, no matter what |
||
100 | restore_error_handler(); |
||
101 | } |
||
102 | |||
103 | // Everything went through fine, complete the transaction |
||
104 | if (DB::get_conn()->supportsTransactions()) { |
||
105 | DB::get_conn()->transactionEnd(); |
||
106 | } |
||
107 | |||
108 | //prevent negative sales from ever occurring |
||
109 | if ($runningtotal < 0) { |
||
110 | $this->logger->error( |
||
111 | "Order (ID = {$this->order->ID}) was calculated to equal $runningtotal.\n |
||
112 | Order totals should never be negative!\n |
||
113 | The order total was set to $0" |
||
114 | ); |
||
115 | |||
116 | $runningtotal = 0; |
||
117 | } |
||
118 | |||
119 | return $runningtotal; |
||
120 | } |
||
161 |