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