Эх сурвалжийг харах

Detect ~> misuse and suggest fix, fixes #2476

Jordi Boggiano 11 жил өмнө
parent
commit
80499bb024

+ 8 - 1
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;

+ 10 - 0
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
      */