Passed
Push — master ( 5dee5a...729c4d )
by
unknown
20:09 queued 09:33
created

AccruedInterest::periodic()   B

Complexity

Conditions 7
Paths 80

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7.0601

Importance

Changes 0
Metric Value
cc 7
eloc 28
nc 80
nop 8
dl 0
loc 46
rs 8.5386
c 0
b 0
f 0
ccs 25
cts 28
cp 0.8929
crap 7.0601

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 PhpOffice\PhpSpreadsheet\Calculation\Financial\Securities;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\YearFrac;
6
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
7
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
8
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
9
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
10
11
class AccruedInterest
12
{
13
    public const ACCRINT_CALCMODE_ISSUE_TO_SETTLEMENT = true;
14
15
    public const ACCRINT_CALCMODE_FIRST_INTEREST_TO_SETTLEMENT = false;
16
17
    /**
18
     * ACCRINT.
19
     *
20
     * Returns the accrued interest for a security that pays periodic interest.
21
     *
22
     * Excel Function:
23
     *        ACCRINT(issue,firstinterest,settlement,rate,par,frequency[,basis][,calc_method])
24
     *
25
     * @param mixed $issue the security's issue date
26
     * @param mixed $firstInterest the security's first interest date
27
     * @param mixed $settlement The security's settlement date.
28
     *                              The security settlement date is the date after the issue date
29
     *                                  when the security is traded to the buyer.
30
     * @param mixed $rate The security's annual coupon rate
31
     * @param mixed $parValue The security's par value.
32
     *                            If you omit par, ACCRINT uses $1,000.
33
     * @param mixed $frequency The number of coupon payments per year.
34
     *                             Valid frequency values are:
35
     *                               1    Annual
36
     *                               2    Semi-Annual
37
     *                               4    Quarterly
38
     * @param mixed $basis The type of day count to use.
39
     *                         0 or omitted    US (NASD) 30/360
40
     *                         1               Actual/actual
41
     *                         2               Actual/360
42
     *                         3               Actual/365
43
     *                         4               European 30/360
44
     * @param mixed $calcMethod Unused by PhpSpreadsheet, and apparently by Excel (https://exceljet.net/functions/accrint-function)
45
     *
46
     * @return float|string Result, or a string containing an error
47
     */
48 22
    public static function periodic(
49
        mixed $issue,
50
        mixed $firstInterest,
51
        mixed $settlement,
52
        mixed $rate,
53
        mixed $parValue = 1000,
54
        mixed $frequency = FinancialConstants::FREQUENCY_ANNUAL,
55
        mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD,
56
        mixed $calcMethod = self::ACCRINT_CALCMODE_ISSUE_TO_SETTLEMENT
57
    ) {
58 22
        $issue = Functions::flattenSingleValue($issue);
59 22
        $firstInterest = Functions::flattenSingleValue($firstInterest);
60 22
        $settlement = Functions::flattenSingleValue($settlement);
61 22
        $rate = Functions::flattenSingleValue($rate);
62 22
        $parValue = ($parValue === null) ? 1000 : Functions::flattenSingleValue($parValue);
63 22
        $frequency = ($frequency === null)
64
            ? FinancialConstants::FREQUENCY_ANNUAL
65 22
            : Functions::flattenSingleValue($frequency);
66 22
        $basis = ($basis === null)
67 1
            ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
68 21
            : Functions::flattenSingleValue($basis);
69
70
        try {
71 22
            $issue = SecurityValidations::validateIssueDate($issue);
72 21
            $settlement = SecurityValidations::validateSettlementDate($settlement);
73 21
            SecurityValidations::validateSecurityPeriod($issue, $settlement);
74 21
            $rate = SecurityValidations::validateRate($rate);
75 17
            $parValue = SecurityValidations::validateParValue($parValue);
76 14
            SecurityValidations::validateFrequency($frequency);
77 13
            $basis = SecurityValidations::validateBasis($basis);
78 11
        } catch (Exception $e) {
79 11
            return $e->getMessage();
80
        }
81
82 11
        $daysBetweenIssueAndSettlement = Functions::scalar(YearFrac::fraction($issue, $settlement, $basis));
83 11
        if (!is_numeric($daysBetweenIssueAndSettlement)) {
84
            //    return date error
85
            return StringHelper::convertToString($daysBetweenIssueAndSettlement);
86
        }
87 11
        $daysBetweenFirstInterestAndSettlement = Functions::scalar(YearFrac::fraction($firstInterest, $settlement, $basis));
88 11
        if (!is_numeric($daysBetweenFirstInterestAndSettlement)) {
89
            //    return date error
90
            return StringHelper::convertToString($daysBetweenFirstInterestAndSettlement);
91
        }
92
93 11
        return $parValue * $rate * $daysBetweenIssueAndSettlement;
94
    }
95
96
    /**
97
     * ACCRINTM.
98
     *
99
     * Returns the accrued interest for a security that pays interest at maturity.
100
     *
101
     * Excel Function:
102
     *        ACCRINTM(issue,settlement,rate[,par[,basis]])
103
     *
104
     * @param mixed $issue The security's issue date
105
     * @param mixed $settlement The security's settlement (or maturity) date
106
     * @param mixed $rate The security's annual coupon rate
107
     * @param mixed $parValue The security's par value.
108
     *                            If you omit parValue, ACCRINT uses $1,000.
109
     * @param mixed $basis The type of day count to use.
110
     *                         0 or omitted    US (NASD) 30/360
111
     *                         1               Actual/actual
112
     *                         2               Actual/360
113
     *                         3               Actual/365
114
     *                         4               European 30/360
115
     *
116
     * @return float|string Result, or a string containing an error
117
     */
118 14
    public static function atMaturity(
119
        mixed $issue,
120
        mixed $settlement,
121
        mixed $rate,
122
        mixed $parValue = 1000,
123
        mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
124
    ) {
125 14
        $issue = Functions::flattenSingleValue($issue);
126 14
        $settlement = Functions::flattenSingleValue($settlement);
127 14
        $rate = Functions::flattenSingleValue($rate);
128 14
        $parValue = ($parValue === null) ? 1000 : Functions::flattenSingleValue($parValue);
129 14
        $basis = ($basis === null)
130 1
            ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
131 13
            : Functions::flattenSingleValue($basis);
132
133
        try {
134 14
            $issue = SecurityValidations::validateIssueDate($issue);
135 13
            $settlement = SecurityValidations::validateSettlementDate($settlement);
136 13
            SecurityValidations::validateSecurityPeriod($issue, $settlement);
137 13
            $rate = SecurityValidations::validateRate($rate);
138 10
            $parValue = SecurityValidations::validateParValue($parValue);
139 8
            $basis = SecurityValidations::validateBasis($basis);
140 8
        } catch (Exception $e) {
141 8
            return $e->getMessage();
142
        }
143
144 6
        $daysBetweenIssueAndSettlement = Functions::scalar(YearFrac::fraction($issue, $settlement, $basis));
145 6
        if (!is_numeric($daysBetweenIssueAndSettlement)) {
146
            //    return date error
147
            return StringHelper::convertToString($daysBetweenIssueAndSettlement);
148
        }
149
150 6
        return $parValue * $rate * $daysBetweenIssueAndSettlement;
151
    }
152
}
153