Test Failed
Pull Request — master (#5)
by Kauri
03:15
created

getPaymentAmounts()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

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