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; |
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.