Failed Conditions
Pull Request — master (#8)
by Kauri
03:50
created

PaymentsCalculatorTest::testCalculatePaymentsNew()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 17
nc 2
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Kauri\Loan\Test;
4
5
6
use Kauri\Loan\InterestAmountCalculator;
7
use Kauri\Loan\PaymentAmountCalculator\AnnuityPaymentAmountCalculator;
8
use Kauri\Loan\PaymentAmountCalculator\EqualPrincipalPaymentAmountCalculator;
9
use Kauri\Loan\PaymentAmountCalculatorInterface;
10
use Kauri\Loan\PaymentPeriodsFactory;
11
use Kauri\Loan\PaymentsCalculator;
12
use Kauri\Loan\PaymentScheduleConfig;
13
use Kauri\Loan\PaymentScheduleFactory;
14
use Kauri\Loan\PeriodInterface;
15
use PHPUnit\Framework\TestCase;
16
17
class PaymentsCalculatorTest extends TestCase
18
{
19
    /**
20
     * @dataProvider dataLoader
21
     * @param $principal
22
     * @param $noOfPayments
23
     * @param $interestRate
24
     * @param $pattern
25
     * @param PaymentAmountCalculatorInterface $paymentAmountCalculator
26
     * @param $calculationMode
27
     * @param array $expectedPaymentAmounts
28
     */
29
    public function testCalculatePaymentsNew(
30
        $principal,
31
        $futureValue,
32
        $noOfPayments,
33
        $interestRate,
34
        $pattern,
35
        PaymentAmountCalculatorInterface $paymentAmountCalculator,
36
        $calculationMode,
37
        array $expectedPaymentAmounts
38
    ) {
39
        $interestAmountCalculator = new InterestAmountCalculator;
40
41
        $config = new PaymentScheduleConfig($noOfPayments, new \DateTime("2016-01-01"), $pattern);
42
        $schedule = PaymentScheduleFactory::generate($config);
43
        $periods = PaymentPeriodsFactory::generate($schedule);
44
        $paymentsCalculator = new PaymentsCalculator($paymentAmountCalculator, $interestAmountCalculator);
45
46
        $payments = $paymentsCalculator->calculatePayments($periods, $principal, $interestRate, $calculationMode, $futureValue);
47
48
        foreach ($payments as $k => $pmt) {
49
            $this->assertEquals($expectedPaymentAmounts[$k], $pmt['payment']);
50
        }
51
    }
52
53
    public function dataLoader(): array
54
    {
55
        $annuityPaymentAmountCalculator = new AnnuityPaymentAmountCalculator();
56
        $equalPaymentAmountCalculator = new EqualPrincipalPaymentAmountCalculator();
57
58
        $averageCalculationMode = PeriodInterface::LENGTH_MODE_AVG;
59
        $exactCalculationMode = PeriodInterface::LENGTH_MODE_EXACT;
60
61
62
        return [
63
            /* Annuity payments */
64
            // average interest
65
            [
66
                6000,
67
                0,
68
                5,
69
                360,
70
                'P1M',
71
                $annuityPaymentAmountCalculator,
72
                $averageCalculationMode,
73
                [1 => 2463.49, 2463.49, 2463.49, 2463.49, 2463.47]
74
            ],
75
            // exact interest
76
            [
77
                6000,
78
                0,
79
                5,
80
                360,
81
                'P1M',
82
                $annuityPaymentAmountCalculator,
83
                $exactCalculationMode,
84
                [1 => 2480.94, 2480.94, 2480.94, 2480.94, 2470.53]
85
            ],
86
            /* Equal principal payments */
87
            // average interest
88
            [
89
                6000,
90
                0,
91
                5,
92
                360,
93
                'P1M',
94
                $equalPaymentAmountCalculator,
95
                $averageCalculationMode,
96
                [1 => 3000, 2640, 2280, 1920, 1560]
97
            ],
98
            // exact payment
99
            /*[
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
100
                6000,
101
                5,
102
                360,
103
                'P1M',
104
                $equalPaymentAmountCalculator,
105
                $exactCalculationMode,
106
                [3060, 2592, 2316, 1920, 1572]
107
            ],*/
108
        ];
109
    }
110
}
111