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

Helpers::daysPerYear()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 10.1371

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 21
ccs 8
cts 9
cp 0.8889
rs 7.6666
c 0
b 0
f 0
cc 10
nc 9
nop 2
crap 10.1371

How to fix   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 DateTimeInterface;
6
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
7
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
8
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
9
10
class Helpers
11
{
12
    /**
13
     * daysPerYear.
14
     *
15
     * Returns the number of days in a specified year, as defined by the "basis" value
16
     *
17
     * @param mixed $year The year against which we're testing, expect int|string
18
     * @param int|string $basis The type of day count:
19
     *                              0 or omitted US (NASD)   360
20
     *                              1                        Actual (365 or 366 in a leap year)
21
     *                              2                        360
22
     *                              3                        365
23
     *                              4                        European 360
24
     *
25
     * @return int|string Result, or a string containing an error
26
     */
27 108
    public static function daysPerYear(mixed $year, $basis = 0): string|int
28
    {
29 108
        if (!is_int($year) && !is_string($year)) {
30
            return ExcelError::VALUE();
31
        }
32 108
        if (!is_numeric($basis)) {
33 1
            return ExcelError::NAN();
34
        }
35
36
        switch ($basis) {
37
            case FinancialConstants::BASIS_DAYS_PER_YEAR_NASD:
38
            case FinancialConstants::BASIS_DAYS_PER_YEAR_360:
39
            case FinancialConstants::BASIS_DAYS_PER_YEAR_360_EUROPEAN:
40 54
                return 360;
41
            case FinancialConstants::BASIS_DAYS_PER_YEAR_365:
42 10
                return 365;
43
            case FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL:
44 42
                return (DateTimeExcel\Helpers::isLeapYear($year)) ? 366 : 365;
45
        }
46
47 1
        return ExcelError::NAN();
48
    }
49
50
    /**
51
     * isLastDayOfMonth.
52
     *
53
     * Returns a boolean TRUE/FALSE indicating if this date is the last date of the month
54
     *
55
     * @param DateTimeInterface $date The date for testing
56
     */
57 128
    public static function isLastDayOfMonth(DateTimeInterface $date): bool
58
    {
59 128
        return $date->format('d') === $date->format('t');
60
    }
61
}
62