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

Amortization::AMORLINC()   B

Complexity

Conditions 11
Paths 24

Size

Total Lines 64
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 11.002

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 64
ccs 38
cts 39
cp 0.9744
rs 7.3166
c 0
b 0
f 0
cc 11
nc 24
nop 7
crap 11.002

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial;
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
10
class Amortization
11
{
12
    /**
13
     * AMORDEGRC.
14
     *
15
     * Returns the depreciation for each accounting period.
16
     * This function is provided for the French accounting system. If an asset is purchased in
17
     * the middle of the accounting period, the prorated depreciation is taken into account.
18
     * The function is similar to AMORLINC, except that a depreciation coefficient is applied in
19
     * the calculation depending on the life of the assets.
20
     * This function will return the depreciation until the last period of the life of the assets
21
     * or until the cumulated value of depreciation is greater than the cost of the assets minus
22
     * the salvage value.
23
     *
24
     * Excel Function:
25
     *        AMORDEGRC(cost,purchased,firstPeriod,salvage,period,rate[,basis])
26
     *
27
     * @param mixed $cost The float cost of the asset
28
     * @param mixed $purchased Date of the purchase of the asset
29
     * @param mixed $firstPeriod Date of the end of the first period
30
     * @param mixed $salvage The salvage value at the end of the life of the asset
31
     * @param mixed $period the period (float)
32
     * @param mixed $rate rate of depreciation (float)
33
     * @param mixed $basis The type of day count to use (int).
34
     *                         0 or omitted    US (NASD) 30/360
35
     *                         1               Actual/actual
36
     *                         2               Actual/360
37
     *                         3               Actual/365
38
     *                         4               European 30/360
39
     *
40
     * @return float|string (string containing the error type if there is an error)
41
     */
42 24
    public static function AMORDEGRC(
43
        mixed $cost,
44
        mixed $purchased,
45
        mixed $firstPeriod,
46
        mixed $salvage,
47
        mixed $period,
48
        mixed $rate,
49
        mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
50
    ): string|float {
51 24
        $cost = Functions::flattenSingleValue($cost);
52 24
        $purchased = Functions::flattenSingleValue($purchased);
53 24
        $firstPeriod = Functions::flattenSingleValue($firstPeriod);
54 24
        $salvage = Functions::flattenSingleValue($salvage);
55 24
        $period = Functions::flattenSingleValue($period);
56 24
        $rate = Functions::flattenSingleValue($rate);
57 24
        $basis = Functions::flattenSingleValue($basis) ?? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD;
58
59
        try {
60 24
            $cost = FinancialValidations::validateFloat($cost);
61 23
            $purchased = FinancialValidations::validateDate($purchased);
62 22
            $firstPeriod = FinancialValidations::validateDate($firstPeriod);
63 21
            $salvage = FinancialValidations::validateFloat($salvage);
64 20
            $period = FinancialValidations::validateInt($period);
65 19
            $rate = FinancialValidations::validateFloat($rate);
66 18
            $basis = FinancialValidations::validateBasis($basis);
67 8
        } catch (Exception $e) {
68 8
            return $e->getMessage();
69
        }
70
71 16
        $yearFracx = DateTimeExcel\YearFrac::fraction($purchased, $firstPeriod, $basis);
72 16
        if (is_string($yearFracx)) {
73
            return $yearFracx;
74
        }
75
        /** @var float $yearFrac */
76 16
        $yearFrac = $yearFracx;
77
78 16
        $amortiseCoeff = self::getAmortizationCoefficient($rate);
79
80 16
        $rate *= $amortiseCoeff;
81 16
        $rate = (float) (string) $rate; // ugly way to avoid rounding problem
82 16
        $fNRate = round($yearFrac * $rate * $cost, 0);
83 16
        $cost -= $fNRate;
84 16
        $fRest = $cost - $salvage;
85
86 16
        for ($n = 0; $n < $period; ++$n) {
87 16
            $fNRate = round($rate * $cost, 0);
88 16
            $fRest -= $fNRate;
89
90 16
            if ($fRest < 0.0) {
91 1
                return match ($period - $n) {
92 1
                    1 => round($cost * 0.5, 0),
93 1
                    default => 0.0,
94 1
                };
95
            }
96 15
            $cost -= $fNRate;
97
        }
98
99 15
        return $fNRate;
100
    }
101
102
    /**
103
     * AMORLINC.
104
     *
105
     * Returns the depreciation for each accounting period.
106
     * This function is provided for the French accounting system. If an asset is purchased in
107
     * the middle of the accounting period, the prorated depreciation is taken into account.
108
     *
109
     * Excel Function:
110
     *        AMORLINC(cost,purchased,firstPeriod,salvage,period,rate[,basis])
111
     *
112
     * @param mixed $cost The cost of the asset as a float
113
     * @param mixed $purchased Date of the purchase of the asset
114
     * @param mixed $firstPeriod Date of the end of the first period
115
     * @param mixed $salvage The salvage value at the end of the life of the asset
116
     * @param mixed $period The period as a float
117
     * @param mixed $rate Rate of depreciation as  float
118
     * @param mixed $basis Integer indicating the type of day count to use.
119
     *                             0 or omitted    US (NASD) 30/360
120
     *                             1               Actual/actual
121
     *                             2               Actual/360
122
     *                             3               Actual/365
123
     *                             4               European 30/360
124
     *
125
     * @return float|string (string containing the error type if there is an error)
126
     */
127 17
    public static function AMORLINC(
128
        mixed $cost,
129
        mixed $purchased,
130
        mixed $firstPeriod,
131
        mixed $salvage,
132
        mixed $period,
133
        mixed $rate,
134
        mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
135
    ): string|float {
136 17
        $cost = Functions::flattenSingleValue($cost);
137 17
        $purchased = Functions::flattenSingleValue($purchased);
138 17
        $firstPeriod = Functions::flattenSingleValue($firstPeriod);
139 17
        $salvage = Functions::flattenSingleValue($salvage);
140 17
        $period = Functions::flattenSingleValue($period);
141 17
        $rate = Functions::flattenSingleValue($rate);
142 17
        $basis = Functions::flattenSingleValue($basis) ?? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD;
143
144
        try {
145 17
            $cost = FinancialValidations::validateFloat($cost);
146 16
            $purchased = FinancialValidations::validateDate($purchased);
147 15
            $firstPeriod = FinancialValidations::validateDate($firstPeriod);
148 14
            $salvage = FinancialValidations::validateFloat($salvage);
149 13
            $period = FinancialValidations::validateFloat($period);
150 12
            $rate = FinancialValidations::validateFloat($rate);
151 11
            $basis = FinancialValidations::validateBasis($basis);
152 8
        } catch (Exception $e) {
153 8
            return $e->getMessage();
154
        }
155
156 9
        $fOneRate = $cost * $rate;
157 9
        $fCostDelta = $cost - $salvage;
158
        //    Note, quirky variation for leap years on the YEARFRAC for this function
159 9
        $purchasedYear = DateTimeExcel\DateParts::year($purchased);
160 9
        $yearFracx = DateTimeExcel\YearFrac::fraction($purchased, $firstPeriod, $basis);
161 9
        if (is_string($yearFracx)) {
162
            return $yearFracx;
163
        }
164
        /** @var float $yearFrac */
165 9
        $yearFrac = $yearFracx;
166
167
        if (
168 9
            $basis == FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL
169 9
            && $yearFrac < 1
170
        ) {
171 2
            $temp = Functions::scalar($purchasedYear);
172 2
            if (is_int($temp) || is_string($temp)) {
173 2
                if (DateTimeExcel\Helpers::isLeapYear($temp)) {
174 2
                    $yearFrac *= 365 / 366;
175
                }
176
            }
177
        }
178
179 9
        $f0Rate = $yearFrac * $rate * $cost;
180 9
        $nNumOfFullPeriods = (int) (($cost - $salvage - $f0Rate) / $fOneRate);
181
182 9
        if ($period == 0) {
183 1
            return $f0Rate;
184 8
        } elseif ($period <= $nNumOfFullPeriods) {
185 6
            return $fOneRate;
186 2
        } elseif ($period == ($nNumOfFullPeriods + 1)) {
187 1
            return $fCostDelta - $fOneRate * $nNumOfFullPeriods - $f0Rate;
188
        }
189
190 1
        return 0.0;
191
    }
192
193 16
    private static function getAmortizationCoefficient(float $rate): float
194
    {
195
        //    The depreciation coefficients are:
196
        //    Life of assets (1/rate)        Depreciation coefficient
197
        //    Less than 3 years            1
198
        //    Between 3 and 4 years        1.5
199
        //    Between 5 and 6 years        2
200
        //    More than 6 years            2.5
201 16
        $fUsePer = 1.0 / $rate;
202
203 16
        if ($fUsePer < 3.0) {
204 1
            return 1.0;
205 15
        } elseif ($fUsePer < 4.0) {
206 3
            return 1.5;
207 12
        } elseif ($fUsePer <= 6.0) {
208 8
            return 2.0;
209
        }
210
211 4
        return 2.5;
212
    }
213
}
214