From cea4c050218723375da2a9deb345dd63e00e6614 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 13 Jan 2013 16:36:19 +0100 Subject: [PATCH] Fix parsing of ~ version constraint with unstable versions, fixes #1476 --- .../Package/Version/VersionParser.php | 40 +++++++++++++++---- .../Package/Version/VersionParserTest.php | 3 ++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index ff60c3d0d..8d0d4d0f2 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -128,10 +128,7 @@ class VersionParser if ('stable' === $matches[$index]) { return $version; } - $mod = array('{^pl?$}i', '{^rc$}i'); - $modNormalized = array('patch', 'RC'); - $version .= '-'.preg_replace($mod, $modNormalized, strtolower($matches[$index])) - . (!empty($matches[$index+1]) ? $matches[$index+1] : ''); + $version .= '-' . $this->expandStability($matches[$index]) . (!empty($matches[$index+1]) ? $matches[$index+1] : ''); } if (!empty($matches[$index+2])) { @@ -259,22 +256,30 @@ class VersionParser return array(); } - if (preg_match('{^~(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?$}', $constraint, $matches)) { - if (isset($matches[4])) { + if (preg_match('{^~(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?'.self::$modifierRegex.'?$}', $constraint, $matches)) { + if (isset($matches[4]) && '' !== $matches[4]) { $highVersion = $matches[1] . '.' . $matches[2] . '.' . ($matches[3] + 1) . '.0-dev'; $lowVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3]. '.' . $matches[4]; - } elseif (isset($matches[3])) { + } elseif (isset($matches[3]) && '' !== $matches[3]) { $highVersion = $matches[1] . '.' . ($matches[2] + 1) . '.0.0-dev'; $lowVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3]. '.0'; } else { $highVersion = ($matches[1] + 1) . '.0.0.0-dev'; - if (isset($matches[2])) { + if (isset($matches[2]) && '' !== $matches[2]) { $lowVersion = $matches[1] . '.' . $matches[2] . '.0.0'; } else { $lowVersion = $matches[1] . '.0.0.0'; } } + if (!empty($matches[5])) { + $lowVersion .= '-' . $this->expandStability($matches[5]) . (!empty($matches[6]) ? $matches[6] : ''); + } + + if (!empty($matches[7])) { + $lowVersion .= '-dev'; + } + return array( new VersionConstraint('>=', $lowVersion), new VersionConstraint('<', $highVersion), @@ -337,6 +342,25 @@ class VersionParser throw new \UnexpectedValueException($message); } + private function expandStability($stability) + { + $stability = strtolower($stability); + + switch ($stability) { + case 'a': + return 'alpha'; + case 'b': + return 'beta'; + case 'p': + case 'pl': + return 'patch'; + case 'rc': + return 'RC'; + default: + return $stability; + } + } + /** * Parses a name/version pairs and returns an array of pairs + the * diff --git a/tests/Composer/Test/Package/Version/VersionParserTest.php b/tests/Composer/Test/Package/Version/VersionParserTest.php index 99a4237da..c115a4e22 100644 --- a/tests/Composer/Test/Package/Version/VersionParserTest.php +++ b/tests/Composer/Test/Package/Version/VersionParserTest.php @@ -263,6 +263,9 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase array('~1.2', new VersionConstraint('>=', '1.2.0.0'), new VersionConstraint('<', '2.0.0.0-dev')), array('~1.2.3', new VersionConstraint('>=', '1.2.3.0'), new VersionConstraint('<', '1.3.0.0-dev')), array('~1.2.3.4', new VersionConstraint('>=', '1.2.3.4'), new VersionConstraint('<', '1.2.4.0-dev')), + array('~1.2-beta',new VersionConstraint('>=', '1.2.0.0-beta'), new VersionConstraint('<', '2.0.0.0-dev')), + array('~1.2-b2', new VersionConstraint('>=', '1.2.0.0-beta2'), new VersionConstraint('<', '2.0.0.0-dev')), + array('~1.2.2-dev', new VersionConstraint('>=', '1.2.2.0-dev'), new VersionConstraint('<', '1.3.0.0-dev')), ); }