Completed
Push — master ( 36f8e9...8874f5 )
by Tobias
02:07
created

Calculator::withSpecifiedPayments()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 13
nc 1
nop 4
crap 3
1
<?php
2
3
namespace Nyholm\EffectiveInterest;
4
5
/**
6
 * @author Tobias Nyholm <[email protected]>
7
 */
8
class Calculator
9
{
10
    /**
11
     * @var NewtonRaphson
12
     */
13
    private $newton;
14
15
    /**
16
     * @param NewtonRaphson $newton
17
     */
18 7
    public function __construct(NewtonRaphson $newton = null)
19
    {
20 7
        $this->newton = $newton ?? new NewtonRaphson();
21 7
    }
22
23
    /**
24
     * Get the interest when you know all the payments and their dates. Use this function when you have
25
     * administration fees at the first payment and/or when payments are irregular.
26
     *
27
     * @param int    $principal
28
     * @param string $startDate in format 'YYYY-mm-dd'
29
     * @param array  $payments  array with payment dates and values ['YYYY-mm-dd'=>int]
30
     * @param float  $guess     A guess what the interest may be. Between zero and one. Example 0.045
31
     *
32
     * @return float
33
     */
34 5
    public function withSpecifiedPayments(int $principal, string $startDate, array $payments, float $guess): float
35
    {
36 5
        list($values, $days) = $this->preparePayments($principal, $startDate, $payments);
37
38
        $fx = function ($x) use ($days, $values) {
39 5
            $sum = 0;
40 5
            foreach ($days as $idx => $day) {
41 5
                $sum += $values[$idx] * pow(1 + $x, ($days[0] - $day) / 365);
42
            }
43
44 5
            return $sum;
45 5
        };
46
47
        $fdx = function ($x) use ($days, $values) {
48 5
            $sum = 0;
49 5
            foreach ($days as $idx => $date) {
50 5
                $sum += (1 / 365) * ($days[0] - $date) * $values[$idx] * pow(1 + $x, (($days[0] - $date) / 365) - 1);
51
            }
52
53 5
            return $sum;
54 5
        };
55
56 5
        return $this->newton->run($fx, $fdx, $guess);
57
    }
58
59
    /**
60
     * Get the effective interest when the monthly payments are exactly the same.
61
     *
62
     * @param int   $a The total loan amount (Principal)
63
     * @param int   $p The monthly payment
64
     * @param int   $n The number of months
65
     * @param float $i A guess of what the interest might be. Interest as a number between zero and one. Example 0.045
66
     *
67
     * @return float
68
     */
69 2
    public function withEqualPayments(int $a, int $p, int $n, float $i): float
70
    {
71 View Code Duplication
        $fx = function ($i) use ($a, $p, $n) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72 2
            return  $p - $p * pow(1 + $i, -1 * $n) - $i * $a;
73 2
        };
74
75 2 View Code Duplication
        $fdx = function ($i) use ($a, $p, $n) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76 2
            return  $n * $p * pow(1 + $i, -1 * $n - 1) - $a;
77 2
        };
78
79 2
        return 12 * $this->newton->run($fx, $fdx, $i);
80
    }
81
82
    /**
83
     * Prepare payment data by separating dates from values and prefix the array with the principal.
84
     *
85
     * @param int    $principal
86
     * @param string $startDate
87
     * @param array  $payments
88
     *
89
     * @return array
90
     */
91 5
    private function preparePayments(int $principal, string $startDate, array $payments): array
92
    {
93 5
        $values = [-1 * $principal];
94 5
        $dates = [1];
95 5
        $startDate = new \DateTimeImmutable($startDate);
96
97 5
        foreach ($payments as $date => $payment) {
98 5
            $values[] = $payment;
99 5
            $dates[] = 1 + $startDate->diff(new \DateTime($date))->days;
100
        }
101
102 5
        return [$values, $dates];
103
    }
104
}
105