From 69ef80124f4033f0b995c8e84dfbff5a92b40406 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 8 Dec 2014 12:57:51 +0000 Subject: [PATCH] Add support for capital X in 3.X and || for OR --- doc/01-basic-usage.md | 2 +- .../Package/Version/VersionParser.php | 10 ++++---- .../Package/Version/VersionParserTest.php | 24 +++++++++++++++---- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/doc/01-basic-usage.md b/doc/01-basic-usage.md index abf684488..493c42cd4 100644 --- a/doc/01-basic-usage.md +++ b/doc/01-basic-usage.md @@ -75,7 +75,7 @@ Version constraints can be specified in a few different ways. Name | Example | Description -------------- | ------------------------------------------------------------------ | ----------- Exact version | `1.0.2` | You can specify the exact version of a package. -Range | `>=1.0` `>=1.0,<2.0` >=1.0,<1.1 | >=1.2 | By using comparison operators you can specify ranges of valid versions. Valid operators are `>`, `>=`, `<`, `<=`, `!=`.
You can define multiple ranges. Ranges separated by a space (` `) or comma (`,`) will be treated as a **logical AND**. A pipe (|) will be treated as a **logical OR**. AND has higher precedence than OR. +Range | `>=1.0` `>=1.0,<2.0` >=1.0,<1.1 | >=1.2 | By using comparison operators you can specify ranges of valid versions. Valid operators are `>`, `>=`, `<`, `<=`, `!=`.
You can define multiple ranges. Ranges separated by a space (` `) or comma (`,`) will be treated as a **logical AND**. A pipe (|) or double pipe will be treated as a **logical OR**. AND has higher precedence than OR. Wildcard | `1.0.*` | You can specify a pattern with a `*` wildcard. `1.0.*` is the equivalent of `>=1.0,<1.1`. Tilde Operator | `~1.2` | Very useful for projects that follow semantic versioning. `~1.2` is equivalent to `>=1.2,<2.0`. For more details, read the next section below. diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index eb1529648..9b5df66b0 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -178,10 +178,10 @@ class VersionParser return $this->normalize($name); } - if (preg_match('#^v?(\d+)(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?$#i', $name, $matches)) { + if (preg_match('#^v?(\d+)(\.(?:\d+|[xX*]))?(\.(?:\d+|[xX*]))?(\.(?:\d+|[xX*]))?$#i', $name, $matches)) { $version = ''; for ($i = 1; $i < 5; $i++) { - $version .= isset($matches[$i]) ? str_replace('*', 'x', $matches[$i]) : '.x'; + $version .= isset($matches[$i]) ? str_replace(array('*', 'X'), 'x', $matches[$i]) : '.x'; } return str_replace('x', '9999999', $version).'-dev'; @@ -230,7 +230,7 @@ class VersionParser $constraints = $match[1]; } - $orConstraints = preg_split('{\s*\|\s*}', trim($constraints)); + $orConstraints = preg_split('{\s*\|\|?\s*}', trim($constraints)); $orGroups = array(); foreach ($orConstraints as $constraints) { $andConstraints = preg_split('{(?< ,]) *[, ] *(?!,|as|$)}', $constraints); @@ -273,7 +273,7 @@ class VersionParser } } - if (preg_match('{^[x*](\.[x*])*$}i', $constraint)) { + if (preg_match('{^[xX*](\.[xX*])*$}i', $constraint)) { return array(new EmptyConstraint); } @@ -330,7 +330,7 @@ class VersionParser } // match wildcard constraints - if (preg_match('{^(\d+)(?:\.(\d+))?(?:\.(\d+))?\.[x*]$}', $constraint, $matches)) { + if (preg_match('{^(\d+)(?:\.(\d+))?(?:\.(\d+))?\.[xX*]$}', $constraint, $matches)) { if (isset($matches[3]) && '' !== $matches[3]) { $position = 3; } elseif (isset($matches[2]) && '' !== $matches[2]) { diff --git a/tests/Composer/Test/Package/Version/VersionParserTest.php b/tests/Composer/Test/Package/Version/VersionParserTest.php index a312b2e98..2ed19c416 100644 --- a/tests/Composer/Test/Package/Version/VersionParserTest.php +++ b/tests/Composer/Test/Package/Version/VersionParserTest.php @@ -90,6 +90,7 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase 'forces w.x.y.z/2' => array('0', '0.0.0.0'), 'parses long' => array('10.4.13-beta', '10.4.13.0-beta'), 'parses long/2' => array('10.4.13beta2', '10.4.13.0-beta2'), + 'parses long/semver' => array('10.4.13beta.2', '10.4.13.0-beta2'), 'expand shorthand' => array('10.4.13-b', '10.4.13.0-beta'), 'expand shorthand2' => array('10.4.13-b5', '10.4.13.0-beta5'), 'strips leading v' => array('v1.0.0', '1.0.0.0'), @@ -208,7 +209,7 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase 'match any' => array('*', new EmptyConstraint()), 'match any/2' => array('*.*', new EmptyConstraint()), 'match any/3' => array('*.x.*', new EmptyConstraint()), - 'match any/4' => array('x.x.x.*', new EmptyConstraint()), + 'match any/4' => array('x.X.x.*', new EmptyConstraint()), 'not equal' => array('<>1.0.0', new VersionConstraint('<>', '1.0.0.0')), 'not equal/2' => array('!=1.0.0', new VersionConstraint('!=', '1.0.0.0')), 'greater than' => array('>1.0.0', new VersionConstraint('>', '1.0.0.0')), @@ -255,7 +256,7 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase array('20.*', new VersionConstraint('>=', '20.0.0.0-dev'), new VersionConstraint('<', '21.0.0.0-dev')), array('2.0.*', new VersionConstraint('>=', '2.0.0.0-dev'), new VersionConstraint('<', '2.1.0.0-dev')), array('2.2.x', new VersionConstraint('>=', '2.2.0.0-dev'), new VersionConstraint('<', '2.3.0.0-dev')), - array('2.10.x', new VersionConstraint('>=', '2.10.0.0-dev'), new VersionConstraint('<', '2.11.0.0-dev')), + array('2.10.X', new VersionConstraint('>=', '2.10.0.0-dev'), new VersionConstraint('<', '2.11.0.0-dev')), array('2.1.3.*', new VersionConstraint('>=', '2.1.3.0-dev'), new VersionConstraint('<', '2.1.4.0-dev')), array('0.*', null, new VersionConstraint('<', '1.0.0.0-dev')), ); @@ -310,6 +311,7 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase return array( array('>2.0,<=3.0'), array('>2.0 <=3.0'), + array('>2.0 <=3.0'), array('>2.0, <=3.0'), array('>2.0 ,<=3.0'), array('>2.0 , <=3.0'), @@ -334,7 +336,10 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase $this->assertSame((string) $multi, (string) $parser->parseConstraints('>=1.1.0-alpha4,<1.2-beta2')); } - public function testParseConstraintsMultiDisjunctiveHasPrioOverConjuctive() + /** + * @dataProvider multiConstraintProvider2 + */ + public function testParseConstraintsMultiDisjunctiveHasPrioOverConjuctive($constraint) { $parser = new VersionParser; $first = new VersionConstraint('>', '2.0.0.0'); @@ -342,7 +347,16 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase $third = new VersionConstraint('>', '2.0.6.0'); $multi1 = new MultiConstraint(array($first, $second)); $multi2 = new MultiConstraint(array($multi1, $third), false); - $this->assertSame((string) $multi2, (string) $parser->parseConstraints('>2.0,<2.0.5 | >2.0.6')); + $this->assertSame((string) $multi2, (string) $parser->parseConstraints($constraint)); + } + + public function multiConstraintProvider2() + { + return array( + array('>2.0,<2.0.5 | >2.0.6'), + array('>2.0,<2.0.5 || >2.0.6'), + array('> 2.0 , <2.0.5 | > 2.0.6'), + ); } public function testParseConstraintsMultiWithStabilities() @@ -371,7 +385,7 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase 'invalid version' => array('1.0.0-meh'), 'operator abuse' => array('>2.0,,<=3.0'), 'operator abuse/2' => array('>2.0 ,, <=3.0'), - 'operator abuse/3' => array('>2.0 || <=3.0'), + 'operator abuse/3' => array('>2.0 ||| <=3.0'), ); }