Skip to content

Commit

Permalink
Allow @stability to stand alone in a multi constraint, fixes #109
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Sep 9, 2020
1 parent 9787c20 commit 7067a2f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/VersionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,6 @@ public function parseConstraints($constraints)
{
$prettyConstraint = $constraints;

if (preg_match('{^([^,\s]*?)@(?:' . self::$stabilitiesRegex . ')$}i', $constraints, $match)) {
$constraints = empty($match[1]) ? '*' : $match[1];
}

if (preg_match('{^(dev-[^,\s@]+?|[^,\s@]+?\.x-dev)#.+$}i', $constraints, $match)) {
$constraints = $match[1];
}

$orConstraints = preg_split('{\s*\|\|?\s*}', trim($constraints));
$orGroups = array();

Expand Down Expand Up @@ -309,13 +301,19 @@ public function parseConstraints($constraints)
*/
private function parseConstraint($constraint)
{
if (preg_match('{^([^,\s]+?)@(' . self::$stabilitiesRegex . ')$}i', $constraint, $match)) {
$constraint = $match[1];
// strip @stability flags, and keep it for later use
if (preg_match('{^([^,\s]*?)@(' . self::$stabilitiesRegex . ')$}i', $constraint, $match)) {
$constraint = '' !== $match[1] ? $match[1] : '*';
if ($match[2] !== 'stable') {
$stabilityModifier = $match[2];
}
}

// get rid of #refs as those are used by composer only
if (preg_match('{^(dev-[^,\s@]+?|[^,\s@]+?\.x-dev)#.+$}i', $constraint, $match)) {
$constraint = $match[1];
}

if (preg_match('{^v?[xX*](\.[xX*])*$}i', $constraint)) {
return array(new EmptyConstraint());
}
Expand Down Expand Up @@ -484,9 +482,11 @@ private function parseConstraint($constraint)
try {
$version = $this->normalize($matches[2]);

if (!empty($stabilityModifier) && self::parseStability($version) === 'stable') {
$op = $matches[1] ?: '=';

if ($op !== '==' && $op !== '=' && !empty($stabilityModifier) && self::parseStability($version) === 'stable') {
$version .= '-' . $stabilityModifier;
} elseif ('<' === $matches[1] || '>=' === $matches[1]) {
} elseif ('<' === $op || '>=' === $op) {
if (!preg_match('/-' . self::$modifierRegex . '$/', strtolower($matches[2]))) {
if (strpos($matches[2], 'dev-') !== 0) {
$version .= '-dev';
Expand Down
21 changes: 21 additions & 0 deletions tests/VersionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public function testParseConstraintsIgnoresStabilityFlag()
$parser = new VersionParser();

$this->assertSame((string) new Constraint('=', '1.0.0.0'), (string) $parser->parseConstraints('1.0@dev'));
$this->assertSame((string) new Constraint('>=', '1.0.0.0-beta'), (string) $parser->parseConstraints('>=1.0@beta'));
$this->assertSame((string) new Constraint('=', 'dev-load-varnish-only-when-used'), (string) $parser->parseConstraints('dev-load-varnish-only-when-used as ^2.0@dev'));
$this->assertSame((string) new Constraint('=', 'dev-load-varnish-only-when-used'), (string) $parser->parseConstraints('dev-load-varnish-only-when-used@dev as ^2.0@dev'));
}
Expand Down Expand Up @@ -631,6 +632,26 @@ public function testParseConstraintsMultiWithStabilities()
$this->assertSame((string) $multi, (string) $parser->parseConstraints('>2.0@stable,<=3.0@dev'));
}

public function testParseConstraintsMultiWithStabilitiesWildcard()
{
$parser = new VersionParser();
$first = new Constraint('>', '2.0.0.0');
$second = new EmptyConstraint();
$multi = new MultiConstraint(array($first, $second));

$this->assertSame((string) $multi, (string) $parser->parseConstraints('>2.0@stable,@dev'));
}

public function testParseConstraintsMultiWithStabilitiesZero()
{
$parser = new VersionParser();
$first = new Constraint('>', '2.0.0.0');
$second = new Constraint('==', '0.0.0.0');
$multi = new MultiConstraint(array($first, $second), false);

$this->assertSame((string) $multi, (string) $parser->parseConstraints('>2.0@stable || 0@dev'));
}

/**
* @dataProvider failingConstraints
* @expectedException \UnexpectedValueException
Expand Down

0 comments on commit 7067a2f

Please sign in to comment.