Failed Conditions
Push — master ( ea97af...216db0 )
by
unknown
15:51 queued 07:40
created

Rates   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 92.5%

Importance

Changes 0
Metric Value
wmc 8
eloc 41
c 0
b 0
f 0
dl 0
loc 118
ccs 37
cts 40
cp 0.925
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A interest() 0 35 4
A discount() 0 35 4
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial\Securities;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
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\Calculation\Information\ExcelError;
10
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
11
12
class Rates
13
{
14
    /**
15
     * DISC.
16
     *
17
     * Returns the discount rate for a security.
18
     *
19
     * Excel Function:
20
     *        DISC(settlement,maturity,price,redemption[,basis])
21
     *
22
     * @param mixed $settlement The security's settlement date.
23
     *                              The security settlement date is the date after the issue
24
     *                                  date when the security is traded to the buyer.
25
     * @param mixed $maturity The security's maturity date.
26
     *                            The maturity date is the date when the security expires.
27
     * @param mixed $price The security's price per $100 face value
28
     * @param mixed $redemption The security's redemption value per $100 face value
29
     * @param mixed $basis The type of day count to use.
30
     *                         0 or omitted    US (NASD) 30/360
31
     *                         1               Actual/actual
32
     *                         2               Actual/360
33
     *                         3               Actual/365
34
     *                         4               European 30/360
35
     */
36 7
    public static function discount(
37
        mixed $settlement,
38
        mixed $maturity,
39
        mixed $price,
40
        mixed $redemption,
41
        mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
42
    ): float|string {
43 7
        $settlement = Functions::flattenSingleValue($settlement);
44 7
        $maturity = Functions::flattenSingleValue($maturity);
45 7
        $price = Functions::flattenSingleValue($price);
46 7
        $redemption = Functions::flattenSingleValue($redemption);
47 7
        $basis = Functions::flattenSingleValue($basis) ?? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD;
48
49
        try {
50 7
            $settlement = SecurityValidations::validateSettlementDate($settlement);
51 6
            $maturity = SecurityValidations::validateMaturityDate($maturity);
52 6
            SecurityValidations::validateSecurityPeriod($settlement, $maturity);
53 6
            $price = SecurityValidations::validatePrice($price);
54 5
            $redemption = SecurityValidations::validateRedemption($redemption);
55 5
            $basis = SecurityValidations::validateBasis($basis);
56 2
        } catch (Exception $e) {
57 2
            return $e->getMessage();
58
        }
59
60 5
        if ($price <= 0.0) {
61 1
            return ExcelError::NAN();
62
        }
63
64 4
        $daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis));
65 4
        if (!is_numeric($daysBetweenSettlementAndMaturity)) {
66
            //    return date error
67
            return StringHelper::convertToString($daysBetweenSettlementAndMaturity);
68
        }
69
70 4
        return (1 - $price / $redemption) / $daysBetweenSettlementAndMaturity;
71
    }
72
73
    /**
74
     * INTRATE.
75
     *
76
     * Returns the interest rate for a fully invested security.
77
     *
78
     * Excel Function:
79
     *        INTRATE(settlement,maturity,investment,redemption[,basis])
80
     *
81
     * @param mixed $settlement The security's settlement date.
82
     *                              The security settlement date is the date after the issue date when the security
83
     *                                  is traded to the buyer.
84
     * @param mixed $maturity The security's maturity date.
85
     *                            The maturity date is the date when the security expires.
86
     * @param mixed $investment the amount invested in the security
87
     * @param mixed $redemption the amount to be received at maturity
88
     * @param mixed $basis The type of day count to use.
89
     *                         0 or omitted    US (NASD) 30/360
90
     *                         1               Actual/actual
91
     *                         2               Actual/360
92
     *                         3               Actual/365
93
     *                         4               European 30/360
94
     */
95 7
    public static function interest(
96
        mixed $settlement,
97
        mixed $maturity,
98
        mixed $investment,
99
        mixed $redemption,
100
        mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
101
    ): float|string {
102 7
        $settlement = Functions::flattenSingleValue($settlement);
103 7
        $maturity = Functions::flattenSingleValue($maturity);
104 7
        $investment = Functions::flattenSingleValue($investment);
105 7
        $redemption = Functions::flattenSingleValue($redemption);
106 7
        $basis = Functions::flattenSingleValue($basis) ?? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD;
107
108
        try {
109 7
            $settlement = SecurityValidations::validateSettlementDate($settlement);
110 6
            $maturity = SecurityValidations::validateMaturityDate($maturity);
111 6
            SecurityValidations::validateSecurityPeriod($settlement, $maturity);
112 6
            $investment = SecurityValidations::validateFloat($investment);
113 6
            $redemption = SecurityValidations::validateRedemption($redemption);
114 5
            $basis = SecurityValidations::validateBasis($basis);
115 3
        } catch (Exception $e) {
116 3
            return $e->getMessage();
117
        }
118
119 4
        if ($investment <= 0) {
120
            return ExcelError::NAN();
121
        }
122
123 4
        $daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis));
124 4
        if (!is_numeric($daysBetweenSettlementAndMaturity)) {
125
            //    return date error
126
            return StringHelper::convertToString($daysBetweenSettlementAndMaturity);
127
        }
128
129 4
        return (($redemption / $investment) - 1) / ($daysBetweenSettlementAndMaturity);
130
    }
131
}
132