| Conditions | 9 |
| Paths | 28 |
| Total Lines | 77 |
| Code Lines | 62 |
| Lines | 36 |
| Ratio | 46.75 % |
| Changes | 3 | ||
| Bugs | 1 | 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 |
||
| 82 | public function CheckTax($isTaxApply,$id) { |
||
| 83 | try { |
||
| 84 | $rate1 = 0; |
||
| 85 | $rate2 = 0; |
||
| 86 | $name1 = 'null'; |
||
| 87 | $name2 = 'null'; |
||
| 88 | $ruleEnabled = $this->taxRules->where('id', '1')->first(); |
||
| 89 | if ($ruleEnabled) { |
||
| 90 | $enabled = $ruleEnabled->status; |
||
| 91 | $type = $ruleEnabled->type; |
||
| 92 | $compound = $ruleEnabled->compound; |
||
| 93 | if ($enabled == 1 && $type == 'exclusive') { |
||
| 94 | if ($isTaxApply == 1) { |
||
| 95 | $tax1 = $this->tax->where('level', 1)->first(); |
||
| 96 | $tax2 = $this->tax->where('level', 2)->first(); |
||
| 97 | View Code Duplication | if ($tax1) { |
|
| 98 | $name1 = $tax1->name; |
||
| 99 | $rate1 = $tax1->rate; |
||
| 100 | $taxCondition1 = new \Darryldecode\Cart\CartCondition([ |
||
| 101 | 'name' => $name1, |
||
| 102 | 'type' => 'tax', |
||
| 103 | 'target' => 'item', |
||
| 104 | 'value' => $rate1 . '%', |
||
| 105 | ]); |
||
| 106 | } else { |
||
| 107 | $taxCondition1 = new \Darryldecode\Cart\CartCondition([ |
||
| 108 | 'name' => $name1, |
||
| 109 | 'type' => 'tax', |
||
| 110 | 'target' => 'item', |
||
| 111 | 'value' => $rate1, |
||
| 112 | ]); |
||
| 113 | } |
||
| 114 | View Code Duplication | if ($tax2) { |
|
| 115 | $name2 = $tax2->name; |
||
| 116 | $rate2 = $tax2->rate; |
||
| 117 | $taxCondition2 = new \Darryldecode\Cart\CartCondition([ |
||
| 118 | 'name' => $name2, |
||
| 119 | 'type' => 'tax', |
||
| 120 | 'target' => 'item', |
||
| 121 | 'value' => $rate2 . '%', |
||
| 122 | ]); |
||
| 123 | } else { |
||
| 124 | $taxCondition2 = new \Darryldecode\Cart\CartCondition([ |
||
| 125 | 'name' => $name2, |
||
| 126 | 'type' => 'tax', |
||
| 127 | 'target' => 'item', |
||
| 128 | 'value' => $rate2, |
||
| 129 | ]); |
||
| 130 | } |
||
| 131 | } else { |
||
| 132 | $taxCondition1 = new \Darryldecode\Cart\CartCondition([ |
||
| 133 | 'name' => $name1, |
||
| 134 | 'type' => 'tax', |
||
| 135 | 'target' => 'item', |
||
| 136 | 'value' => $rate1, |
||
| 137 | ]); |
||
| 138 | $taxCondition2 = new \Darryldecode\Cart\CartCondition([ |
||
| 139 | 'name' => $name2, |
||
| 140 | 'type' => 'tax', |
||
| 141 | 'target' => 'item', |
||
| 142 | 'value' => $rate2, |
||
| 143 | ]); |
||
| 144 | } |
||
| 145 | $currency_attribute = $this->addCurrencyAttributes($id); |
||
| 146 | //dd($currency_attribute); |
||
| 147 | if ($compound == 1) { |
||
| 148 | return ['conditions' => [$taxCondition1, $taxCondition2], 'attributes' => ['tax' => [['name' => $name1, 'rate' => $rate1], ['name' => $name2, 'rate' => $rate2]],'currency'=>$currency_attribute]]; |
||
| 149 | } else { |
||
| 150 | return ['conditions' => $taxCondition2, 'attributes' => ['tax' => [['name' => $name2, 'rate' => $rate2]],'currency'=>$currency_attribute]]; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } catch (\Exception $ex) { |
||
| 155 | dd($ex); |
||
| 156 | throw new \Exception('Can not check the tax'); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 305 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: