Conditions | 9 |
Paths | 128 |
Total Lines | 83 |
Code Lines | 53 |
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 |
||
43 | public function getRecalculated( |
||
44 | $entity, |
||
45 | $taxValue = '', |
||
46 | $taxAttributeCode = '', |
||
47 | $shippingTaxValue = '' |
||
48 | ) { |
||
49 | |||
50 | $generalHelper = Mage::helper($this->_code); |
||
51 | $generalHelper->addLog("== START == Recalculation of entity prices. Entity class: " . get_class($entity) . ". Entity id: {$entity->getId()}"); |
||
52 | |||
53 | $subTotal = $entity->getData('subtotal'); |
||
54 | $shippingAmount = $entity->getData('shipping_amount'); |
||
55 | $grandTotal = $entity->getData('grand_total'); |
||
56 | $grandDiscount = $grandTotal - $subTotal - $shippingAmount; |
||
57 | |||
58 | $percentageSum = 0; |
||
59 | |||
60 | $items = $entity->getAllVisibleItems() ? $entity->getAllVisibleItems() : $entity->getAllItems(); |
||
61 | $itemsFinal = []; |
||
62 | $itemsSum = 0.00; |
||
63 | foreach ($items as $item) { |
||
64 | if (!$this->isValidItem($item)) { |
||
65 | continue; |
||
66 | } |
||
67 | |||
68 | $taxValue = $this->addTaxValue($taxAttributeCode, $entity, $item); |
||
69 | |||
70 | $price = $item->getData('price'); |
||
71 | $qty = $item->getQty() ?: $item->getQtyOrdered(); |
||
72 | $rowTotal = $item->getData('row_total'); |
||
73 | |||
74 | //Calculate Percentage. The heart of logic. |
||
75 | $rowPercentage = $rowTotal / $subTotal; |
||
76 | $percentageSum += $rowPercentage; |
||
77 | |||
78 | $discountPerUnit = $rowPercentage * $grandDiscount / $qty; |
||
79 | $priceWithDiscount = $this->slyFloor($price + $discountPerUnit); |
||
80 | |||
81 | $entityItem = $this->_buildItem($item, $priceWithDiscount, $taxValue); |
||
82 | |||
83 | $itemsFinal[$item->getSku()] = $entityItem; |
||
84 | $itemsSum += $entityItem['sum']; |
||
85 | } |
||
86 | |||
87 | $generalHelper->addLog("Sum of all percentages: {$percentageSum}"); |
||
88 | |||
89 | //Calculate DIFF! |
||
90 | $itemsSumDiff = $this->slyFloor($grandTotal - $itemsSum - $shippingAmount); |
||
91 | |||
92 | $generalHelper->addLog("Items sum: {$itemsSum}. All Discounts: {$grandDiscount} Diff value: {$itemsSumDiff}"); |
||
93 | if (bccomp($itemsSumDiff, 0.00, 2) < 0) { |
||
94 | //if: $itemsSumDiff < 0 |
||
|
|||
95 | $generalHelper->addLog("Notice: Sum of all items is greater than sumWithAllDiscount of entity. ItemsSumDiff: {$itemsSumDiff}"); |
||
96 | $itemsSumDiff = 0.0; |
||
97 | } |
||
98 | |||
99 | $receipt = [ |
||
100 | 'sum' => $itemsSum, |
||
101 | 'origGrandTotal' => floatval($grandTotal) |
||
102 | ]; |
||
103 | |||
104 | $shippingName = $entity->getShippingDescription() ?: ($entity->getOrder() ? $entity->getOrder()->getShippingDescription() : ''); |
||
105 | |||
106 | $shippingItem = [ |
||
107 | 'name' => $shippingName, |
||
108 | 'price' => $entity->getShippingAmount() + $itemsSumDiff, |
||
109 | 'quantity' => 1.0, |
||
110 | 'sum' => $entity->getShippingAmount() + $itemsSumDiff, |
||
111 | 'tax' => $shippingTaxValue, |
||
112 | ]; |
||
113 | |||
114 | $itemsFinal['shipping'] = $shippingItem; |
||
115 | $receipt['items'] = $itemsFinal; |
||
116 | |||
117 | if (!$this->_checkReceipt($receipt)) { |
||
118 | $generalHelper->addLog("WARNING: Calculation error! Sum of items is not equal to grandTotal!"); |
||
119 | } |
||
120 | $generalHelper->addLog("Final result of recalculation:"); |
||
121 | $generalHelper->addLog($receipt); |
||
122 | $generalHelper->addLog("== STOP == Recalculation of entity prices. "); |
||
123 | |||
124 | return $receipt; |
||
125 | } |
||
126 | |||
200 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.