getPaymentAmounts()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 14
cts 14
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 19
nc 4
nop 4
crap 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Kauri\Loan\PaymentAmountCalculator;
6
7
use Kauri\Loan\PaymentAmountCalculator;
8
9
class AnnuityPaymentAmountCalculator extends PaymentAmountCalculator
10
{
11
    /**
12
     * @see http://www.financeformulas.net/Annuity_Payment_Formula.html
13
     * @see http://www.tvmcalcs.com/index.php/calculators/apps/lease_payments
14
     * @see http://www.ms.uky.edu/~rkremer/files/Finance04.pdf
15
     * @param array $periods
16
     * @param float $presentValue
17
     * @param float $interestRate
18
     * @param float $futureValue
19
     * @return array
20
     */
21 6
    public function getPaymentAmounts(
22
        array $periods,
23
        float $presentValue,
24
        float $interestRate,
25
        float $futureValue = 0.0
26
    ): array {
27 6
        $paymentAmounts = array();
28
29 6
        $discountFactor = 0;
30 6
        $futureValueDenominator = 1;
31
32 6
        foreach ($periods as $sequenceNo => $periodsLength) {
33 6
            $partialDiscountFactor = pow(1 + ($interestRate / 360 / 100 * $periodsLength),
34 6
                -$sequenceNo);
35
36 6
            $discountFactor = $discountFactor + $partialDiscountFactor;
37
38 6
            $partialFutureValueDenominator = 1 + ($interestRate / 360 / 100 * $periodsLength);
39 6
            $futureValueDenominator = $futureValueDenominator * $partialFutureValueDenominator;
40
        }
41
42 6
        $paymentAmount = ($presentValue - ($futureValue / $futureValueDenominator)) / $discountFactor;
43
44 6
        foreach ($periods as $sequenceNo => $periodsLength) {
45 6
            $paymentAmounts[$sequenceNo] = $paymentAmount;
46
        }
47
48 6
        return $paymentAmounts;
49
    }
50
51
}
52