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 ( f4b1b2...f12cf1 )
by Sebastian
02:21
created

Time   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 77
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fromString() 0 10 2
A fromDateTime() 0 4 1
A isSame() 0 4 1
A isAfter() 0 12 4
A isBefore() 0 8 2
A isSameOrAfter() 0 4 2
A toDateTime() 0 4 1
A format() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace Spatie\OpeningHours;
4
5
use DateTime;
6
use DateTimeInterface;
7
use Spatie\OpeningHours\Exceptions\InvalidTimeString;
8
9
class Time
10
{
11
    /** @var int */
12
    protected $hours;
13
14
    /** @var int */
15
    protected $minutes;
16
17
    protected function __construct(int $hours, int $minutes)
18
    {
19
        $this->hours = $hours;
20
        $this->minutes = $minutes;
21
    }
22
23
    public static function fromString(string $string): self
24
    {
25
        if (! preg_match('/^([0-1][0-9])|(2[0-4]):[0-5][0-9]$/', $string)) {
26
            throw InvalidTimeString::forString($string);
27
        }
28
29
        list($hours, $minutes) = explode(':', $string);
30
31
        return new self($hours, $minutes);
32
    }
33
34
    public static function fromDateTime(DateTimeInterface $dateTime): self
35
    {
36
        return self::fromString($dateTime->format('H:i'));
37
    }
38
39
    public function isSame(Time $time): bool
40
    {
41
        return (string) $this === (string) $time;
42
    }
43
44
    public function isAfter(Time $time): bool
45
    {
46
        if ($this->isSame($time)) {
47
            return false;
48
        }
49
50
        if ($this->hours > $time->hours) {
51
            return true;
52
        }
53
54
        return $this->hours === $time->hours && $this->minutes >= $time->minutes;
55
    }
56
57
    public function isBefore(Time $time): bool
58
    {
59
        if ($this->isSame($time)) {
60
            return false;
61
        }
62
63
        return ! $this->isAfter($time);
64
    }
65
66
    public function isSameOrAfter(Time $time): bool
67
    {
68
        return $this->isSame($time) || $this->isAfter($time);
69
    }
70
71
    public function toDateTime(): DateTime
72
    {
73
        return (new DateTime('1970-01-01 00:00:00'))->setTime($this->hours, $this->minutes);
74
    }
75
76
    public function format(string $format = 'H:i'): string
77
    {
78
        return $this->toDateTime()->format($format);
79
    }
80
81
    public function __toString(): string
82
    {
83
        return $this->format();
84
    }
85
}
86