Issues (35)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Entities/OpeningHours.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Webfactor\Laravel\OpeningHours\Entities;
4
5
use DateTime;
6
use DateTimeZone;
7
use DateTimeInterface;
8
use Webfactor\Laravel\OpeningHours\Exceptions\Exception;
9
use Webfactor\Laravel\OpeningHours\Exceptions\InvalidDate;
10
use Webfactor\Laravel\OpeningHours\Exceptions\InvalidDayName;
11
use Webfactor\Laravel\OpeningHours\Helpers\Arr;
12
13
class OpeningHours
14
{
15
    /** @var Day[] */
16
    protected $openingHours = [];
17
18
    /** @var array */
19
    protected $exceptions = [];
20
21
    /** @var DateTimeZone|null */
22
    protected $timezone = null;
23
24 41
    public function __construct($timezone = null)
25
    {
26 41
        $this->timezone = $timezone ? new DateTimeZone($timezone) : null;
27
28 41
        $this->openingHours = Day::mapDays(function () {
29 41
            return new OpeningHoursForDay();
30 41
        });
31 41
    }
32
33
    /**
34
     * @param array $data
35
     *
36
     * @return static
37
     */
38 41
    public static function create(array $data)
39
    {
40 41
        return (new static())->fill($data);
41
    }
42
43
    /**
44
     * @param array $data
45
     *
46
     * @return bool
47
     */
48 1
    public static function isValid(array $data): bool
49
    {
50
        try {
51 1
            static::create($data);
52
53 1
            return true;
54 1
        } catch (Exception $exception) {
55 1
            return false;
56
        }
57
    }
58
59 41
    public function fill(array $data)
60
    {
61 41
        list($openingHours, $exceptions) = $this->parseOpeningHoursAndExceptions($data);
62
63 40
        foreach ($openingHours as $day => $openingHoursForThisDay) {
64 33
            $this->setOpeningHoursFromStrings($day, $openingHoursForThisDay);
65
        }
66
67 40
        $this->setExceptionsFromStrings($exceptions);
68
69 39
        return $this;
70
    }
71
72 1
    public function forWeek(): array
73
    {
74 1
        return $this->openingHours;
75
    }
76
77 19
    public function forDay(string $day): OpeningHoursForDay
78
    {
79 19
        $day = $this->normalizeDayName($day);
80
81 19
        return $this->openingHours[$day];
82
    }
83
84 11
    public function forDate(DateTimeInterface $date): OpeningHoursForDay
85
    {
86 11
        $date = $this->applyTimezone($date);
87
88 11
        return $this->exceptions[$date->format('Y-m-d')] ?? ($this->exceptions[$date->format('m-d')] ?? $this->forDay(Day::onDateTime($date)));
0 ignored issues
show
This line exceeds maximum limit of 120 characters; contains 143 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
89
    }
90
91 1
    public function exceptions(): array
92
    {
93 1
        return $this->exceptions;
94
    }
95
96 7
    public function isOpenOn(string $day): bool
97
    {
98 7
        return count($this->forDay($day)) > 0;
99
    }
100
101 6
    public function isClosedOn(string $day): bool
102
    {
103 6
        return ! $this->isOpenOn($day);
104
    }
105
106 5
    public function isOpenAt(DateTimeInterface $dateTime): bool
107
    {
108 5
        $dateTime = $this->applyTimezone($dateTime);
109
110 5
        $openingHoursForDay = $this->forDate($dateTime);
111
112 5
        return $openingHoursForDay->isOpenAt(Time::fromDateTime($dateTime));
113
    }
114
115 5
    public function isClosedAt(DateTimeInterface $dateTime): bool
116
    {
117 5
        return ! $this->isOpenAt($dateTime);
118
    }
119
120
    public function isOpen(): bool
121
    {
122
        return $this->isOpenAt(new DateTime());
123
    }
124
125 1
    public function isClosed(): bool
126
    {
127 1
        return $this->isClosedAt(new DateTime());
128
    }
129
130 4
    public function nextOpen(DateTimeInterface $dateTime): DateTime
131
    {
132 4
        $openingHoursForDay = $this->forDate($dateTime);
133 4
        $nextOpen = $openingHoursForDay->nextOpen(Time::fromDateTime($dateTime));
134
135 4
        while ($nextOpen == false) {
136
            $dateTime
137 3
                ->modify('+1 day')
138 3
                ->setTime(0, 0, 0);
139
140 3
            $openingHoursForDay = $this->forDate($dateTime);
141
142 3
            $nextOpen = $openingHoursForDay->nextOpen(Time::fromDateTime($dateTime));
143
        }
144
145 4
        $nextDateTime = $nextOpen->toDateTime();
146 4
        $dateTime->setTime($nextDateTime->format('G'), $nextDateTime->format('i'), 0);
147
148 4
        return $dateTime;
149
    }
150
151
    public function regularClosingDays(): array
152
    {
153 2
        return array_keys($this->filter(function (OpeningHoursForDay $openingHoursForDay) {
154 2
            return $openingHoursForDay->isEmpty();
155 2
        }));
156
    }
157
158 1
    public function regularClosingDaysISO(): array
159
    {
160 1
        return Arr::map($this->regularClosingDays(), [Day::class, 'toISO']);
161
    }
162
163
    public function exceptionalClosingDates(): array
164
    {
165 1
        $dates = array_keys($this->filterExceptions(function (OpeningHoursForDay $openingHoursForDay, $date) {
0 ignored issues
show
The parameter $date is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
166 1
            return $openingHoursForDay->isEmpty();
167 1
        }));
168
169 1
        return Arr::map($dates, function ($date) {
170 1
            return DateTime::createFromFormat('Y-m-d', $date);
171 1
        });
172
    }
173
174
    public function setTimezone($timezone)
175
    {
176
        $this->timezone = new DateTimeZone($timezone);
177
    }
178
179 41
    protected function parseOpeningHoursAndExceptions(array $data): array
180
    {
181 41
        $exceptions = Arr::pull($data, 'exceptions', []);
182 41
        $openingHours = [];
183
184 41
        foreach ($data as $day => $openingHoursData) {
185 34
            $openingHours[$this->normalizeDayName($day)] = $openingHoursData;
186
        }
187
188 40
        return [$openingHours, $exceptions];
189
    }
190
191 33
    protected function setOpeningHoursFromStrings(string $day, array $openingHours)
192
    {
193 33
        $day = $this->normalizeDayName($day);
194
195 33
        $this->openingHours[$day] = OpeningHoursForDay::fromStrings($openingHours);
196 33
    }
197
198
    protected function setExceptionsFromStrings(array $exceptions)
199
    {
200 40
        $this->exceptions = Arr::map($exceptions, function (array $openingHours, string $date) {
201 10
            $recurring = DateTime::createFromFormat('m-d', $date);
202
203 10
            if ($recurring === false || $recurring->format('m-d') !== $date) {
204 9
                $dateTime = DateTime::createFromFormat('Y-m-d', $date);
205
206 9
                if ($dateTime === false || $dateTime->format('Y-m-d') !== $date) {
207 1
                    throw InvalidDate::invalidDate($date);
208
                }
209
            }
210
211 9
            return OpeningHoursForDay::fromStrings($openingHours);
212 40
        });
213 39
    }
214
215 38
    protected function normalizeDayName(string $day)
216
    {
217 38
        $day = strtolower($day);
218
219 38
        if (! Day::isValid($day)) {
220 2
            throw new InvalidDayName();
221
        }
222
223 37
        return $day;
224
    }
225
226 11
    protected function applyTimezone(DateTimeInterface $date)
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
227
    {
228 11
        if ($this->timezone) {
229
            $date = $date->setTimezone($this->timezone);
230
        }
231
232 11
        return $date;
233
    }
234
235 2
    public function filter(callable $callback): array
236
    {
237 2
        return Arr::filter($this->openingHours, $callback);
238
    }
239
240
    public function map(callable $callback): array
241
    {
242
        return Arr::map($this->openingHours, $callback);
243
    }
244
245 13
    public function flatMap(callable $callback): array
246
    {
247 13
        return Arr::flatMap($this->openingHours, $callback);
248
    }
249
250 1
    public function filterExceptions(callable $callback): array
251
    {
252 1
        return Arr::filter($this->exceptions, $callback);
253
    }
254
255
    public function mapExceptions(callable $callback): array
256
    {
257
        return Arr::map($this->exceptions, $callback);
258
    }
259
260 1
    public function flatMapExceptions(callable $callback): array
261
    {
262 1
        return Arr::flatMap($this->exceptions, $callback);
263
    }
264
265
    public function asStructuredData(): array
266
    {
267 View Code Duplication
        $regularHours = $this->flatMap(function (OpeningHoursForDay $openingHoursForDay, string $day) {
0 ignored issues
show
This code seems to be duplicated across 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...
268 1
            return $openingHoursForDay->map(function (TimeRange $timeRange) use ($day) {
269
                return [
270 1
                    '@type' => 'OpeningHoursSpecification',
271 1
                    'dayOfWeek' => ucfirst($day),
272 1
                    'opens' => (string) $timeRange->start(),
273 1
                    'closes' => (string) $timeRange->end(),
274
                ];
275 1
            });
276 1
        });
277
278 1
        $exceptions = $this->flatMapExceptions(function (OpeningHoursForDay $openingHoursForDay, string $date) {
279 1
            if ($openingHoursForDay->isEmpty()) {
280
                return [[
281 1
                    '@type' => 'OpeningHoursSpecification',
282 1
                    'opens' => '00:00',
283 1
                    'closes' => '00:00',
284 1
                    'validFrom' => $date,
285 1
                    'validThrough' => $date,
286
                ]];
287
            }
288
289 1 View Code Duplication
            return $openingHoursForDay->map(function (TimeRange $timeRange) use ($date) {
0 ignored issues
show
This code seems to be duplicated across 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...
290
                return [
291 1
                    '@type' => 'OpeningHoursSpecification',
292 1
                    'opens' => $timeRange->start(),
293 1
                    'closes' => $timeRange->end(),
294 1
                    'validFrom' => $date,
295 1
                    'validThrough' => $date,
296
                ];
297 1
            });
298 1
        });
299
300 1
        return array_merge($regularHours, $exceptions);
301
    }
302
}
303