These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Deployer\Component\Version; |
||
4 | |||
5 | /** |
||
6 | * Compares two Version instances. |
||
7 | * |
||
8 | * @author Kevin Herrera <[email protected]> |
||
9 | */ |
||
10 | class Comparator |
||
11 | { |
||
12 | /** |
||
13 | * The version is equal to another. |
||
14 | */ |
||
15 | const EQUAL_TO = 0; |
||
16 | |||
17 | /** |
||
18 | * The version is greater than another. |
||
19 | */ |
||
20 | const GREATER_THAN = 1; |
||
21 | |||
22 | /** |
||
23 | * The version is less than another. |
||
24 | */ |
||
25 | const LESS_THAN = -1; |
||
26 | |||
27 | /** |
||
28 | * Compares one version with another. |
||
29 | * |
||
30 | * @param Version $left The left version to compare. |
||
31 | * @param Version $right The right version to compare. |
||
32 | * |
||
33 | * @return integer Returns Comparator::EQUAL_TO if the two versions are |
||
34 | * equal. If the left version is less than the right |
||
35 | * version, Comparator::LESS_THAN is returned. If the left |
||
36 | * version is greater than the right version, |
||
37 | * Comparator::GREATER_THAN is returned. |
||
38 | */ |
||
39 | public static function compareTo(Version $left, Version $right) |
||
40 | { |
||
41 | switch (true) { |
||
42 | case ($left->getMajor() < $right->getMajor()): |
||
43 | return self::LESS_THAN; |
||
44 | case ($left->getMajor() > $right->getMajor()): |
||
45 | return self::GREATER_THAN; |
||
46 | case ($left->getMinor() > $right->getMinor()): |
||
47 | return self::GREATER_THAN; |
||
48 | case ($left->getMinor() < $right->getMinor()): |
||
49 | return self::LESS_THAN; |
||
50 | case ($left->getPatch() > $right->getPatch()): |
||
51 | return self::GREATER_THAN; |
||
52 | case ($left->getPatch() < $right->getPatch()): |
||
53 | return self::LESS_THAN; |
||
54 | // @codeCoverageIgnoreStart |
||
55 | } |
||
56 | // @codeCoverageIgnoreEnd |
||
57 | |||
58 | return self::compareIdentifiers( |
||
59 | $left->getPreRelease(), |
||
60 | $right->getPreRelease() |
||
61 | ); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Checks if the left version is equal to the right. |
||
66 | * |
||
67 | * @param Version $left The left version to compare. |
||
68 | * @param Version $right The right version to compare. |
||
69 | * |
||
70 | * @return boolean TRUE if the left version is equal to the right, FALSE |
||
71 | * if not. |
||
72 | */ |
||
73 | public static function isEqualTo(Version $left, Version $right) |
||
74 | { |
||
75 | return (self::EQUAL_TO === self::compareTo($left, $right)); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Checks if the left version is greater than the right. |
||
80 | * |
||
81 | * @param Version $left The left version to compare. |
||
82 | * @param Version $right The right version to compare. |
||
83 | * |
||
84 | * @return boolean TRUE if the left version is greater than the right, |
||
85 | * FALSE if not. |
||
86 | */ |
||
87 | public static function isGreaterThan(Version $left, Version $right) |
||
88 | { |
||
89 | return (self::GREATER_THAN === self::compareTo($left, $right)); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Checks if the left version is less than the right. |
||
94 | * |
||
95 | * @param Version $left The left version to compare. |
||
96 | * @param Version $right The right version to compare. |
||
97 | * |
||
98 | * @return boolean TRUE if the left version is less than the right, |
||
99 | * FALSE if not. |
||
100 | */ |
||
101 | public static function isLessThan(Version $left, Version $right) |
||
102 | { |
||
103 | return (self::LESS_THAN === self::compareTo($left, $right)); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Compares the identifier components of the left and right versions. |
||
108 | * |
||
109 | * @param array $left The left identifiers. |
||
110 | * @param array $right The right identifiers. |
||
111 | * |
||
112 | * @return integer Returns Comparator::EQUAL_TO if the two identifiers are |
||
113 | * equal. If the left identifiers is less than the right |
||
114 | * identifiers, Comparator::LESS_THAN is returned. If the |
||
115 | * left identifiers is greater than the right identifiers, |
||
116 | * Comparator::GREATER_THAN is returned. |
||
117 | */ |
||
118 | public static function compareIdentifiers(array $left, array $right) |
||
119 | { |
||
120 | if ($left && empty($right)) { |
||
121 | return self::LESS_THAN; |
||
122 | } elseif (empty($left) && $right) { |
||
123 | return self::GREATER_THAN; |
||
124 | } |
||
125 | |||
126 | $l = $left; |
||
127 | $r = $right; |
||
128 | $x = self::GREATER_THAN; |
||
129 | $y = self::LESS_THAN; |
||
130 | |||
131 | if (count($l) < count($r)) { |
||
132 | $l = $right; |
||
133 | $r = $left; |
||
134 | $x = self::LESS_THAN; |
||
135 | $y = self::GREATER_THAN; |
||
136 | } |
||
137 | |||
138 | foreach (array_keys($l) as $i) { |
||
139 | if (!isset($r[$i])) { |
||
140 | return $x; |
||
141 | } |
||
142 | |||
143 | if ($l[$i] === $r[$i]) { |
||
144 | continue; |
||
145 | } |
||
146 | |||
147 | View Code Duplication | if (true === ($li = (false != preg_match('/^\d+$/', $l[$i])))) { |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
148 | $l[$i] = intval($l[$i]); |
||
149 | } |
||
150 | |||
151 | View Code Duplication | if (true === ($ri = (false != preg_match('/^\d+$/', $r[$i])))) { |
|
0 ignored issues
–
show
|
|||
152 | $r[$i] = intval($r[$i]); |
||
153 | } |
||
154 | |||
155 | if ($li && $ri) { |
||
156 | return ($l[$i] > $r[$i]) ? $x : $y; |
||
157 | } elseif (!$li && $ri) { |
||
158 | return $x; |
||
159 | } elseif ($li && !$ri) { |
||
160 | return $y; |
||
161 | } |
||
162 | |||
163 | $result = strcmp($l[$i], $r[$i]); |
||
164 | |||
165 | if ($result > 0) { |
||
166 | return $x; |
||
167 | } elseif ($result < 0) { |
||
168 | return $y; |
||
169 | } |
||
170 | } |
||
171 | |||
172 | return self::EQUAL_TO; |
||
173 | } |
||
174 | } |
||
175 |