Completed
Push — master ( 156cbf...ec5f3d )
by Frederick
14s
created

DateChecker::validateDateRange()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
rs 9.2
cc 4
eloc 9
nc 1
nop 2
1
<?php
2
3
namespace Vanbrabantf\NpmStatFetcher\Dates;
4
5
use Cake\Chronos\Chronos;
6
use DateTimeInterface;
7
8
class DateChecker
9
{
10
    /**
11
     * @param DateTimeInterface $start
12
     * @param DateTimeInterface $end
13
     * @return bool
14
     * @throws DateException
15
     */
16
    public static function validateDateRange(DateTimeInterface $start, DateTimeInterface $end)
17
    {
18
        $start = new Chronos($start);
0 ignored issues
show
Bug introduced by
$start of type DateTimeInterface is incompatible with the type null|string expected by parameter $time of Cake\Chronos\Chronos::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

18
        $start = new Chronos(/** @scrutinizer ignore-type */ $start);
Loading history...
19
        $end = new Chronos($end);
20
21
        if ($start->isFuture()) {
22
            throw new DateException("Start date \"{$start}\" must be in the past");
23
        }
24
25
        if ($end->isFuture()) {
26
            throw new DateException("End date \"{$end}\" must be in the past");
27
        }
28
29
        if ($start > $end) {
30
            throw new DateException("Start date \"{$start}\" must be smaller than end date \"{$end}\"");
31
        }
32
33
        return true;
34
    }
35
}
36