Skip to content

Commit 0b6007e

Browse files
committed
Respect SemVer semantics when comparing package versions (Case 162267)
Comparing a normalised string is not enough. Fixes #49
1 parent d55ff42 commit 0b6007e

File tree

2 files changed

+12
-30
lines changed

2 files changed

+12
-30
lines changed

src/AppBundle/Entity/VersionConstraint.php

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace AppBundle\Entity;
44

5+
use Composer\Semver\Comparator;
56
use Composer\Semver\VersionParser;
67
use InvalidArgumentException;
78

@@ -40,37 +41,10 @@ public function __construct($operator, $versionString)
4041
*/
4142
public function matches(PackageVersion $packageVersion)
4243
{
43-
switch ($this->operator) {
44-
case 'all':
45-
return true;
46-
break;
47-
case '<':
48-
if ($packageVersion->getNormalizedVersion() < $this->normalizedVersionString) {
49-
return true;
50-
}
51-
break;
52-
case '<=':
53-
if ($packageVersion->getNormalizedVersion() <= $this->normalizedVersionString) {
54-
return true;
55-
}
56-
break;
57-
case '>':
58-
if ($packageVersion->getNormalizedVersion() > $this->normalizedVersionString) {
59-
return true;
60-
}
61-
break;
62-
case '>=':
63-
if ($packageVersion->getNormalizedVersion() >= $this->normalizedVersionString) {
64-
return true;
65-
}
66-
break;
67-
case '==':
68-
if ($packageVersion->getNormalizedVersion() == $this->normalizedVersionString) {
69-
return true;
70-
}
71-
break;
44+
if ('all' === $this->operator) {
45+
return true;
7246
}
7347

74-
return false;
48+
return Comparator::compare($packageVersion->getNormalizedVersion(), $this->operator, $this->normalizedVersionString);
7549
}
7650
}

src/AppBundle/Tests/Entity/VersionConstraintTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@ class VersionConstraintTest extends TestCase
1313
public static function provideCases(): Generator
1414
{
1515
yield ['<', '2.0.0', ['1.0.0'], ['3.0.0']];
16+
yield ['<', '2.2.0', ['2.1.0'], ['2.10.0']];
17+
yield ['<', '2.10.0', ['2.2.0'], ['2.11.0']];
1618
yield ['<=', '2.0.0', ['1.0.0', '2.0.0'], ['3.0.0']];
19+
yield ['<=', '2.2.0', ['2.1.0', '2.2.0'], ['2.10.0']];
20+
yield ['<=', '2.10.0', ['2.2.0', '2.10.0'], ['2.11.0']];
1721
yield ['>', '2.0.0', ['3.0.0'], ['1.0.0']];
22+
yield ['>', '2.2.0', ['2.10.0'], ['2.1.0']];
23+
yield ['>', '2.10.0', ['2.11.0'], ['2.2.0']];
1824
yield ['>=', '2.0.0', ['2.0.0', '3.0.0'], ['1.0.0']];
25+
yield ['>=', '2.2.0', ['2.10.0', '2.2.0'], ['2.1.0']];
26+
yield ['>=', '2.10.0', ['2.11.0', '2.10.0'], ['2.2.0']];
1927
yield ['==', '2.0.0', ['2.0.0'], ['1.0.0', '3.0.0']];
2028
yield ['all', '2.0.0', ['1.0.0', '2.0.0', '3.0.0'], []];
2129
}

0 commit comments

Comments
 (0)