1 | <?php |
||||
2 | |||||
3 | declare(strict_types = 1); |
||||
4 | |||||
5 | /* |
||||
6 | * Copyright (c) DateTime-Contributors |
||||
7 | * |
||||
8 | * Licensed under the MIT License. See LICENSE.md file in the project root |
||||
9 | * for full license information. |
||||
10 | */ |
||||
11 | |||||
12 | namespace DateTime; |
||||
13 | |||||
14 | use DateTime\Exception\NotYetImplemented; |
||||
15 | use DateTimeImmutable; |
||||
16 | use DateTimeInterface; |
||||
17 | use DateTimeZone; |
||||
18 | |||||
19 | class Date |
||||
20 | { |
||||
21 | private $datetime; |
||||
22 | |||||
23 | /** |
||||
24 | * Date constructor. |
||||
25 | * |
||||
26 | * @param string $date A date-string in an ISO-format. |
||||
27 | * |
||||
28 | * @throws \Exception |
||||
29 | */ |
||||
30 | public function __construct(string $date) |
||||
31 | { |
||||
32 | $tempDate = new DateTimeImmutable($date); |
||||
33 | $this->datetime = new DateTimeImmutable( |
||||
34 | $tempDate->format('Y-m-d') . 'T12:00:00.0', |
||||
35 | new DateTimeZone('UTC') |
||||
36 | ); |
||||
37 | $this->datetime = $this->datetime->setTime( |
||||
38 | 12, |
||||
39 | 00, |
||||
40 | 00, |
||||
41 | 0 |
||||
42 | ); |
||||
43 | } |
||||
44 | |||||
45 | public function format(string $format) : string |
||||
46 | { |
||||
47 | $format = $this->sanitizeFormatString($format); |
||||
48 | |||||
49 | return $this->datetime->format($format); |
||||
50 | } |
||||
51 | |||||
52 | public function add(DateInterval $interval) : Date |
||||
53 | { |
||||
54 | $self = clone($this); |
||||
55 | $self->datetime = $this->datetime->add($interval->getDateTimeInterval()); |
||||
56 | |||||
57 | return $self; |
||||
58 | } |
||||
59 | |||||
60 | public function sub(DateInterval $interval) : Date |
||||
61 | { |
||||
62 | $self = clone($this); |
||||
63 | $self->datetime = $this->datetime->sub($interval->getDateTimeInterval()); |
||||
64 | |||||
65 | return $self; |
||||
66 | } |
||||
67 | |||||
68 | public function modify(string $modification) : Date |
||||
0 ignored issues
–
show
|
|||||
69 | { |
||||
70 | throw new NotYetImplemented('"modify" is not yet implemented'); |
||||
71 | // TODO Remove all time-information from modification-string |
||||
72 | $self = clone($this); |
||||
0 ignored issues
–
show
$self = clone $this is not reachable.
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed. Unreachable code is most often the result of function fx() {
try {
doSomething();
return true;
}
catch (\Exception $e) {
return false;
}
return false;
}
In the above example, the last ![]() |
|||||
73 | $self->datetime = $this->datetime->modify($modification); |
||||
74 | |||||
75 | return $self; |
||||
76 | } |
||||
77 | |||||
78 | public function diff(Date $date) : DateInterval |
||||
79 | { |
||||
80 | return DateInterval::fromDateTimeInterval( |
||||
81 | $this->datetime->diff($date->datetime) |
||||
0 ignored issues
–
show
It seems like
$date->datetime can also be of type boolean ; however, parameter $datetime2 of DateTimeImmutable::diff() does only seem to accept DateTimeInterface , 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
![]() |
|||||
82 | ); |
||||
83 | } |
||||
84 | |||||
85 | public static function fromDateTimeInterface(DateTimeInterface $datetime) |
||||
86 | { |
||||
87 | return new self($datetime->format('Y-m-d')); |
||||
88 | } |
||||
89 | |||||
90 | public function getYear() : int |
||||
91 | { |
||||
92 | return (int) $this->datetime->format('Y'); |
||||
93 | } |
||||
94 | |||||
95 | public function getMonth() : int |
||||
96 | { |
||||
97 | return (int) $this->datetime->format('n'); |
||||
98 | } |
||||
99 | |||||
100 | public function getDay() : int |
||||
101 | { |
||||
102 | return (int) $this->datetime->format('j'); |
||||
103 | } |
||||
104 | |||||
105 | private function sanitizeFormatString(string $format) : string |
||||
106 | { |
||||
107 | return preg_replace('/(?<!\\\\)([aABgGhHisuveIOPTZcrU])/', '\\\\$1', $format); |
||||
108 | } |
||||
109 | } |
||||
110 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.