ScheduleDate   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 25.49 %

Importance

Changes 0
Metric Value
wmc 11
dl 26
loc 102
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isWeekend() 0 3 1
A __construct() 0 3 1
A getWeekDay() 0 3 1
A parseDayNumber() 0 7 3
A createNow() 0 3 1
A createWeekend() 12 12 2
A createWorkday() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Itmedia\ZippyBusBundle\Schedule;
6
7
/**
8
 * Актальная дата для расписания
9
 */
10
class ScheduleDate
11
{
12
13
    /**
14
     * @var \DateTimeImmutable
15
     */
16
    private $datetime;
17
18
19
    /**
20
     * Date constructor.
21
     * @param \DateTimeImmutable $datetime
22
     */
23
    public function __construct(\DateTimeImmutable $datetime)
24
    {
25
        $this->datetime = $datetime;
26
    }
27
28
29
    /**
30
     * Текущая дата
31
     * @return ScheduleDate
32
     */
33
    public static function createNow(): ScheduleDate
34
    {
35
        return new self(new \DateTimeImmutable('now'));
36
    }
37
38
39
    /**
40
     * Выходной день
41
     * @return ScheduleDate
42
     * @throws \Exception
43
     */
44 View Code Duplication
    public static function createWeekend(): ScheduleDate
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...
45
    {
46
        $datetime = new \DateTimeImmutable('now');
47
        $day = self::parseDayNumber($datetime);
48
49
        if ($day !== 7) {
50
            $datetime = $datetime->add(
51
                new \DateInterval(
52
                    sprintf('P%sD', 7 - $day)
53
                ));
54
        }
55
        return new self($datetime);
56
    }
57
58
59
    /**
60
     * Рабочий день
61
     *
62
     * @return ScheduleDate
63
     * @throws \Exception
64
     */
65 View Code Duplication
    public static function createWorkday(): ScheduleDate
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...
66
    {
67
        $datetime = new \DateTimeImmutable('now');
68
        $day = self::parseDayNumber($datetime);
69
70
        if ($day > 5) {
71
            $datetime = $datetime->sub(
72
                new \DateInterval(
73
                    sprintf('P2D')
74
                ));
75
        }
76
        return new self($datetime);
0 ignored issues
show
Bug introduced by
It seems like $datetime can also be of type false; however, parameter $datetime of Itmedia\ZippyBusBundle\S...duleDate::__construct() does only seem to accept DateTimeImmutable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        return new self(/** @scrutinizer ignore-type */ $datetime);
Loading history...
77
    }
78
79
80
    /**
81
     * День недели
82
     * @return int
83
     */
84
    public function getWeekDay(): int
85
    {
86
        return self::parseDayNumber($this->datetime);
87
    }
88
89
90
    /**
91
     * Выходной день?
92
     * @return boolean
93
     */
94
    public function isWeekend(): bool
95
    {
96
        return in_array($this->getWeekDay(), [6, 7], true);
97
    }
98
99
100
    /**
101
     * Определить день недели (для расписания)
102
     * @param \DateTimeImmutable $datetime
103
     * @return int
104
     */
105
    private static function parseDayNumber(\DateTimeImmutable $datetime): int
106
    {
107
        $day = (int)$datetime->format('N');
108
        if ((int)$datetime->format('H') < 4) {
109
            $day = $day === 1 ? 7 : $day - 1;
110
        }
111
        return $day;
112
    }
113
114
}
115