Failed Conditions
Push — master ( 07c60b...7328e1 )
by Adrien
12:29
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
10
class AccruedInterest
11
{
12
    public const ACCRINT_CALCMODE_ISSUE_TO_SETTLEMENT = true;
13
14
    public const ACCRINT_CALCMODE_FIRST_INTEREST_TO_SETTLEMENT = false;
15
16
    /**
17
     * ACCRINT.
18
     *
19
     * Returns the accrued interest for a security that pays periodic interest.
20
     *
21
     * Excel Function:
22
     *        ACCRINT(issue,firstinterest,settlement,rate,par,frequency[,basis][,calc_method])
23
     *
24
     * @param mixed $issue the security's issue date
25
     * @param mixed $firstInterest the security's first interest date
26
     * @param mixed $settlement The security's settlement date.
27
     *                              The security settlement date is the date after the issue date
28
     *                                  when the security is traded to the buyer.
29
     * @param mixed $rate The security's annual coupon rate
30
     * @param mixed $parValue The security's par value.
31
     *                            If you omit par, ACCRINT uses $1,000.
32
     * @param mixed $frequency The number of coupon payments per year.
33
     *                             Valid frequency values are:
34
     *                               1    Annual
35
     *                               2    Semi-Annual
36
     *                               4    Quarterly
37
     * @param mixed $basis The type of day count to use.
38
     *                         0 or omitted    US (NASD) 30/360
39
     *                         1               Actual/actual
40
     *                         2               Actual/360
41
     *                         3               Actual/365
42
     *                         4               European 30/360
43
     *
44
     * @return float|string Result, or a string containing an error
45
     */
46 23
    public static function periodic(
47
        mixed $issue,
48
        mixed $firstInterest,
49
        mixed $settlement,
50
        mixed $rate,
51
        mixed $parValue = 1000,
52
        mixed $frequency = FinancialConstants::FREQUENCY_ANNUAL,
53
        mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD,
54
        mixed $calcMethod = self::ACCRINT_CALCMODE_ISSUE_TO_SETTLEMENT
1 ignored issue
show
Unused Code introduced by
The parameter $calcMethod is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

54
        /** @scrutinizer ignore-unused */ mixed $calcMethod = self::ACCRINT_CALCMODE_ISSUE_TO_SETTLEMENT

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

Loading history...
55
    ) {
56 23
        $issue = Functions::flattenSingleValue($issue);
57 23
        $firstInterest = Functions::flattenSingleValue($firstInterest);
58 23
        $settlement = Functions::flattenSingleValue($settlement);
59 23
        $rate = Functions::flattenSingleValue($rate);
60 23
        $parValue = ($parValue === null) ? 1000 : Functions::flattenSingleValue($parValue);
61 23
        $frequency = ($frequency === null)
62
            ? FinancialConstants::FREQUENCY_ANNUAL
63 23
            : Functions::flattenSingleValue($frequency);
64 23
        $basis = ($basis === null)
65 1
            ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
66 22
            : Functions::flattenSingleValue($basis);
67
68
        try {
69 23
            $issue = SecurityValidations::validateIssueDate($issue);
70 22
            $settlement = SecurityValidations::validateSettlementDate($settlement);
71 22
            SecurityValidations::validateSecurityPeriod($issue, $settlement);
72 22
            $rate = SecurityValidations::validateRate($rate);
73 18
            $parValue = SecurityValidations::validateParValue($parValue);
74 15
            SecurityValidations::validateFrequency($frequency);
75 14
            $basis = SecurityValidations::validateBasis($basis);
76 11
        } catch (Exception $e) {
77 11
            return $e->getMessage();
78
        }
79
80 12
        $daysBetweenIssueAndSettlement = Functions::scalar(YearFrac::fraction($issue, $settlement, $basis));
81 12
        if (!is_numeric($daysBetweenIssueAndSettlement)) {
82
            //    return date error
83
            return $daysBetweenIssueAndSettlement;
84
        }
85 12
        $daysBetweenFirstInterestAndSettlement = Functions::scalar(YearFrac::fraction($firstInterest, $settlement, $basis));
86 12
        if (!is_numeric($daysBetweenFirstInterestAndSettlement)) {
87
            //    return date error
88
            return $daysBetweenFirstInterestAndSettlement;
89
        }
90
91 12
        return $parValue * $rate * $daysBetweenIssueAndSettlement;
92
    }
93
94
    /**
95
     * ACCRINTM.
96
     *
97
     * Returns the accrued interest for a security that pays interest at maturity.
98
     *
99
     * Excel Function:
100
     *        ACCRINTM(issue,settlement,rate[,par[,basis]])
101
     *
102
     * @param mixed $issue The security's issue date
103
     * @param mixed $settlement The security's settlement (or maturity) date
104
     * @param mixed $rate The security's annual coupon rate
105
     * @param mixed $parValue The security's par value.
106
     *                            If you omit parValue, ACCRINT uses $1,000.
107
     * @param mixed $basis The type of day count to use.
108
     *                         0 or omitted    US (NASD) 30/360
109
     *                         1               Actual/actual
110
     *                         2               Actual/360
111
     *                         3               Actual/365
112
     *                         4               European 30/360
113
     *
114
     * @return float|string Result, or a string containing an error
115
     */
116 15
    public static function atMaturity(
117
        mixed $issue,
118
        mixed $settlement,
119
        mixed $rate,
120
        mixed $parValue = 1000,
121
        mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
122
    ) {
123 15
        $issue = Functions::flattenSingleValue($issue);
124 15
        $settlement = Functions::flattenSingleValue($settlement);
125 15
        $rate = Functions::flattenSingleValue($rate);
126 15
        $parValue = ($parValue === null) ? 1000 : Functions::flattenSingleValue($parValue);
127 15
        $basis = ($basis === null)
128 1
            ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
129 14
            : Functions::flattenSingleValue($basis);
130
131
        try {
132 15
            $issue = SecurityValidations::validateIssueDate($issue);
133 14
            $settlement = SecurityValidations::validateSettlementDate($settlement);
134 14
            SecurityValidations::validateSecurityPeriod($issue, $settlement);
135 14
            $rate = SecurityValidations::validateRate($rate);
136 11
            $parValue = SecurityValidations::validateParValue($parValue);
137 9
            $basis = SecurityValidations::validateBasis($basis);
138 8
        } catch (Exception $e) {
139 8
            return $e->getMessage();
140
        }
141
142 7
        $daysBetweenIssueAndSettlement = Functions::scalar(YearFrac::fraction($issue, $settlement, $basis));
143 7
        if (!is_numeric($daysBetweenIssueAndSettlement)) {
144
            //    return date error
145
            return $daysBetweenIssueAndSettlement;
146
        }
147
148 7
        return $parValue * $rate * $daysBetweenIssueAndSettlement;
149
    }
150
}
151