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
Push — master ( 2f23ab...a77251 )
by Maeda
02:19
created

DateRange   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 1 Features 3
Metric Value
wmc 26
c 5
b 1
f 3
lcom 1
cbo 0
dl 0
loc 118
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 22 8
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 contains() 0 20 4
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
        $num = func_num_args();
20
        if ($num === 2) {
21
            $this->start = self::convertToDateTime(func_get_arg(0));
22
            $this->end = self::convertToDateTime(func_get_arg(1));
23
        } elseif ($num === 1 && is_array(func_get_arg(0)) && count(func_get_arg(0)) === 2) {
24
            $startEndArray = func_get_arg(0);
25
            if (is_array($startEndArray) && count($startEndArray) === 2) {
26
                $values = array_values($startEndArray);
27
                $this->start = self::convertToDateTime($values[0]);
28
                $this->end = self::convertToDateTime($values[1]);
29
            }
30
        } else {
31
            throw new \InvalidArgumentException('Invalid argument number or format');
32
        }
33
34
        if ($this->start->getTimestamp() > $this->end->getTimestamp()) {
35
            throw new \InvalidArgumentException('end date is the day before than start date');
36
        }
37
38
        $this->interval = new DateInterval(self::INTERVAL);
39
    }
40
41
    public function excludeStartDate()
42
    {
43
        $this->excludeStartDate = true;
44
    }
45
46
    public function excludeEndDate()
47
    {
48
        $this->excludeEndDate = true;
49
    }
50
51
    public function setInterval(DateInterval $interval)
52
    {
53
        $this->interval = $interval;
54
    }
55
56
    public function getInterval()
57
    {
58
        return $this->interval;
59
    }
60
61
    public function getDatePeriod(DateInterval $interval = null)
62
    {
63
        if (!$this->excludeEndDate) {
64
            $end = clone $this->end;
65
            // DatePeriod does not include end date so, plus 1 sec to end date.
66
            $end->modify('+1 sec');
67
        } else {
68
            $end = $this->end;
69
        }
70
71
        $option = null;
72
        if ($this->excludeStartDate) {
73
            $option = DatePeriod::EXCLUDE_START_DATE;
74
        }
75
76
        return new DatePeriod($this->start, ($interval) ?: $this->interval, $end, $option);
77
    }
78
79
    public function getIterator()
80
    {
81
        return $this->getDatePeriod();
82
    }
83
84
    public static function convertToDateTime($param)
85
    {
86
        if ($param instanceOf DateTime) {
87
            return $param;
88
        }
89
        if (strtotime($param) === false) {
90
            throw new \InvalidArgumentException('Invalid datetime string');
91
        }
92
93
        return new DateTime($param);
94
    }
95
96
    public function getStart()
97
    {
98
        return $this->start;
99
    }
100
101
    public function getEnd()
102
    {
103
        return $this->end;
104
    }
105
106
    public function contains($dateString)
107
    {
108
        $date = self::convertToDateTime($dateString);
109
        $timestamp = $date->getTimestamp();
110
111
        if ($this->excludeStartDate) {
112
            $isAfterThanStart = $this->start->getTimestamp() < $timestamp;
113
        } else {
114
            $isAfterThanStart = $this->start->getTimestamp() <= $timestamp;
115
        }
116
117
        if ($this->excludeEndDate) {
118
            $isBeforeThanEnd = $timestamp < $this->end->getTimestamp();
119
        } else {
120
            $isBeforeThanEnd = $timestamp <= $this->end->getTimestamp();
121
        }
122
123
124
        return $isAfterThanStart && $isBeforeThanEnd;
125
    }
126
}
127