GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch master (47506f)
by Maeda
02:12 queued 11s
created

DateRange   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 10
Bugs 3 Features 6
Metric Value
wmc 31
c 10
b 3
f 6
lcom 1
cbo 0
dl 0
loc 148
rs 9.8

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 4
A parseArguments() 0 11 3
A getDateFromArray() 0 12 3
A excludeStartDate() 0 4 1
A excludeEndDate() 0 4 1
A setInterval() 0 4 1
A getInterval() 0 4 1
A getDatePeriod() 0 17 4
A getIterator() 0 4 1
A convertToDateTime() 0 11 3
A getStart() 0 4 1
A getEnd() 0 4 1
A diff() 0 4 1
A contains() 0 19 4
A toString() 0 4 1
A __toString() 0 4 1
1
<?php
2
namespace Brtriver\DateRange;
3
4
use DateTime;
5
use DateInterval;
6
use DatePeriod;
7
use IteratorAggregate;
8
9
class DateRange implements IteratorAggregate
10
{
11
    private $start;
12
    private $end;
13
    private $interval;
14
    private $excludeStartDate = false;
15
    private $excludeEndDate = false;
16
    const INTERVAL = 'P1D';
17
18
    public function __construct() {
19
        $this->parseArguments(func_get_args());
20
21
        if (!($this->start instanceof DateTime) || !($this->end instanceof DateTime)) {
22
            throw new \InvalidArgumentException('cannot parse start and end date');
23
        }
24
25
        if ($this->start->getTimestamp() > $this->end->getTimestamp()) {
26
            throw new \InvalidArgumentException('end date is the day before than start date');
27
        }
28
29
        $this->interval = new DateInterval(self::INTERVAL);
30
    }
31
32
    private function parseArguments($args)
33
    {
34
        if (count($args) === 1) {
35
            list($this->start, $this->end) = self::getDateFromArray($args[0]);
36
        } elseif (count($args) === 2) {
37
            $this->start = self::convertToDateTime($args[0]);
38
            $this->end = self::convertToDateTime($args[1]);
39
        } else {
40
            throw new \InvalidArgumentException('Invalid number of arguments');
41
        }
42
    }
43
44
    private static function getDateFromArray($startEndArray)
45
    {
46
        $start = $end = null;
47
48
        if (is_array($startEndArray) && count($startEndArray) === 2) {
49
            $values = array_values($startEndArray);
50
            $start = self::convertToDateTime($values[0]);
51
            $end = self::convertToDateTime($values[1]);
52
        }
53
54
        return [$start, $end];
55
    }
56
57
    public function excludeStartDate()
58
    {
59
        $this->excludeStartDate = true;
60
    }
61
62
    public function excludeEndDate()
63
    {
64
        $this->excludeEndDate = true;
65
    }
66
67
    public function setInterval(DateInterval $interval)
68
    {
69
        $this->interval = $interval;
70
    }
71
72
    public function getInterval()
73
    {
74
        return $this->interval;
75
    }
76
77
    public function getDatePeriod(DateInterval $interval = null)
78
    {
79
        if (!$this->excludeEndDate) {
80
            $end = clone $this->end;
81
            // DatePeriod does not include end date so, plus 1 sec to end date.
82
            $end->modify('+1 sec');
83
        } else {
84
            $end = $this->end;
85
        }
86
87
        $option = null;
88
        if ($this->excludeStartDate) {
89
            $option = DatePeriod::EXCLUDE_START_DATE;
90
        }
91
92
        return new DatePeriod($this->start, ($interval) ?: $this->interval, $end, $option);
93
    }
94
95
    public function getIterator()
96
    {
97
        return $this->getDatePeriod();
98
    }
99
100
    public static function convertToDateTime($param)
101
    {
102
        if ($param instanceOf DateTime) {
103
            return $param;
104
        }
105
        if (strtotime($param) === false) {
106
            throw new \InvalidArgumentException('Invalid datetime string');
107
        }
108
109
        return new DateTime($param);
110
    }
111
112
    public function getStart()
113
    {
114
        return $this->start;
115
    }
116
117
    public function getEnd()
118
    {
119
        return $this->end;
120
    }
121
122
    public function diff()
123
    {
124
        return $this->start->diff($this->end);
125
    }
126
127
    public function contains($dateString)
128
    {
129
        $date = self::convertToDateTime($dateString);
130
131
        if ($this->excludeStartDate) {
132
            $isAfterThanStart = $this->start < $date;
133
        } else {
134
            $isAfterThanStart = $this->start <= $date;
135
        }
136
137
        if ($this->excludeEndDate) {
138
            $isBeforeThanEnd = $date < $this->end;
139
        } else {
140
            $isBeforeThanEnd = $date <= $this->end;
141
        }
142
143
144
        return $isAfterThanStart && $isBeforeThanEnd;
145
    }
146
147
    public function toString($format = 'Y-m-d', $separator = '~')
148
    {
149
        return sprintf('%s %s %s', $this->start->format($format), $separator, $this->end->format($format));
150
    }
151
152
    public function __toString()
153
    {
154
        return $this->toString();
155
    }
156
}
157