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

Helpers   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 11
eloc 16
dl 0
loc 50
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isLastDayOfMonth() 0 3 1
B daysPerYear() 0 21 10
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