BankLoan::__construct()   B
last analyzed

Complexity

Conditions 9
Paths 16

Size

Total Lines 19
Code Lines 13

Duplication

Lines 6
Ratio 31.58 %

Code Coverage

Tests 17
CRAP Score 9

Importance

Changes 0
Metric Value
dl 6
loc 19
ccs 17
cts 17
cp 1
rs 7.756
c 0
b 0
f 0
cc 9
eloc 13
nc 16
nop 1
crap 9
1
<?php
2
/**
3
 * Amortization Schedule Calculator
4
 */
5
namespace Jiangbianwanghai\BankLoan;
6
class BankLoan
7
{
8
	/**
9
	 * @var int
10
	 */
11
	protected $loanAmount = 100000;
12
13
	/**
14
	 * @var int
15
	 */
16
	protected $year = 1;
17
18
	/**
19
	 * @var float
20
	 */
21
	protected $interestRate = 0;
22
23
	/**
24
	 * @var int
25
	 */
26
	protected $interestRateChangeIndex = 0;
27
28
	/**
29
	 * @var int
30
	 */
31
	protected $bank = 'PBC'; // The people's bank of China
32
33
	/**
34
	 * @var int
35
	 */
36
	private $_monthlyinterestRate = 0;
37
38
	/**
39
	 * @var int
40
	 */
41
	private $_interestPartTemp = 0;
42
43
	/**
44
	 * init param
45
	 *
46
	 * @param int $config['loanAmount']
0 ignored issues
show
Documentation introduced by
There is no parameter named $config['loanAmount']. Did you maybe mean $config?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
47
	 * @param int $config['year']
0 ignored issues
show
Documentation introduced by
There is no parameter named $config['year']. Did you maybe mean $config?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
48
	 * @param int $config['interestRate']
0 ignored issues
show
Documentation introduced by
There is no parameter named $config['interestRate']. Did you maybe mean $config?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
49
	 * @param int $config['interestRateChangeIndex']
0 ignored issues
show
Documentation introduced by
There is no parameter named $config['interestRateChangeIndex']. Did you maybe mean $config?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
50
	 *
51
	 */
52 9
	public function __construct($config)
53
	{
54 9
		if (isset($config['loanAmount']) && !empty($config['loanAmount'])) {
55 9
			$this->loanAmount = $config['loanAmount'];
56 9
		}
57 9 View Code Duplication
		if (isset($config['year']) && !empty($config['year'])) {
0 ignored issues
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 9
			$this->year = $config['year'];
59 9
		}
60 9 View Code Duplication
		if (isset($config['interestRateChangeIndex']) && !empty($config['interestRateChangeIndex'])) {
0 ignored issues
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...
61 3
			$this->interestRateChangeIndex = $config['interestRateChangeIndex'];
62 3
		}
63 9
		if (isset($config['interestRate']) && !empty($config['interestRate'])) {
64 3
			$this->interestRate = $config['interestRate']/100;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config['interestRate'] / 100 can also be of type integer. However, the property $interestRate is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
65 3
		} else {
66 6
			$this->_getinterestRate();
67
		}
68 9
		$this->_monthlyinterestRate = $this->interestRate/12;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->interestRate / 12 can also be of type double. However, the property $_monthlyinterestRate is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
69 9
		$this->period = $this->year*12;
0 ignored issues
show
Bug introduced by
The property period does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
70 9
	}
71
72
	/**
73
	 * Equal Loan Payments
74
	 *
75
	 * @return array
76
	 *
77
	 */
78 9
	public function getELP()
79
	{
80 9
		$output = [];
81 9
		$paymentAmount = $this->loanAmount*($this->_monthlyinterestRate*pow(1+$this->_monthlyinterestRate, $this->year*12))/(pow(1+$this->_monthlyinterestRate, $this->year*12)-1); // Payment Amount
82 9
		$loanAmount = $this->loanAmount;
83 9
		$initPrincipalPart = 0; // Init Prncipal Part
84 9
		for ($i=1; $i <= $this->period; $i++) {
85 9
			$loanAmount = $loanAmount - $initPrincipalPart;
86 9
			$interestPart = $loanAmount*$this->_monthlyinterestRate;
87 9
			$output['period'][$i]['ip'] = sprintf("%.2f", $interestPart); // Interest Part
88 9
			$output['period'][$i]['pa'] = sprintf("%.2f", $paymentAmount); // Payment Amount
89 9
			$principal = $initPrincipalPart = $paymentAmount - $interestPart;
90 9
			$output['period'][$i]['pp'] = sprintf("%.2f", $principal); // Principal Part
91 9
			$output['period'][$i]['bo'] = sprintf("%.2f", abs($loanAmount-$principal)); // Banlance Owed
92 9
		}
93 9
		return $output;
94
	}
95
96
	/**
97
	 * Equal Principal Payments
98
	 *
99
	 * @return array
100
	 *
101
	 */
102 9
	public function getEPP()
103
	{
104 9
		$output = [];
105 9
		$principalPart = $this->loanAmount/($this->year*12); // Principal Part
106 9
		$loanAmountInterest = 0;
107 9
		$loanAmount = $this->loanAmount;
108 9
		$equalAll = $equalItem = 0;
109 9
		for ($i=1; $i <= $this->period; $i++) {
110 9
			if ($i > 1)
111 9
				$loanAmount = $loanAmount - $principalPart;
112 9
			$interestPart = $loanAmount*$this->_monthlyinterestRate;
113 9
			$loanAmountInterest += $interestPart;
114 9
			$output['period'][$i]['ip'] = sprintf("%.2f", $interestPart); // Interest Part
115 9
			$output['period'][$i]['pp'] = sprintf("%.2f", $principalPart); // Principal Part
116 9
			$output['period'][$i]['pa'] = sprintf("%.2f", $principalPart+$interestPart); // Payment Amount
117 9
			$output['period'][$i]['bo'] = sprintf("%.2f", $loanAmount - $principalPart); // Balance Owed
118 9
			$i > 1 && $equalItem = $this->_interestPartTemp - $interestPart;
119 9
			$this->_interestPartTemp = $interestPart;
120 9
			$equalAll += $equalItem;
121 9
		}
122 9
		$output['ti'] = sprintf("%.2f", $loanAmountInterest); // loanAmount Interest
123 9
		$output['tp'] = sprintf("%.2f", $this->loanAmount+$loanAmountInterest); // loanAmount Payments
124 9
		$output['equal'] = sprintf("%.2f", $equalAll/($this->year*12 - 1));
125 9
		return $output;
126
	}
127
128
	/**
129
	 * Get ank interestRate
130
	 */
131 6
	private function _getinterestRate()
132
	{
133 6
		$config = require(__DIR__.'/config.php');
134 6
		if ($this->interestRateChangeIndex) {
135 3
			if (isset($config[$this->bank][$this->interestRateChangeIndex])) {
136 3
				$currConfig = $config[$this->bank][$this->interestRateChangeIndex];
137 3
			}
138 3
		} else {
139 3
			$currConfig = end($config[$this->bank]);
140
		}
141
142 6
		if (empty($this->interestRate)) {
143 6
			if ($this->year <= 0.6) {
144
				$interestRate = $currConfig[0];
0 ignored issues
show
Bug introduced by
The variable $currConfig does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
145 6
			} elseif ($this->year <= 1) {
146
				$interestRate = $currConfig[1];
147 6
			} elseif ($this->year <= 3) {
148
				$interestRate = $currConfig[2];
149 6
			} elseif ($this->year <= 5) {
150 3
				$interestRate = $currConfig[3];
151 3
			} else {
152 3
				$interestRate = $currConfig[4];
153
			}
154 6
			$this->interestRate = $interestRate/100;
0 ignored issues
show
Documentation Bug introduced by
It seems like $interestRate / 100 can also be of type integer. However, the property $interestRate is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
155 6
		}
156 6
	}
157
}
158