|
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
|
|
|
abstract class VersionComparator |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param Version $version1 |
|
20
|
|
|
* @param Version $version2 |
|
21
|
|
|
* |
|
22
|
|
|
* @return int |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function compare(Version $version1, Version $version2) : int |
|
25
|
|
|
{ |
|
26
|
|
|
$compare = static::compareNumericParts($version1, $version2); |
|
|
|
|
|
|
27
|
|
|
if (0 !== $compare) { |
|
28
|
|
|
return $compare; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return static::comparePreRelease($version1, $version2); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param Version $version1 |
|
36
|
|
|
* @param Version $version2 |
|
37
|
|
|
* |
|
38
|
|
|
* @return int |
|
39
|
|
|
*/ |
|
40
|
|
|
private static function compareNumericParts(Version $version1, Version $version2) : int |
|
41
|
|
|
{ |
|
42
|
|
|
$compare = $version1->getMajor() <=> $version2->getMajor(); |
|
43
|
|
|
if (0 !== $compare) { |
|
44
|
|
|
return $compare; |
|
45
|
|
|
} |
|
46
|
|
|
$compare = $version1->getMinor() <=> $version2->getMinor(); |
|
47
|
|
|
if (0 !== $compare) { |
|
48
|
|
|
return $compare; |
|
49
|
|
|
} |
|
50
|
|
|
$compare = $version1->getPatch() <=> $version2->getPatch(); |
|
51
|
|
|
if (0 !== $compare) { |
|
52
|
|
|
return $compare; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return 0; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param Version $version1 |
|
60
|
|
|
* @param Version $version2 |
|
61
|
|
|
* |
|
62
|
|
|
* @return int |
|
63
|
|
|
*/ |
|
64
|
|
|
private static function comparePreRelease(Version $version1, Version $version2) : int |
|
65
|
|
|
{ |
|
66
|
|
|
$leftPreReleaseIsEmpty = '' === $version1->getPreRelease(); |
|
67
|
|
|
$rightPreReleaseIsEmpty = '' === $version2->getPreRelease(); |
|
68
|
|
|
if ($rightPreReleaseIsEmpty !== $leftPreReleaseIsEmpty) { |
|
69
|
|
|
return $leftPreReleaseIsEmpty ? 1 : -1; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($leftPreReleaseIsEmpty) { |
|
73
|
|
|
return 0; |
|
74
|
|
|
} |
|
75
|
|
|
// need to compare each subversion |
|
76
|
|
|
$preReleaseIdentifiers1 = explode('.', $version1->getPreRelease()); |
|
77
|
|
|
$preReleaseIdentifiers2 = explode('.', $version2->getPreRelease()); |
|
78
|
|
|
|
|
79
|
|
|
do { |
|
80
|
|
|
$preReleasePart1 = array_shift($preReleaseIdentifiers1); |
|
81
|
|
|
$preReleasePart2 = array_shift($preReleaseIdentifiers2); |
|
82
|
|
|
|
|
83
|
|
|
$myCurrentPreReleasePartIsNull = null === $preReleasePart1; |
|
84
|
|
|
$theirCurrentPreReleasePartIsNull = null === $preReleasePart2; |
|
85
|
|
|
|
|
86
|
|
|
if ($myCurrentPreReleasePartIsNull !== $theirCurrentPreReleasePartIsNull) { |
|
87
|
|
|
return $myCurrentPreReleasePartIsNull ? -1 : 1; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$compare = static::comparePreReleasePart($preReleasePart1, $preReleasePart2); |
|
|
|
|
|
|
91
|
|
|
if (0 !== $compare) { |
|
92
|
|
|
return $compare; |
|
93
|
|
|
} |
|
94
|
|
|
} while (count($preReleaseIdentifiers1) || count($preReleaseIdentifiers2)); |
|
95
|
|
|
|
|
96
|
|
|
return 0; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $preReleasePart1 |
|
101
|
|
|
* @param string $preReleasePart2 |
|
102
|
|
|
* |
|
103
|
|
|
* @return int |
|
104
|
|
|
*/ |
|
105
|
|
|
private static function comparePreReleasePart(string $preReleasePart1, string $preReleasePart2) : int |
|
106
|
|
|
{ |
|
107
|
|
|
$mineIsInt = ctype_digit($preReleasePart1) && strpos($preReleasePart1, '00') !== 0; |
|
108
|
|
|
$theirIsInt = ctype_digit($preReleasePart2) && strpos($preReleasePart2, '00') !== 0; |
|
109
|
|
|
|
|
110
|
|
|
if ($mineIsInt !== $theirIsInt) { |
|
111
|
|
|
return $mineIsInt ? -1 : 1; |
|
112
|
|
|
} |
|
113
|
|
|
if ($mineIsInt) { |
|
114
|
|
|
return ((int) $preReleasePart1) <=> ((int) $preReleasePart2); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $preReleasePart1 <=> $preReleasePart2; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: