Version::compare()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.1429

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 7
nop 1
dl 0
loc 17
ccs 12
cts 14
cp 0.8571
crap 7.1429
rs 8.8333
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Igni\Storage\Migration;
4
5
use Igni\Storage\Exception\VersionException;
6
7
/**
8
 * Value object used by MigrationManager and VersionSynchronizer.
9
 *
10
 * @see \Igni\Storage\MigrationManager
11
 * @see \Igni\Storage\Migration\VersionSynchronizer
12
 *
13
 * @package Igni\Storage\Migration
14
 */
15
final class Version
16
{
17
    private $major;
18
    private $minor;
19
    private $patch;
20
21 15
    public function __construct(int $major, int $minor, int $patch)
22
    {
23 15
        $this->major = $major;
24 15
        $this->minor = $minor;
25 15
        $this->patch = $patch;
26 15
    }
27
28 2
    public function getMajor(): int
29
    {
30 2
        return $this->major;
31
    }
32
33 2
    public function getMinor(): int
34
    {
35 2
        return $this->minor;
36
    }
37
38 2
    public function getPatch(): int
39
    {
40 2
        return $this->patch;
41
    }
42
43 7
    public function compare(Version $version): int
44
    {
45 7
        if ($version->major > $this->major) {
46 2
            return 1;
47 7
        } else if ($this->major > $version->major) {
48 2
            return -1;
49 7
        } else if ($version->minor > $this->minor) {
50 5
            return 1;
51 7
        } else if ($this->minor > $version->minor) {
52 5
            return -1;
53 6
        } else if ($version->patch > $this->patch) {
54
            return 1;
55 6
        } else if ($this->patch > $version->patch) {
56
            return -1;
57
        }
58
59 6
        return 0;
60
    }
61
62 2
    public function lowerThan(Version $version): bool
63
    {
64 2
        return $this->compare($version) === 1;
65
    }
66
67 2
    public function lowerOrEquals(Version $version): bool
68
    {
69 2
        return $this->compare($version) >= 0;
70
    }
71
72 3
    public function greaterThan(Version $version): bool
73
    {
74 3
        return $this->compare($version) === -1;
75
    }
76
77 1
    public function greaterOrEquals(Version $version): bool
78
    {
79 1
        return $this->compare($version) <= 0;
80
    }
81
82 1
    public function equals(Version $version): bool
83
    {
84 1
        return $this->compare($version) === 0;
85
    }
86
87 2
    public function equalsLiteral(string $version): bool
88
    {
89 2
        return (string) $this === $version;
90
    }
91
92 1
    public function getNextPatch(): self
93
    {
94 1
        $instance = clone $this;
95 1
        $instance->patch++;
96
97 1
        return $instance;
98
    }
99
100 1
    public function getNextMinor(): self
101
    {
102 1
        $instance = clone $this;
103 1
        $instance->minor++;
104
105 1
        return $instance;
106
    }
107
108 1
    public function getNextMajor(): self
109
    {
110 1
        $instance = clone $this;
111 1
        $instance->major++;
112
113 1
        return $instance;
114
    }
115
116 8
    public static function fromString(string $version): self
117
    {
118 8
        $version = explode('.', $version);
119 8
        if (count($version) !== 3) {
120
            throw VersionException::forParseError(implode('.', $version));
121
        }
122
123 8
        return new self((int) $version[0], (int) $version[1], (int) $version[2]);
124
    }
125
126 5
    public function __toString(): string
127
    {
128 5
        return "{$this->major}.{$this->minor}.{$this->patch}";
129
    }
130
}
131