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
Pull Request — master (#68)
by
unknown
01:21
created

OpeningHoursForDay::findNextCloseInWorkingHours()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Spatie\OpeningHours;
4
5
use Countable;
6
use ArrayAccess;
7
use ArrayIterator;
8
use IteratorAggregate;
9
use Spatie\OpeningHours\Helpers\Arr;
10
use Spatie\OpeningHours\Exceptions\OverlappingTimeRanges;
11
12
class OpeningHoursForDay implements ArrayAccess, Countable, IteratorAggregate
13
{
14
    /** @var \Spatie\OpeningHours\TimeRange[] */
15
    protected $openingHours = [];
16
17
    public static function fromStrings(array $strings)
18
    {
19
        $openingHoursForDay = new static();
20
21
        $timeRanges = Arr::map($strings, function ($string) {
22
            return TimeRange::fromString($string);
23
        });
24
25
        $openingHoursForDay->guardAgainstTimeRangeOverlaps($timeRanges);
26
27
        $openingHoursForDay->openingHours = $timeRanges;
28
29
        return $openingHoursForDay;
30
    }
31
32
    public function isOpenAt(Time $time)
33
    {
34
        foreach ($this->openingHours as $timeRange) {
35
            if ($timeRange->containsTime($time)) {
36
                return true;
37
            }
38
        }
39
40
        return false;
41
    }
42
43
    public function nextOpen(Time $time)
44
    {
45
        foreach ($this->openingHours as $timeRange) {
46
            if ($nextOpen = $this->findNextOpenInWorkingHours($time, $timeRange)) {
47
                return $nextOpen;
48
            }
49
50
            if ($nextOpen = $this->findNextOpenInFreeTime($time, $timeRange)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $nextOpen is correct as $this->findNextOpenInFreeTime($time, $timeRange) (which targets Spatie\OpeningHours\Open...indNextOpenInFreeTime()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
51
                return $nextOpen;
52
            }
53
        }
54
55
        return false;
56
    }
57
58
    public function nextClose(Time $time)
59
    {
60
        foreach ($this->openingHours as $timeRange) {
61
            if ($nextClose = $this->findNextCloseInWorkingHours($time, $timeRange)) {
62
                return $nextClose;
63
            }
64
65
            if ($nextClose = $this->findNextCloseInFreeTime($time, $timeRange)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $nextClose is correct as $this->findNextCloseInFreeTime($time, $timeRange) (which targets Spatie\OpeningHours\Open...ndNextCloseInFreeTime()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
66
                return $nextClose;
67
            }
68
        }
69
70
        return false;
71
    }
72
73
    protected function findNextOpenInWorkingHours(Time $time, TimeRange $timeRange)
74
    {
75
        if ($timeRange->containsTime($time) && next($timeRange) !== $timeRange) {
76
            return next($timeRange);
77
        }
78
    }
79
80 View Code Duplication
    protected function findNextOpenInFreeTime(Time $time, TimeRange $timeRange, TimeRange &$prevTimeRange = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $timeOffRange = $prevTimeRange ?
83
            TimeRange::fromString($prevTimeRange->end().'-'.$timeRange->start()) :
84
            TimeRange::fromString('00:00-'.$timeRange->start());
85
86
        if ($timeOffRange->containsTime($time) || $timeOffRange->start()->isSame($time)) {
87
            return $timeRange->start();
88
        }
89
90
        $prevTimeRange = $timeRange;
91
    }
92
93
    protected function findNextCloseInWorkingHours(Time $time, TimeRange $timeRange)
94
    {
95
        if ($timeRange->containsTime($time)) {
96
            return next($timeRange);
97
        }
98
    }
99
100 View Code Duplication
    protected function findNextCloseInFreeTime(Time $time, TimeRange $timeRange, TimeRange &$prevTimeRange = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $timeOffRange = $prevTimeRange ?
103
            TimeRange::fromString($prevTimeRange->end().'-'.$timeRange->start()) :
104
            TimeRange::fromString('00:00-'.$timeRange->start());
105
106
        if ($timeOffRange->containsTime($time) || $timeOffRange->start()->isSame($time)) {
107
            return $timeRange->end();
108
        }
109
110
        $prevTimeRange = $timeRange;
111
    }
112
113
    public function offsetExists($offset): bool
114
    {
115
        return isset($this->openingHours[$offset]);
116
    }
117
118
    public function offsetGet($offset)
119
    {
120
        return $this->openingHours[$offset];
121
    }
122
123
    public function offsetSet($offset, $value)
124
    {
125
        throw new \Exception();
126
    }
127
128
    public function offsetUnset($offset)
129
    {
130
        unset($this->openingHours[$offset]);
131
    }
132
133
    public function count(): int
134
    {
135
        return count($this->openingHours);
136
    }
137
138
    public function getIterator()
139
    {
140
        return new ArrayIterator($this->openingHours);
141
    }
142
143
    public function isEmpty(): bool
144
    {
145
        return empty($this->openingHours);
146
    }
147
148
    public function map(callable $callback): array
149
    {
150
        return Arr::map($this->openingHours, $callback);
151
    }
152
153
    protected function guardAgainstTimeRangeOverlaps(array $openingHours)
154
    {
155
        foreach (Arr::createUniquePairs($openingHours) as $timeRanges) {
156
            if ($timeRanges[0]->overlaps($timeRanges[1])) {
157
                throw OverlappingTimeRanges::forRanges($timeRanges[0], $timeRanges[1]);
158
            }
159
        }
160
    }
161
162
    public function __toString()
163
    {
164
        $values = [];
165
        foreach ($this->openingHours as $openingHour) {
166
            $values[] = (string) $openingHour;
167
        }
168
169
        return implode(',', $values);
170
    }
171
}
172