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.

DateRange   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

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