Completed
Pull Request — master (#2)
by Julien
02:13
created

VersionComparator::compare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of semver/semver.
5
 *
6
 * (c) SemVer <https://github.com/git-pull-request>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
declare(strict_types = 1);
13
14
namespace SemVer\SemVer;
15
16
final class VersionComparator
17
{
18
    /** @var Version */
19
    private $first;
20
    /** @var Version */
21
    private $second;
22
23
    /**
24
     * @param Version $version1
25
     * @param Version $version2
26
     *
27
     * @return int
28
     */
29
    public static function compare(Version $version1, Version $version2) : int
30
    {
31
        $obj = new self($version1, $version2);
32
33
        return $obj->doCompare();
34
    }
35
36
    /**
37
     * @param Version $first
38
     * @param Version $second
39
     */
40
    private function __construct(Version $first, Version $second)
41
    {
42
        $this->first = $first;
43
        $this->second = $second;
44
    }
45
46
    /**
47
     * @return int
48
     */
49
    private function doCompare() : int
50
    {
51
        $compare = $this->compareNumericParts();
52
        if (0 !== $compare) {
53
            return $compare;
54
        }
55
56
        return $this->comparePreRelease();
57
    }
58
59
    /**
60
     * @return int
61
     */
62
    private function compareNumericParts() : int
63
    {
64
        $compare = $this->first->getMajor() <=> $this->second->getMajor();
65
        if (0 !== $compare) {
66
            return $compare;
67
        }
68
        $compare = $this->first->getMinor() <=> $this->second->getMinor();
69
        if (0 !== $compare) {
70
            return $compare;
71
        }
72
        $compare = $this->first->getPatch() <=> $this->second->getPatch();
73
74
        return $compare;
75
    }
76
77
    /**
78
     * @return int
79
     */
80
    private function comparePreRelease() : int
81
    {
82
        $preRelease1 = $this->first->getPreRelease();
83
        $preRelease2 = $this->second->getPreRelease();
84
85
        $leftPreReleaseIsEmpty = '' === $preRelease1;
86
        $rightPreReleaseIsEmpty = '' === $preRelease2;
87
        if ($rightPreReleaseIsEmpty !== $leftPreReleaseIsEmpty) {
88
            return $leftPreReleaseIsEmpty ? 1 : -1;
89
        }
90
91
        if ($leftPreReleaseIsEmpty) {
92
            return 0;
93
        }
94
95
        return $this->comparePreReleaseIdentifiers(explode('.', $preRelease1), explode('.', $preRelease2));
96
    }
97
98
    /**
99
     * @param array $identifiers1
100
     * @param array $identifiers2
101
     *
102
     * @return int
103
     */
104
    private function comparePreReleaseIdentifiers(array $identifiers1, array $identifiers2) : int
105
    {
106
        $preReleasePart1 = array_shift($identifiers1);
107
        $preReleasePart2 = array_shift($identifiers2);
108
        if (null === $preReleasePart1) {
109
            return -1;
110
        }
111
112
        if (null === $preReleasePart2) {
113
            return 1;
114
        }
115
116
        $compare = $this->comparePreReleaseIdentifier($preReleasePart1, $preReleasePart2);
117
        if (0 === $compare) {
118
            return $this->comparePreReleaseIdentifiers($identifiers1, $identifiers2);
119
        }
120
121
        return $compare;
122
    }
123
124
    /**
125
     * @param $identifier1
126
     * @param $identifier2
127
     *
128
     * @return int
129
     */
130
    private function comparePreReleaseIdentifier($identifier1, $identifier2) : int
131
    {
132
        $mineIsInt = $this->isIdentifierInt($identifier1);
133
        $theirIsInt = $this->isIdentifierInt($identifier2);
134
135
        if ($mineIsInt !== $theirIsInt) {
136
            return $mineIsInt ? -1 : 1;
137
        }
138
139
        if ($mineIsInt) {
140
            return ((int)$identifier1) <=> ((int)$identifier2);
141
        }
142
143
        return $identifier1 <=> $identifier2;
144
    }
145
146
    /**
147
     * @param string $identifier
148
     *
149
     * @return bool
150
     */
151
    private function isIdentifierInt(string $identifier) : bool
152
    {
153
        return ctype_digit($identifier) && strpos($identifier, '00') !== 0;
154
    }
155
}
156