Skip to content

Commit

Permalink
Fix conversion of semver special range
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispluchino committed Sep 24, 2014
1 parent 75e2f7a commit 8af4aa0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
22 changes: 12 additions & 10 deletions Converter/SemverConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,9 @@ protected function matchRangeTokenStep1($i, $match, array &$matches, &$special,
$matches[$i] = ',<=';
} elseif (in_array($match, array('', '<', '>', '=', ','))) {
$replace = in_array($match, array('<', '>')) ? $match : $replace;
} elseif ('~' === $match) {
} elseif (in_array($match, array('~', '^'))) {
$special = $match;
$matches[$i] = '';
} elseif ('^' === $match) {
$matches[$i] = '~';
} else {
$matched = false;
}
Expand All @@ -152,8 +150,8 @@ protected function matchRangeTokenStep2($i, $match, array &$matches, &$special,
$matches[$i] = ',';
} elseif ('||' === $match) {
$matches[$i] = '|';
} elseif ('~' === $special) {
$matches[$i] = $this->replaceSpecialRange($match);
} elseif (in_array($special, array('~', '^'))) {
$matches[$i] = $this->replaceSpecialRange($match, '^' === $special);
$special = null;
} else {
$matches[$i] = $this->convertVersion($match);
Expand All @@ -168,23 +166,27 @@ protected function matchRangeTokenStep2($i, $match, array &$matches, &$special,
/**
* Replaces the special range "~".
*
* @param string $match The match version
* @param string $match The match version
* @param bool $majorVersion Limit the the major version or
*
* @return string the new match version
*/
protected function replaceSpecialRange($match)
protected function replaceSpecialRange($match, $majorVersion = false)
{
$newMatch = $this->convertVersion($match);
$newMatch = '>='.SemverUtil::replaceAlias($newMatch, '>').',<';
$exp = explode('.', $match);
$upVersion = isset($exp[0]) ? $exp[0] : '0';

if (isset($exp[1])) {
$upVersion .= '.' . ($exp[1] + 1);
if (!$majorVersion) {
$minor = isset($exp[1]) ? (int) $exp[1] : 0;
$upVersion .= '.' . ($minor + 1);

} else {
$upVersion .= '.1';
$upVersion = ((int) $upVersion + 1) . '.0';
}


$newMatch .= $this->convertVersion($upVersion);

return $newMatch;
Expand Down
4 changes: 2 additions & 2 deletions Resources/doc/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ Here are the matches currently validated:
| ~ 1.2.3 | >=1.2.3,<1.3 |
| ~1 | >=1,<1.1 |
| ~ 1 | >=1,<1.1 |
| ^1.2.3 | ~1.2.3 |
| ^ 1.2.3 | ~1.2.3 |
| ^1.2.3 | >=1.2.3,<2.0 |
| ^ 1.2.3 | >=1.2.3,<2.0 |
| >1.2.3 <2.0 | >1.2.3,<2.0 |
| &gt;=1.0 &lt;1.1 `¦¦` &gt;=1.2 | &gt;=1.0,&lt;1.1`¦`&gt;=1.2 |
| 1.2.3 - 2.3.4 | >=1.2.3,<=2.3.4 |
Expand Down
5 changes: 3 additions & 2 deletions Tests/Converter/SemverConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public function getTestRanges()
array('<=1.2.3', '<=1.2.3'),
array('~1.2.3', '>=1.2.3,<1.3'),
array('~1', '>=1,<1.1'),
array('^1.2.3', '~1.2.3'),
array('^1.2.3', '>=1.2.3,<2.0'),
array('^1.2', '>=1.2,<2.0'),
array('>1.2.3 <2.0', '>1.2.3,<2.0'),
array('>=1.0 <1.1 || >=1.2', '>=1.0,<1.1|>=1.2'),
array('< 1.2.3', '<1.2.3'),
Expand All @@ -112,7 +113,7 @@ public function getTestRanges()
array('>= 1.2.3', '>=1.2.3'),
array('~ 1.2.3', '>=1.2.3,<1.3'),
array('~ 1', '>=1,<1.1'),
array('^ 1.2.3', '~1.2.3'),
array('^ 1.2.3', '>=1.2.3,<2.0'),
array('1.2.3 - 2.3.4', '>=1.2.3,<=2.3.4'),
array('>=0.10.x', '>=0.10.0'),
array('>=0.10.*', '>=0.10.0'),
Expand Down

0 comments on commit 8af4aa0

Please sign in to comment.