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
|
|||||
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
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. ![]() |
|||||
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
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
![]() |
|||||
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 |
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.