Completed
Push — master ( 6889fd...9bcd54 )
by Nikola
01:40
created

Version::fromMinor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
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\InvalidVersionPartException;
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 69
    protected function __construct(int $major, int $minor, int $patch, PreRelease $preRelease, Build $build)
49
    {
50 69
        $this->major = $major;
51 69
        $this->minor = $minor;
52 69
        $this->patch = $patch;
53 69
        $this->preRelease = $preRelease;
54 69
        $this->build = $build;
55 69
    }
56
57 72
    public static function fromParts(int $major, int $minor = 0, int $patch = 0, PreRelease $preRelease = null, Build $build = null) : Version
58
    {
59 72
        static::validatePart('major', $major);
60 71
        static::validatePart('minor', $minor);
61 70
        static::validatePart('patch', $patch);
62
63 69
        return new static($major, $minor, $patch, $preRelease ?? new NoPreRelease(), $build ?? new NoBuild());
64
    }
65
66
    /**
67
     * @param string $versionString
68
     * @return Version
69
     * @throws InvalidVersionStringException
70
     */
71 70
    public static function fromString(string $versionString) : Version
72
    {
73 70
        if (!preg_match(
74
            '#^'
75
            . 'v?'
76
            . '(?P<core>(?:[0-9]|[1-9][0-9]+)(?:\.(?:[0-9]|[1-9][0-9]+)){2})'
77
            . '(?:\-(?P<preRelease>[0-9A-Za-z\-\.]+))?'
78
            . '(?:\+(?P<build>[0-9A-Za-z\-\.]+))?'
79 70
            . '$#',
80 70
            $versionString,
81 70
            $parts
82
        )) {
83 4
            throw InvalidVersionStringException::forVersionString($versionString);
84
        }
85
86 66
        [$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...
87
88 66
        $preRelease = !empty($parts['preRelease']) ? PreRelease::fromIdentifiersString($parts['preRelease']) : new NoPreRelease();
89
90 66
        $build = !empty($parts['build']) ? Build::fromIdentifiersString($parts['build']) : new NoBuild();
91
92 66
        return static::fromParts((int) $major, (int) $minor, (int) $patch, $preRelease, $build);
93
    }
94
95 72
    protected static function validatePart(string $part, int $value) : void
96
    {
97 72
        if ($value < 0) {
98 3
            throw InvalidVersionPartException::forPart($part);
99
        }
100 71
    }
101
102 49
    public function getMajor() : int
103
    {
104 49
        return $this->major;
105
    }
106
107 45
    public function getMinor() : int
108
    {
109 45
        return $this->minor;
110
    }
111
112 42
    public function getPatch() : int
113
    {
114 42
        return $this->patch;
115
    }
116
117 28
    public function getPreRelease() : PreRelease
118
    {
119 28
        return $this->preRelease;
120
    }
121
122 6
    public function getBuild() : Build
123
    {
124 6
        return $this->build;
125
    }
126
127 45
    public function isPreRelease() : bool
128
    {
129 45
        return !$this->preRelease->isEmpty();
130
    }
131
132 25
    public function isBuild() : bool
133
    {
134 25
        return !$this->build->isEmpty();
135
    }
136
137
    /**
138
     * @param self|string $version
139
     * @return bool
140
     */
141 5
    public function isEqualTo($version) : bool
142
    {
143 5
        return $this->compareTo($version) === 0;
144
    }
145
146
    /**
147
     * @param self|string $version
148
     * @return bool
149
     */
150 1
    public function isNotEqualTo($version) : bool
151
    {
152 1
        return !$this->isEqualTo($version);
153
    }
154
155
    /**
156
     * @param self|string $version
157
     * @return bool
158
     */
159 3
    public function isGreaterThan($version) : bool
160
    {
161 3
        return $this->compareTo($version) > 0;
162
    }
163
164
    /**
165
     * @param self|string $version
166
     * @return bool
167
     */
168 4
    public function isGreaterOrEqualTo($version) : bool
169
    {
170 4
        return $this->compareTo($version) >= 0;
171
    }
172
173
    /**
174
     * @param self|string $version
175
     * @return bool
176
     */
177 3
    public function isLessThan($version) : bool
178
    {
179 3
        return $this->compareTo($version) < 0;
180
    }
181
182
    /**
183
     * @param self|string $version
184
     * @return bool
185
     */
186 2
    public function isLessOrEqualTo($version) : bool
187
    {
188 2
        return $this->compareTo($version) <= 0;
189
    }
190
191
    /**
192
     * @param self|string $version
193
     * @return int (1 if $this > $version, -1 if $this < $version, 0 if equal)
194
     */
195 20
    public function compareTo($version) : int
196
    {
197 20
        if (is_string($version)) {
198 7
            $version = static::fromString($version);
199
        }
200
201 20
        return $this->getComparator()->compare($this, $version);
202
    }
203
204 1
    public function incrementMajor() : Version
205
    {
206 1
        return static::fromParts($this->major + 1, 0, 0, new NoPreRelease(), new NoBuild());
207
    }
208
209 2
    public function incrementMinor() : Version
210
    {
211 2
        return static::fromParts($this->major, $this->minor + 1, 0, new NoPreRelease(), new NoBuild());
212
    }
213
214 1
    public function incrementPatch() : Version
215
    {
216 1
        return static::fromParts($this->major, $this->minor, $this->patch + 1, new NoPreRelease(), new NoBuild());
217
    }
218
219
    /**
220
     * @param PreRelease|string $preRelease
221
     * @return Version
222
     */
223 2
    public function withPreRelease($preRelease) : Version
224
    {
225 2
        if (is_string($preRelease)) {
226 2
            $preRelease = PreRelease::fromIdentifiersString($preRelease);
227
        }
228
229 2
        return static::fromParts($this->major, $this->minor, $this->patch, $preRelease, new NoBuild());
230
    }
231
232
    /**
233
     * @param Build|string $build
234
     * @return Version
235
     */
236 1
    public function withBuild($build) : Version
237
    {
238 1
        if (is_string($build)) {
239 1
            $build = Build::fromIdentifiersString($build);
240
        }
241
242 1
        return static::fromParts($this->major, $this->minor, $this->patch, $this->preRelease, $build);
243
    }
244
245 2
    public function matches(ConstraintInterface $constraint) : bool
246
    {
247 2
        return $constraint->assert($this);
248
    }
249
250 14
    public function getVersionString() : string
251
    {
252
        return
253 14
            $this->major
254 14
            . '.' . $this->minor
255 14
            . '.' . $this->patch
256 14
            . ($this->isPreRelease() ? '-' . (string) $this->preRelease : '')
257 14
            . ($this->isBuild() ? '+' . (string) $this->build : '')
258
        ;
259
    }
260
261 10
    public function __toString() : string
262
    {
263 10
        return $this->getVersionString();
264
    }
265
266 4
    public function jsonSerialize() : string
267
    {
268 4
        return $this->getVersionString();
269
    }
270
271 4
    public function toArray() : array
272
    {
273
        return [
274 4
            'major' => $this->major,
275 4
            'minor' => $this->minor,
276 4
            'patch' => $this->patch,
277 4
            'preRelease' => $this->preRelease->getIdentifiers(),
278 4
            'build' => $this->build->getIdentifiers(),
279
        ];
280
    }
281
282 20
    protected function getComparator() : ComparatorInterface
283
    {
284 20
        static $comparator = null;
285
286 20
        if (null === $comparator) {
287 1
            $comparator = new SemverComparator();
288
        }
289
290 20
        return $comparator;
291
    }
292
}
293