DateTime   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A comesBefore() 0 3 1
A fromString() 0 13 3
A format() 0 14 2
A add() 0 6 1
A __construct() 0 3 2
A __toString() 0 3 1
A comesAfterOrIsEqual() 0 3 1
A sub() 0 6 1
A comesAfter() 0 3 1
A comesBeforeOrIsEqual() 0 3 1
A now() 0 3 2
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2016 SURFnet bv
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
20
21
namespace Surfnet\StepupSelfService\SelfServiceBundle\Value;
22
23
use DateInterval;
24
use DateTime as CoreDateTime;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\InvalidArgumentException;
26
use Stringable;
27
28
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
29
 * @SuppressWarnings(PHPMD.TooManyPublicMethods) due to comparison methods
30
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
31
class DateTime implements Stringable
32
{
33
    /**
34
     * This string can also be used with `DateTime::createFromString()`.
35
     */
36
    final public const FORMAT = DATE_ATOM;
37
38
    /**
39
     * Allows for mocking of time.
40
     *
41
     * @var self|null
42
     */
43
    private static $now;
0 ignored issues
show
Coding Style introduced by
Private member variable "now" must be prefixed with an underscore
Loading history...
44
45
    private readonly CoreDateTime $dateTime;
0 ignored issues
show
Coding Style introduced by
Private member variable "dateTime" must be prefixed with an underscore
Loading history...
46
47
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
48
     * @return self
49
     */
50
    public static function now()
51
    {
52
        return self::$now ?: new self(new CoreDateTime);
53
    }
54
55
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
56
     * @param string $string A date-time string formatted using `self::FORMAT` (eg. '2014-11-26T15:20:43+01:00').
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
57
     * @return DateTime
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
58
     */
59
    public static function fromString($string): self
60
    {
61
        if (!is_string($string)) {
0 ignored issues
show
introduced by
The condition is_string($string) is always true.
Loading history...
62
            InvalidArgumentException::invalidType('string', 'string', $string);
63
        }
64
65
        $dateTime = CoreDateTime::createFromFormat(self::FORMAT, $string);
66
67
        if ($dateTime === false) {
68
            throw new InvalidArgumentException('Date-time string could not be parsed: is it formatted correctly?');
69
        }
70
71
        return new self($dateTime);
72
    }
73
74
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
75
     * @param CoreDateTime|null $dateTime
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
76
     */
77
    public function __construct(CoreDateTime $dateTime = null)
78
    {
79
        $this->dateTime = $dateTime ?: new CoreDateTime();
0 ignored issues
show
Bug introduced by
The property dateTime is declared read-only in Surfnet\StepupSelfServic...ceBundle\Value\DateTime.
Loading history...
80
    }
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $interval should have a doc-comment as per coding-style.
Loading history...
83
     * @return DateTime
84
     */
85
    public function add(DateInterval $interval): self
86
    {
87
        $dateTime = clone $this->dateTime;
88
        $dateTime->add($interval);
89
90
        return new self($dateTime);
91
    }
92
93
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $interval should have a doc-comment as per coding-style.
Loading history...
94
     * @return DateTime
95
     */
96
    public function sub(DateInterval $interval): self
97
    {
98
        $dateTime = clone $this->dateTime;
99
        $dateTime->sub($interval);
100
101
        return new self($dateTime);
102
    }
103
104
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $dateTime should have a doc-comment as per coding-style.
Loading history...
105
     * @return boolean
106
     */
107
    public function comesBefore(DateTime $dateTime): bool
108
    {
109
        return $this->dateTime < $dateTime->dateTime;
110
    }
111
112
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $dateTime should have a doc-comment as per coding-style.
Loading history...
113
     * @return boolean
114
     */
115
    public function comesBeforeOrIsEqual(DateTime $dateTime): bool
116
    {
117
        return $this->dateTime <= $dateTime->dateTime;
118
    }
119
120
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $dateTime should have a doc-comment as per coding-style.
Loading history...
121
     * @return boolean
122
     */
123
    public function comesAfter(DateTime $dateTime): bool
124
    {
125
        return $this->dateTime > $dateTime->dateTime;
126
    }
127
128
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $dateTime should have a doc-comment as per coding-style.
Loading history...
129
     * @return boolean
130
     */
131
    public function comesAfterOrIsEqual(DateTime $dateTime): bool
132
    {
133
        return $this->dateTime >= $dateTime->dateTime;
134
    }
135
136
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
137
     * @param $format
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
138
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
139
     */
140
    public function format($format)
141
    {
142
        $formatted = $this->dateTime->format($format);
143
144
        if ($formatted === false) {
145
            throw new InvalidArgumentException(
146
                sprintf(
147
                    'Given format "%s" is not a valid format for DateTime',
148
                    $format
149
                )
150
            );
151
        }
152
153
        return $formatted;
154
    }
155
156
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
157
     * @return string An ISO 8601 representation of this DateTime.
158
     */
159
    public function __toString(): string
160
    {
161
        return $this->format(self::FORMAT);
162
    }
163
}
164