Passed
Push — master ( b3452a...0c5808 )
by
unknown
18:22 queued 08:16
created

Days   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 18
dl 0
loc 50
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A between() 0 27 6
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
4
5
use DateTimeInterface;
6
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
7
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
8
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
9
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
10
11
class Days
12
{
13
    use ArrayEnabled;
14
15
    /**
16
     * DAYS.
17
     *
18
     * Returns the number of days between two dates
19
     *
20
     * Excel Function:
21
     *        DAYS(endDate, startDate)
22
     *
23
     * @param array|DateTimeInterface|float|int|string $endDate Excel date serial value (float),
24
     *           PHP date timestamp (integer), PHP DateTime object, or a standard date string
25
     *                         Or can be an array of date values
26
     * @param array|DateTimeInterface|float|int|string $startDate Excel date serial value (float),
27
     *           PHP date timestamp (integer), PHP DateTime object, or a standard date string
28
     *                         Or can be an array of date values
29
     *
30
     * @return array|int|string Number of days between start date and end date or an error
31
     *         If an array of values is passed for the $startDate or $endDays,arguments, then the returned result
32
     *            will also be an array with matching dimensions
33
     */
34 78
    public static function between(array|DateTimeInterface|float|int|string $endDate, array|DateTimeInterface|float|int|string $startDate): array|int|string
35
    {
36 78
        if (is_array($endDate) || is_array($startDate)) {
37 24
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $endDate, $startDate);
38
        }
39
40
        try {
41 78
            $startDate = Helpers::getDateValue($startDate);
42 75
            $endDate = Helpers::getDateValue($endDate);
43 6
        } catch (Exception $e) {
44 6
            return $e->getMessage();
45
        }
46
47
        // Execute function
48 72
        $PHPStartDateObject = SharedDateHelper::excelToDateTimeObject($startDate);
49 72
        $PHPEndDateObject = SharedDateHelper::excelToDateTimeObject($endDate);
50
51 72
        $days = ExcelError::VALUE();
52 72
        $diff = $PHPStartDateObject->diff($PHPEndDateObject);
53 72
        if (!is_bool($diff->days)) {
54 72
            $days = $diff->days;
55 72
            if ($diff->invert) {
56 20
                $days = -$days;
57
            }
58
        }
59
60 72
        return $days;
61
    }
62
}
63