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.

DayBuilder::fromArray()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 17
cts 17
cp 1
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 15
nc 8
nop 2
crap 5
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Speicher210\BusinessHours\Day;
6
7
use Speicher210\BusinessHours\Day\Time\Time;
8
use Speicher210\BusinessHours\Day\Time\TimeBuilder;
9
use Speicher210\BusinessHours\Day\Time\TimeInterval;
10
use Speicher210\BusinessHours\Day\Time\TimeIntervalInterface;
11
12
/**
13
 * Build a DayInterface concrete implementation.
14
 */
15
final class DayBuilder
16
{
17
    /**
18
     * Create a new Day.
19
     *
20
     * @param integer $dayOfWeek The day of week.
21
     * @param array $openingIntervals The opening intervals.
22 72
     * @return Day
23
     */
24 72
    public static function fromArray($dayOfWeek, array $openingIntervals): Day
25 72
    {
26 72
        $intervals = [];
27 6
        foreach ($openingIntervals as $interval) {
28 72
            if ($interval instanceof TimeIntervalInterface) {
29 66
                $intervals[] = $interval;
30 66
            } elseif (\is_array($intervals)) {
31 66
                $intervals[] = new TimeInterval(
32 66
                    TimeBuilder::fromString($interval[0]),
33 66
                    TimeBuilder::fromString($interval[1])
34 72
                );
35
            }
36 72
        }
37 72
38
        $day = new Day($dayOfWeek, $intervals);
39 72
        $dayIntervals = $day->getOpeningHoursIntervals();
40 72
        /** @var TimeIntervalInterface $dayInterval */
41 6
        $dayInterval = \reset($dayIntervals);
42
        if (self::isIntervalAllDay($dayInterval->getStart(), $dayInterval->getEnd())) {
43
            return new AllDay($dayOfWeek);
44 72
        }
45
46
        return $day;
47
    }
48
49
    /**
50
     * Create a DayInterface object from an array.
51
     *
52
     * @param array $data The day data.
53 18
     * @return DayInterface
54
     */
55 18
    public static function fromAssociativeArray(array $data): DayInterface
56 9
    {
57
        if (!isset($data['openingIntervals'], $data['dayOfWeek']) || !\is_array($data['openingIntervals'])) {
58
            throw new \InvalidArgumentException('Array is not valid.');
59 9
        }
60 9
61 9
        $openingIntervals = [];
62 9
        foreach ($data['openingIntervals'] as $openingInterval) {
63 9
            if (!isset($openingInterval['start'], $openingInterval['end'])) {
64 6
                throw new \InvalidArgumentException('Array is not valid.');
65
            }
66
            $start = TimeBuilder::fromArray($openingInterval['start']);
67 6
            $end = TimeBuilder::fromArray($openingInterval['end']);
68 6
            if (self::isIntervalAllDay($start, $end)) {
69
                return new AllDay($data['dayOfWeek']);
70 6
            }
71
72
            $openingIntervals[] = new TimeInterval($start, $end);
73
        }
74
75
        return new Day($data['dayOfWeek'], $openingIntervals);
76
    }
77
78
    /**
79
     * Check if an interval array is all day.
80 81
     *
81
     * @param Time $start The start time.
82 81
     * @param Time $end The end time.
83 78
     * @return boolean
84
     */
85
    private static function isIntervalAllDay(Time $start, Time $end): bool
86 12
    {
87 6 View Code Duplication
        if ($start->getHours() !== 0 || $start->getMinutes() !== 0 || $start->getSeconds() !== 0) {
0 ignored issues
show
Duplication introduced by
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...
88
            return false;
89
        }
90 12
91 View Code Duplication
        if ($end->getHours() !== 24 || $end->getMinutes() !== 0 || $end->getSeconds() !== 0) {
0 ignored issues
show
Duplication introduced by
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...
92
            return false;
93
        }
94
95
        return true;
96
    }
97
}
98