Failed Conditions
Pull Request — master (#8)
by Kauri
02:29
created

PaymentsCalculatorTest::testCalculatePaymentsNew()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 18
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 $futureValue
23
     * @param $noOfPayments
24
     * @param $interestRate
25
     * @param $pattern
26
     * @param PaymentAmountCalculatorInterface $paymentAmountCalculator
27
     * @param $calculationMode
28
     * @param array $expectedPaymentAmounts
29
     */
30
    public function testCalculatePaymentsNew(
31
        $principal,
32
        $futureValue,
33
        $noOfPayments,
34
        $interestRate,
35
        $pattern,
36
        PaymentAmountCalculatorInterface $paymentAmountCalculator,
37
        $calculationMode,
38
        array $expectedPaymentAmounts
39
    ) {
40
        $interestAmountCalculator = new InterestAmountCalculator;
41
42
        $config = new PaymentScheduleConfig($noOfPayments, new \DateTime("2016-01-01"), $pattern);
43
        $schedule = PaymentScheduleFactory::generate($config);
44
        $periods = PaymentPeriodsFactory::generate($schedule);
45
        $paymentsCalculator = new PaymentsCalculator($paymentAmountCalculator, $interestAmountCalculator);
46
47
        $payments = $paymentsCalculator->calculatePayments($periods, $principal, $interestRate, $calculationMode,
48
            $futureValue);
49
50
        foreach ($payments as $k => $pmt) {
51
            $this->assertEquals($expectedPaymentAmounts[$k], $pmt['payment']);
52
        }
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function dataLoader(): array
59
    {
60
        $interestAmountCalculator = new InterestAmountCalculator;
61
62
        $annuityPaymentAmountCalculator = new AnnuityPaymentAmountCalculator($interestAmountCalculator);
1 ignored issue
show
Unused Code introduced by
The call to AnnuityPaymentAmountCalculator::__construct() has too many arguments starting with $interestAmountCalculator.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
63
        $equalPaymentAmountCalculator = new EqualPrincipalPaymentAmountCalculator($interestAmountCalculator);
1 ignored issue
show
Unused Code introduced by
The call to EqualPrincipalPaymentAmo...lculator::__construct() has too many arguments starting with $interestAmountCalculator.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
64
65
        $averageCalculationMode = PeriodInterface::LENGTH_MODE_AVG;
66
        $exactCalculationMode = PeriodInterface::LENGTH_MODE_EXACT;
67
68
        return [
69
            /* Annuity payments */
70
            // average interest
71
            [
72
                6000,
73
                0,
74
                5,
75
                360,
76
                'P1M',
77
                $annuityPaymentAmountCalculator,
78
                $averageCalculationMode,
79
                [1 => 2463.49, 2463.49, 2463.49, 2463.49, 2463.47]
80
            ],
81
            // exact interest
82
            [
83
                6000,
84
                0,
85
                5,
86
                360,
87
                'P1M',
88
                $annuityPaymentAmountCalculator,
89
                $exactCalculationMode,
90
                [1 => 2480.94, 2480.94, 2480.94, 2480.94, 2470.53]
91
            ],
92
            /* Equal principal payments */
93
            // average interest
94
            [
95
                6000,
96
                0,
97
                5,
98
                360,
99
                'P1M',
100
                $equalPaymentAmountCalculator,
101
                $averageCalculationMode,
102
                [1 => 3000, 2640, 2280, 1920, 1560]
103
            ],
104
            // exact payment
105
            [
106
                6000,
107
                0,
108
                5,
109
                360,
110
                'P1M',
111
                $equalPaymentAmountCalculator,
112
                $exactCalculationMode,
113
                [1 => 3060, 2592, 2316, 1920, 1572]
114
            ]
115
        ];
116
    }
117
}
118