DateRange::isCurrent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace DateRanger;
4
5
use Countable;
6
use DateInterval;
7
use DatePeriod;
8
use DateTimeImmutable;
9
use Iterator;
10
11
abstract class DateRange implements Iterator, Countable
12
{
13
    /** @var DateTimeImmutable */
14
    protected $start;
15
    /** @var DateTimeImmutable */
16
    protected $end;
17
    /** @var array */
18
    protected $dates = [];
19
20 12
    public function start(): DateTimeImmutable
21
    {
22 12
        return $this->start;
23
    }
24
25 2
    public function end(): DateTimeImmutable
26
    {
27 2
        return $this->end;
28
    }
29
30 9
    public function getPeriod($interval = 'P1D', DateTimeImmutable $start = null, DateTimeImmutable $end = null): DatePeriod
31
    {
32 9
        if (null === $start) {
33 9
            $start = $this->start;
34
        }
35
36 9
        if (null === $end) {
37 9
            $end = $this->end;
38
        }
39
40 9
        return new DatePeriod($start, new DateInterval($interval), $end);
41
    }
42
43 2
    public function equals(self $period): bool
44
    {
45 2
        return $this->start()->format('YmdHis') === $period->start()->format('YmdHis') && $this->end()->format('YmdHis') === $period->end()->format('YmdHis');
46
    }
47
48 1
    public function overlaps(self $period): bool
49
    {
50 1
        return $this->start() <= $period->end() && $this->end() >= $period->start();
51
    }
52
53 1
    public function isCurrent(): bool
54
    {
55 1
        $period = new static();
56
57 1
        return $this->equals($period);
58
    }
59
60 3
    public function current()
61
    {
62 3
        return current($this->dates);
63
    }
64
65 3
    public function next()
66
    {
67 3
        next($this->dates);
68 3
    }
69
70 3
    public function key()
71
    {
72 3
        return key($this->dates);
73
    }
74
75 3
    public function valid()
76
    {
77 3
        return current($this->dates);
78
    }
79
80 3
    public function rewind()
81
    {
82 3
        reset($this->dates);
83 3
    }
84
85 1
    public function count()
86
    {
87 1
        return count($this->dates);
88
    }
89
}
90