Completed
Push — master ( a3da9c...ba4a7f )
by Tobias
23:20
created

Calculator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 10.71 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 6
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A withSpecifiedPayments() 0 4 1
A withEqualPayments() 6 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
     *
17
     * @param NewtonRaphson $newton
18
     */
19
    public function __construct(NewtonRaphson $newton = null)
20
    {
21
        $this->newton = $newton ?? new NewtonRaphson();
22
    }
23
24
25
    /**
26
     * Get the interest when you know all the payments and their dates. Use this function when you have
27
     * administration fees at the first payment and/or when payments are irregular.
28
     *
29
     * @param int    $principal
30
     * @param string $startDate in format 'YYYY-mm-dd'
31
     * @param array  $payments  array with payment dates and values ['YYYY-mm-dd'=>int]
32
     * @param float  $guess     A guess what the interest may be. Between zero and one. Example 0.045
33
     *
34
     * @return float
35
     */
36
    public function withSpecifiedPayments(int $principal, string $startDate, array $payments, float $guess): float
0 ignored issues
show
Unused Code introduced by
The parameter $principal is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $startDate is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $payments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $guess is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
    {
38
        return 0.045;
39
    }
40
41
    /**
42
     * Get the effective interest when the monthly payments are exactly the same.
43
     *
44
     * @param int   $a The total loan amount (Principal)
45
     * @param int   $p The monthly payment
46
     * @param int   $n The number of months
47
     * @param float $i A guess of what the interest might be. Interest as a number between zero and one. Example 0.045
48
     *
49
     * @return float
50
     */
51
    public function withEqualPayments(int $a, int $p, int $n, float $i): float
52
    {
53 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...
54
            return  $p - $p * pow(1 + $i, -1 * $n) - $i * $a;
55
        };
56
57 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...
58
            return  $n * $p * pow(1 + $i, -1 * $n - 1) - $a;
59
        };
60
61
        return $this->newton->run($fx, $fdx, $i);
62
    }
63
}
64