| Conditions | 6 |
| Paths | 13 |
| Total Lines | 73 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 35 | public function generateInvoiceForPeriod(\DateTimeInterface $startDate, \DateTimeInterface $endDate, CustomerInterface $customer): Receipt |
||
| 36 | { |
||
| 37 | $payments = $this->paymentRepository->getPaymentsForCustomerDuring($startDate, $endDate, $customer); |
||
| 38 | |||
| 39 | if (empty($payments)) { |
||
| 40 | throw new \Exception('No payments for receipt'); |
||
| 41 | } |
||
| 42 | |||
| 43 | $total = null; |
||
| 44 | $vatTotal = null; |
||
| 45 | $subTotalTotal = null; |
||
| 46 | $subscriptions = []; |
||
| 47 | $lines = []; |
||
| 48 | |||
| 49 | $receipt = new Receipt(); |
||
| 50 | foreach ($payments as $payment) { |
||
| 51 | $subscriptions = array_merge($subscriptions, $payment->getSubscriptions()->toArray()); |
||
| 52 | $money = $payment->getMoneyAmount(); |
||
| 53 | |||
| 54 | $total = $this->addToTotal($total, $money); |
||
| 55 | |||
| 56 | if (0 === $payment->getSubscriptions()->count()) { |
||
| 57 | $vat = $this->taxCalculator->calculateVatAmountForCustomer($customer, $payment->getMoneyAmount()); |
||
| 58 | $subTotal = $this->taxCalculator->calculateSubTotalForCustomer($customer, $money); |
||
| 59 | $vatTotal = $this->addToTotal($vatTotal, $vat); |
||
| 60 | $subTotalTotal = $this->addToTotal($subTotalTotal, $subTotal); |
||
| 61 | |||
| 62 | $line = new ReceiptLine(); |
||
| 63 | $line->setTotal($payment->getAmount()); |
||
| 64 | $line->setCurrency($payment->getCurrency()); |
||
| 65 | $line->setDescription($payment->getDescription()); |
||
| 66 | $line->setReceipt($receipt); |
||
| 67 | $line->setVatTotal($vat->getMinorAmount()->toInt()); |
||
| 68 | $line->setSubTotal($subTotal->getMinorAmount()->toInt()); |
||
| 69 | |||
| 70 | $lines[] = $line; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | /** @var Subscription $subscription */ |
||
| 75 | foreach ($subscriptions as $subscription) { |
||
| 76 | $money = $subscription->getMoneyAmount(); |
||
| 77 | |||
| 78 | $vat = $this->taxCalculator->calculateVatAmountForCustomer($customer, $money); |
||
| 79 | $subTotal = $money->minus($vat, RoundingMode::HALF_DOWN); |
||
| 80 | |||
| 81 | $vatTotal = $this->addToTotal($vatTotal, $vat); |
||
| 82 | $subTotalTotal = $this->addToTotal($subTotalTotal, $subTotal); |
||
| 83 | |||
| 84 | $line = new ReceiptLine(); |
||
| 85 | $line->setTotal($subscription->getAmount()); |
||
| 86 | $line->setCurrency($subscription->getCurrency()); |
||
| 87 | $line->setDescription($subscription->getPlanName()); |
||
| 88 | $line->setReceipt($receipt); |
||
| 89 | $line->setVatTotal($vat->getMinorAmount()->toInt()); |
||
| 90 | $line->setSubTotal($subTotal->getMinorAmount()->toInt()); |
||
| 91 | |||
| 92 | $lines[] = $line; |
||
| 93 | } |
||
| 94 | |||
| 95 | if (!$total instanceof Money) { |
||
| 96 | throw new \LogicException('Total must be money if payments exist'); |
||
| 97 | } |
||
| 98 | |||
| 99 | $receipt->setCustomer($customer); |
||
| 100 | $receipt->setPayments($payments); |
||
| 101 | $receipt->setSubscriptions($subscriptions); |
||
| 102 | $receipt->setTotal($total->getMinorAmount()->toInt()); |
||
| 103 | $receipt->setSubTotal($subTotalTotal->getMinorAmount()->toInt()); |
||
| 104 | $receipt->setVatTotal($vatTotal->getMinorAmount()->toInt()); |
||
| 105 | $receipt->setLines($lines); |
||
| 106 | |||
| 107 | return $receipt; |
||
| 108 | } |
||
| 166 |