From 9438f3a58f5789016fe121e23b92ef09a4eb37ae Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 8 Dec 2014 16:57:27 +0000 Subject: [PATCH] Use better semver ranges for 0.x versions, fixes #3518 --- src/Composer/Package/Version/VersionSelector.php | 16 ++++++++++++++-- .../Test/Package/Version/VersionSelectorTest.php | 5 ++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Composer/Package/Version/VersionSelector.php b/src/Composer/Package/Version/VersionSelector.php index 202b11883..ce416ca12 100644 --- a/src/Composer/Package/Version/VersionSelector.php +++ b/src/Composer/Package/Version/VersionSelector.php @@ -103,10 +103,22 @@ class VersionSelector // attempt to transform 2.1.1 to 2.1 // this allows you to upgrade through minor versions $semanticVersionParts = explode('.', $version); + $op = '~'; + // check to see if we have a semver-looking version if (count($semanticVersionParts) == 4 && preg_match('{^0\D?}', $semanticVersionParts[3])) { // remove the last parts (i.e. the patch version number and any extra) - unset($semanticVersionParts[2], $semanticVersionParts[3]); + if ($semanticVersionParts[0] === '0') { + if ($semanticVersionParts[2] === '0') { + $semanticVersionParts[2] = '*'; + unset($semanticVersionParts[3]); + } else { + $semanticVersionParts[3] = '*'; + } + $op = ''; + } else { + unset($semanticVersionParts[2], $semanticVersionParts[3]); + } $version = implode('.', $semanticVersionParts); } else { return $prettyVersion; @@ -118,7 +130,7 @@ class VersionSelector } // 2.1 -> ~2.1 - return '~'.$version; + return $op.$version; } private function getParser() diff --git a/tests/Composer/Test/Package/Version/VersionSelectorTest.php b/tests/Composer/Test/Package/Version/VersionSelectorTest.php index 2a9cb45a0..884337396 100644 --- a/tests/Composer/Test/Package/Version/VersionSelectorTest.php +++ b/tests/Composer/Test/Package/Version/VersionSelectorTest.php @@ -98,7 +98,10 @@ class VersionSelectorTest extends \PHPUnit_Framework_TestCase array('v1.2.1', false, 'stable', '~1.2'), array('3.1.2-pl2', false, 'stable', '~3.1'), array('3.1.2-patch', false, 'stable', '~3.1'), - // for non-stable versions, we add ~, but don't try the (1.2.1 -> 1.2) transformation + array('0.1.0', false, 'stable', '0.1.*'), + array('0.1.3', false, 'stable', '0.1.3.*'), + array('0.0.3', false, 'stable', '0.0.3.*'), + array('0.0.3-alpha', false, 'alpha', '0.0.3.*@alpha'), array('2.0-beta.1', false, 'beta', '~2.0@beta'), array('3.1.2-alpha5', false, 'alpha', '~3.1@alpha'), array('3.0-RC2', false, 'RC', '~3.0@RC'),