From 80499bb02418711b34bba59c1a6d8032429e5702 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 6 Dec 2013 11:57:37 +0100 Subject: [PATCH] Detect ~> misuse and suggest fix, fixes #2476 --- src/Composer/Package/Version/VersionParser.php | 9 ++++++++- .../Test/Package/Version/VersionParserTest.php | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index 8b519f943..ea37d821d 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -275,7 +275,14 @@ class VersionParser // version, to ensure that unstable instances of the current version are allowed. // however, if a stability suffix is added to the constraint, then a >= match on the current version is // used instead - if (preg_match('{^~(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?'.self::$modifierRegex.'?$}i', $constraint, $matches)) { + if (preg_match('{^~>?(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?'.self::$modifierRegex.'?$}i', $constraint, $matches)) { + if (substr($constraint, 0, 2) === '~>') { + throw new \UnexpectedValueException( + 'Could not parse version constraint '.$constraint.': '. + 'Invalid operator "~>", you probably meant to use the "~" operator' + ); + } + // Work out which position in the version we are operating at if (isset($matches[4]) && '' !== $matches[4]) { $position = 4; diff --git a/tests/Composer/Test/Package/Version/VersionParserTest.php b/tests/Composer/Test/Package/Version/VersionParserTest.php index e16cdc10c..18b5c493f 100644 --- a/tests/Composer/Test/Package/Version/VersionParserTest.php +++ b/tests/Composer/Test/Package/Version/VersionParserTest.php @@ -181,6 +181,16 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase $this->assertSame((string) new VersionConstraint('=', '1.0.0.0'), (string) $parser->parseConstraints('1.0#trunk/@123')); } + /** + * @expectedException UnexpectedValueException + * @expectedExceptionMessage Invalid operator "~>", you probably meant to use the "~" operator + */ + public function testParseConstraintsNudgesRubyDevsTowardsThePathOfRighteousness() + { + $parser = new VersionParser; + $parser->parseConstraints('~>1.2'); + } + /** * @dataProvider simpleConstraints */