AnnuityPaymentAmountCalculator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 43
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getPaymentAmounts() 0 29 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