Completed
Push — master ( 3e1dfc...0211a0 )
by Nikola
12s
created

Version::isMinorRelease()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Version;
6
7
use JsonSerializable;
8
use Version\Extension\Build;
9
use Version\Extension\NoBuild;
10
use Version\Extension\NoPreRelease;
11
use Version\Exception\InvalidVersionException;
12
use Version\Exception\InvalidVersionStringException;
13
use Version\Comparator\ComparatorInterface;
14
use Version\Comparator\SemverComparator;
15
use Version\Constraint\ConstraintInterface;
16
use Version\Extension\PreRelease;
17
18
/**
19
 * @author Nikola Posa <[email protected]>
20
 */
21
class Version implements JsonSerializable
22
{
23
    /**
24
     * @var int
25
     */
26
    protected $major;
27
28
    /**
29
     * @var int
30
     */
31
    protected $minor;
32
33
    /**
34
     * @var int
35
     */
36
    protected $patch;
37
38
    /**
39
     * @var PreRelease
40
     */
41
    protected $preRelease;
42
43
    /**
44
     * @var Build
45
     */
46
    protected $build;
47
48 84
    protected function __construct(int $major, int $minor, int $patch, PreRelease $preRelease, Build $build)
49
    {
50 84
        $this->validateNumber('major', $major);
51 83
        $this->validateNumber('minor', $minor);
52 82
        $this->validateNumber('patch', $patch);
53
54 81
        $this->major = $major;
55 81
        $this->minor = $minor;
56 81
        $this->patch = $patch;
57 81
        $this->preRelease = $preRelease;
58 81
        $this->build = $build;
59 81
    }
60
61 84
    protected function validateNumber(string $name, int $value) : void
62
    {
63 84
        if ($value < 0) {
64 3
            throw InvalidVersionException::forNumber($name, $value);
65
        }
66 83
    }
67
68 84
    public static function fromParts(int $major, int $minor = 0, int $patch = 0, PreRelease $preRelease = null, Build $build = null) : Version
69
    {
70 84
        return new static($major, $minor, $patch, $preRelease ?? new NoPreRelease(), $build ?? new NoBuild());
71
    }
72
73
    /**
74
     * @param string $versionString
75
     * @return Version
76
     * @throws InvalidVersionStringException
77
     */
78 82
    public static function fromString(string $versionString) : Version
79
    {
80 82
        if (!preg_match(
81
            '#^'
82
            . '(v|release\-)?'
83
            . '(?P<core>(?:[0-9]|[1-9][0-9]+)(?:\.(?:[0-9]|[1-9][0-9]+)){2})'
84
            . '(?:\-(?P<preRelease>[0-9A-Za-z\-\.]+))?'
85
            . '(?:\+(?P<build>[0-9A-Za-z\-\.]+))?'
86 82
            . '$#',
87 82
            $versionString,
88 82
            $parts
89
        )) {
90 5
            throw InvalidVersionStringException::forVersionString($versionString);
91
        }
92
93 78
        [$major, $minor, $patch] = explode('.', $parts['core']);
0 ignored issues
show
Bug introduced by
The variable $major does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $minor does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $patch does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
94 78
        $preRelease = !empty($parts['preRelease']) ? PreRelease::fromIdentifiersString($parts['preRelease']) : new NoPreRelease();
95 78
        $build = !empty($parts['build']) ? Build::fromIdentifiersString($parts['build']) : new NoBuild();
96
97 78
        return static::fromParts((int) $major, (int) $minor, (int) $patch, $preRelease, $build);
98
    }
99
100 61
    public function getMajor() : int
101
    {
102 61
        return $this->major;
103
    }
104
105 56
    public function getMinor() : int
106
    {
107 56
        return $this->minor;
108
    }
109
110 53
    public function getPatch() : int
111
    {
112 53
        return $this->patch;
113
    }
114
115 47
    public function getPreRelease() : PreRelease
116
    {
117 47
        return $this->preRelease;
118
    }
119
120 28
    public function getBuild() : Build
121
    {
122 28
        return $this->build;
123
    }
124
125
    /**
126
     * @param Version|string $version
127
     * @return bool
128
     */
129 5
    public function isEqualTo($version) : bool
130
    {
131 5
        return $this->compareTo($version) === 0;
132
    }
133
134
    /**
135
     * @param Version|string $version
136
     * @return bool
137
     */
138 1
    public function isNotEqualTo($version) : bool
139
    {
140 1
        return !$this->isEqualTo($version);
141
    }
142
143
    /**
144
     * @param Version|string $version
145
     * @return bool
146
     */
147 3
    public function isGreaterThan($version) : bool
148
    {
149 3
        return $this->compareTo($version) > 0;
150
    }
151
152
    /**
153
     * @param Version|string $version
154
     * @return bool
155
     */
156 6
    public function isGreaterOrEqualTo($version) : bool
157
    {
158 6
        return $this->compareTo($version) >= 0;
159
    }
160
161
    /**
162
     * @param Version|string $version
163
     * @return bool
164
     */
165 3
    public function isLessThan($version) : bool
166
    {
167 3
        return $this->compareTo($version) < 0;
168
    }
169
170
    /**
171
     * @param Version|string $version
172
     * @return bool
173
     */
174 2
    public function isLessOrEqualTo($version) : bool
175
    {
176 2
        return $this->compareTo($version) <= 0;
177
    }
178
179
    /**
180
     * @param Version|string $version
181
     * @return int (1 if $this > $version, -1 if $this < $version, 0 if equal)
182
     */
183 24
    public function compareTo($version) : int
184
    {
185 24
        if (is_string($version)) {
186 7
            $version = static::fromString($version);
187
        }
188
189 24
        return $this->getComparator()->compare($this, $version);
190
    }
191
192 2
    public function isMajorRelease() : bool
193
    {
194 2
        return $this->major > 0 && $this->minor === 0 && $this->patch === 0;
195
    }
196
197 1
    public function isMinorRelease() : bool
198
    {
199 1
        return $this->minor > 0 && $this->patch === 0;
200
    }
201
202 1
    public function isPatchRelease() : bool
203
    {
204 1
        return $this->patch > 0;
205
    }
206
207 39
    public function isPreRelease() : bool
208
    {
209 39
        return !$this->preRelease->isEmpty();
210
    }
211
212
    /**
213
     * @deprecated Use hasBuild() instead
214
     * @return bool
215
     */
216 1
    public function isBuild() : bool
217
    {
218 1
        return !$this->build->isEmpty();
219
    }
220
221 16
    public function hasBuild() : bool
222
    {
223 16
        return !$this->build->isEmpty();
224
    }
225
226 1
    public function incrementMajor() : Version
227
    {
228 1
        return static::fromParts($this->major + 1, 0, 0, new NoPreRelease(), new NoBuild());
229
    }
230
231 2
    public function incrementMinor() : Version
232
    {
233 2
        return static::fromParts($this->major, $this->minor + 1, 0, new NoPreRelease(), new NoBuild());
234
    }
235
236 1
    public function incrementPatch() : Version
237
    {
238 1
        return static::fromParts($this->major, $this->minor, $this->patch + 1, new NoPreRelease(), new NoBuild());
239
    }
240
241
    /**
242
     * @param PreRelease|string $preRelease
243
     * @return Version
244
     */
245 2
    public function withPreRelease($preRelease) : Version
246
    {
247 2
        if (is_string($preRelease)) {
248 2
            $preRelease = PreRelease::fromIdentifiersString($preRelease);
249
        }
250
251 2
        return static::fromParts($this->major, $this->minor, $this->patch, $preRelease, new NoBuild());
252
    }
253
254
    /**
255
     * @param Build|string $build
256
     * @return Version
257
     */
258 1
    public function withBuild($build) : Version
259
    {
260 1
        if (is_string($build)) {
261 1
            $build = Build::fromIdentifiersString($build);
262
        }
263
264 1
        return static::fromParts($this->major, $this->minor, $this->patch, $this->preRelease, $build);
265
    }
266
267 4
    public function matches(ConstraintInterface $constraint) : bool
268
    {
269 4
        return $constraint->assert($this);
270
    }
271
272 15
    public function getVersionString() : string
273
    {
274
        return
275 15
            $this->major
276 15
            . '.' . $this->minor
277 15
            . '.' . $this->patch
278 15
            . ($this->isPreRelease() ? '-' . (string) $this->preRelease : '')
279 15
            . ($this->hasBuild() ? '+' . (string) $this->build : '')
280
        ;
281
    }
282
283 11
    public function __toString() : string
284
    {
285 11
        return $this->getVersionString();
286
    }
287
288 4
    public function jsonSerialize() : string
289
    {
290 4
        return $this->getVersionString();
291
    }
292
293 4
    public function toArray() : array
294
    {
295
        return [
296 4
            'major' => $this->major,
297 4
            'minor' => $this->minor,
298 4
            'patch' => $this->patch,
299 4
            'preRelease' => $this->preRelease->getIdentifiers(),
300 4
            'build' => $this->build->getIdentifiers(),
301
        ];
302
    }
303
304 24
    protected function getComparator() : ComparatorInterface
305
    {
306 24
        static $comparator = null;
307
308 24
        if (null === $comparator) {
309 1
            $comparator = new SemverComparator();
310
        }
311
312 24
        return $comparator;
313
    }
314
}
315