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