Failed Conditions
Pull Request — master (#3743)
by Adrien
14:08
created

AccruedInterest::doNothing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 23
            ? FinancialConstants::FREQUENCY_ANNUAL
63
            : Functions::flattenSingleValue($frequency);
64 23
        $basis = ($basis === null)
65 23
            ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
66 1
            : Functions::flattenSingleValue($basis);
67 22
68
        try {
69
            $issue = SecurityValidations::validateIssueDate($issue);
70 23
            $settlement = SecurityValidations::validateSettlementDate($settlement);
71 22
            SecurityValidations::validateSecurityPeriod($issue, $settlement);
72 22
            $rate = SecurityValidations::validateRate($rate);
73 22
            $parValue = SecurityValidations::validateParValue($parValue);
74 18
            SecurityValidations::validateFrequency($frequency);
75 15
            $basis = SecurityValidations::validateBasis($basis);
76 14
        } catch (Exception $e) {
77 14
            return $e->getMessage();
78 11
        }
79 11
80
        $daysBetweenIssueAndSettlement = Functions::scalar(YearFrac::fraction($issue, $settlement, $basis));
81
        if (!is_numeric($daysBetweenIssueAndSettlement)) {
82 12
            //    return date error
83 12
            return $daysBetweenIssueAndSettlement;
84
        }
85
        $daysBetweenFirstInterestAndSettlement = Functions::scalar(YearFrac::fraction($firstInterest, $settlement, $basis));
86
        if (!is_numeric($daysBetweenFirstInterestAndSettlement)) {
87 12
            //    return date error
88 12
            return $daysBetweenFirstInterestAndSettlement;
89
        }
90
91
        return $parValue * $rate * $daysBetweenIssueAndSettlement;
92
    }
93 12
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
    public static function atMaturity(
117
        mixed $issue,
118 15
        mixed $settlement,
119
        mixed $rate,
120
        mixed $parValue = 1000,
121
        mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
122
    ) {
123
        $issue = Functions::flattenSingleValue($issue);
124
        $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 15
            ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
129 15
            : Functions::flattenSingleValue($basis);
130 1
131 14
        try {
132
            $issue = SecurityValidations::validateIssueDate($issue);
133
            $settlement = SecurityValidations::validateSettlementDate($settlement);
134 15
            SecurityValidations::validateSecurityPeriod($issue, $settlement);
135 14
            $rate = SecurityValidations::validateRate($rate);
136 14
            $parValue = SecurityValidations::validateParValue($parValue);
137 14
            $basis = SecurityValidations::validateBasis($basis);
138 11
        } catch (Exception $e) {
139 9
            return $e->getMessage();
140 8
        }
141 8
142
        $daysBetweenIssueAndSettlement = Functions::scalar(YearFrac::fraction($issue, $settlement, $basis));
143
        if (!is_numeric($daysBetweenIssueAndSettlement)) {
144 7
            //    return date error
145 7
            return $daysBetweenIssueAndSettlement;
146
        }
147
148
        return $parValue * $rate * $daysBetweenIssueAndSettlement;
149
    }
150
}
151