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

EqualPrincipalPaymentAmountCalculator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 81
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getPaymentAmounts() 0 26 2
A getPeriodInterestRate() 0 4 1
A getPrincipalPart() 0 4 1
A getPaymentAmount() 0 16 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Kauri\Loan\PaymentAmountCalculator;
6
7
use Kauri\Loan\PaymentAmountCalculatorInterface;
8
9
class EqualPrincipalPaymentAmountCalculator implements PaymentAmountCalculatorInterface
10
{
11
    /**
12
     * @param array $periods
13
     * @param float $presentValue
14
     * @param float $interestRate
15
     * @param float $futureValue
16
     * @return array
17 5
     */
18
    public function getPaymentAmounts(
19 5
        array $periods,
20
        float $presentValue,
21 5
        float $interestRate,
22 3
        float $futureValue
23
    ): array {
24 2
        $paymentAmounts = array();
25
26
        $principalLeft = $presentValue;
27 5
        $noOfPayments = count($periods);
28
        $principal = $this->getPrincipalPart($presentValue, $futureValue, $noOfPayments);
29
30
        foreach ($periods as $sequenceNo => $periodLength) {
31
            $ratePerPeriod = $this->getPeriodInterestRate($interestRate, $periodLength);
32
            $noOfRemainingPeriods = $noOfPayments - $sequenceNo + 1;
33
34
            $paymentAmount = $this->getPaymentAmount($principalLeft, $futureValue, $ratePerPeriod,
35
                $noOfRemainingPeriods);
36
37
            $principalLeft = $principalLeft - $principal;
38
39
            $paymentAmounts[$sequenceNo] = $paymentAmount;
40
        }
41
42
        return $paymentAmounts;
43
    }
44
45
    /**
46
     * @param $interestRate
47
     * @param $periodLength
48
     * @return float|int
49
     */
50
    private function getPeriodInterestRate($interestRate, $periodLength)
51
    {
52
        return $interestRate / 360 * $periodLength;
53
    }
54
55
    /**
56
     * @param $presentValue
57
     * @param $futureValue
58
     * @param $noOfPeriods
59
     * @return float|int
60
     */
61
    private function getPrincipalPart($presentValue, $futureValue, $noOfPeriods)
62
    {
63
        return $principal = ($presentValue - $futureValue) / $noOfPeriods;
0 ignored issues
show
Unused Code introduced by
$principal is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
    }
65
66
    /**
67
     * @param float $presentValue
68
     * @param float $futureValue
69
     * @param float $ratePerPeriod
70
     * @param float $numberOfPeriods
71
     * @return float
72
     */
73
    private function getPaymentAmount(
74
        float $presentValue,
75
        float $futureValue,
76
        float $ratePerPeriod,
77
        float $numberOfPeriods
78
    ): float {
79
        $principal = $this->getPrincipalPart($presentValue, $futureValue, $numberOfPeriods);
80
81
        if ($ratePerPeriod > 0) {
82
            $payment = $principal + $presentValue * ($ratePerPeriod / 100);
83
        } else {
84
            $payment = $principal;
85
        }
86
87
        return $payment;
88
    }
89
}
90