Skip to content

Commit

Permalink
Remove validation of aliases, allow using ^2.x-dev and ~2.x-dev even …
Browse files Browse the repository at this point in the history
…though it is kinda meaningless, fixes #107
  • Loading branch information
Seldaek committed Sep 8, 2020
1 parent 495dcdd commit 71404e6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
34 changes: 26 additions & 8 deletions src/VersionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class VersionParser
*/
private static $modifierRegex = '[._-]?(?:(stable|beta|b|RC|alpha|a|patch|pl|p)((?:[.-]?\d+)*+)?)?([.-]?dev)?';

/** @var array */
private static $stabilities = array('stable', 'RC', 'beta', 'alpha', 'dev');
/** @var string */
private static $stabilitiesRegex = 'stable|RC|beta|alpha|dev';

/**
* Returns the stability of a version.
Expand Down Expand Up @@ -108,12 +108,14 @@ public function normalize($version, $fullVersion = null)

// strip off aliasing
if (preg_match('{^([^,\s]++) ++as ++([^,\s]++)$}', $version, $match)) {
// verify that the alias is a version without constraint
$this->normalize($match[2]);

$version = $match[1];
}

// strip off stability flag
if (preg_match('{@(?:' . self::$stabilitiesRegex . ')$}i', $version, $match)) {
$version = substr($version, 0, strlen($version) - strlen($match[0]));
}

// match master-like branches
if (preg_match('{^(?:dev-)?(?:master|trunk|default)$}i', $version)) {
return '9999999-dev';
Expand Down Expand Up @@ -236,7 +238,7 @@ public function parseConstraints($constraints)
{
$prettyConstraint = $constraints;

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

Expand Down Expand Up @@ -306,7 +308,7 @@ public function parseConstraints($constraints)
*/
private function parseConstraint($constraint)
{
if (preg_match('{^([^,\s]+?)@(' . implode('|', self::$stabilities) . ')$}i', $constraint, $match)) {
if (preg_match('{^([^,\s]+?)@(' . self::$stabilitiesRegex . ')$}i', $constraint, $match)) {
$constraint = $match[1];
if ($match[2] !== 'stable') {
$stabilityModifier = $match[2];
Expand All @@ -317,7 +319,7 @@ private function parseConstraint($constraint)
return array(new EmptyConstraint());
}

$versionRegex = 'v?(\d++)(?:\.(\d++))?(?:\.(\d++))?(?:\.(\d++))?' . self::$modifierRegex . '(?:\+[^\s]+)?';
$versionRegex = 'v?(\d++)(?:\.(\d++|[xX*]))?(?:\.(\d++|[xX*]))?(?:\.(\d++|[xX*]))?' . self::$modifierRegex . '(?:\+[^\s]+)?';

// Tilde Range
//
Expand All @@ -343,6 +345,13 @@ private function parseConstraint($constraint)
$position = 1;
}

// make sure all Xs are converted to the 9999999 it represents
for ($i = $position; $i >= 0; $i--) {
if ($matches[$i] === 'x' || $matches[$i] === 'X' || $matches[$i] === '*') {
$matches[$i] = '9999999';
}
}

// Calculate the stability suffix
$stabilitySuffix = '';
if (empty($matches[5]) && empty($matches[7])) {
Expand Down Expand Up @@ -379,6 +388,11 @@ private function parseConstraint($constraint)
$position = 3;
}

// support ^0.x resolving to 0.9999 - 1.0-dev
if ($position === 2 && ($matches[2] === 'x' || $matches[2] === 'X' || $matches[2] === '*')) {
$position = 1;
}

// Calculate the stability suffix
$stabilitySuffix = '';
if (empty($matches[5]) && empty($matches[7])) {
Expand Down Expand Up @@ -450,6 +464,10 @@ private function parseConstraint($constraint)
$upperBound = new Constraint('<=', $highVersion);
} else {
$highMatch = array('', $matches[10], $matches[11], $matches[12], $matches[13]);

// validate to version
$this->normalize($matches['to']);

$highVersion = $this->manipulateVersionString($highMatch, $empty($matches[11]) ? 1 : 2, 1) . '-dev';
$upperBound = new Constraint('<', $highVersion);
}
Expand Down
25 changes: 21 additions & 4 deletions tests/VersionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public function successfulNormalizedVersions()
'parses arbitrary/3' => array('dev-feature/foo', 'dev-feature/foo'),
'parses arbitrary/4' => array('dev-feature+issue-1', 'dev-feature+issue-1'),
'ignores aliases' => array('dev-master as 1.0.0', '9999999-dev'),
'ignores aliases/2' => array('dev-load-varnish-only-when-used as ^2.0', 'dev-load-varnish-only-when-used'),
'ignores aliases/3' => array('dev-load-varnish-only-when-used@dev as ^2.0@dev', 'dev-load-varnish-only-when-used'),
'ignores stability' => array('1.0.0+foo@dev', '1.0.0.0'),
'ignores stability/2' => array('dev-load-varnish-only-when-used@stable', 'dev-load-varnish-only-when-used'),
'semver metadata/2' => array('1.0.0-beta.5+foo', '1.0.0.0-beta5'),
'semver metadata/3' => array('1.0.0+foo', '1.0.0.0'),
'semver metadata/4' => array('1.0.0-alpha.3.1+foo', '1.0.0.0-alpha3.1'),
Expand Down Expand Up @@ -126,10 +130,6 @@ public function failingNormalizedVersions()
'non-dev arbitrary' => array('feature-foo'),
'metadata w/ space' => array('1.0.0+foo bar'),
'maven style release' => array('1.0.1-SNAPSHOT'),
'Alias and caret' => array('1.0.0+foo as ^2.0'),
'Alias and tilde' => array('1.0.0+foo as ~2.0'),
'Alias and greater than' => array('1.0.0+foo as >2.0'),
'Alias and less than' => array('1.0.0+foo as <2.0'),
'dev with less than' => array('1.0.0<1.0.5-dev'),
'dev with less than/2' => array('1.0.0-dev<1.0.5-dev'),
);
Expand Down Expand Up @@ -187,6 +187,8 @@ 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('=', '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'));
}

public function testParseConstraintsIgnoresReferenceOnDevVersion()
Expand Down Expand Up @@ -352,6 +354,11 @@ public function tildeConstraints()
array('~201903.0-beta', new Constraint('>=', '201903.0-beta'), new Constraint('<', '201904.0.0.0-dev')),
array('~201903.0-stable', new Constraint('>=', '201903.0'), new Constraint('<', '201904.0.0.0-dev')),
array('~201903.205830.1-stable', new Constraint('>=', '201903.205830.1'), new Constraint('<', '201903.205831.0.0-dev')),
array('~2.x.x.x-dev', new Constraint('>=', '2.9999999.9999999.9999999-dev'), new Constraint('<', '2.9999999.10000000.0-dev')),
array('~2.0.3.x-dev', new Constraint('>=', '2.0.3.9999999-dev'), new Constraint('<', '2.0.4.0-dev')),
array('~2.0.x-dev', new Constraint('>=', '2.0.9999999.9999999-dev'), new Constraint('<', '2.1.0.0-dev')),
array('~2.x-dev', new Constraint('>=', '2.9999999.9999999.9999999-dev'), new Constraint('<', '3.0.0.0-dev')),
array('~0.x-dev', new Constraint('>=', '0.9999999.9999999.9999999-dev'), new Constraint('<', '1.0.0.0-dev')),
);
}

Expand Down Expand Up @@ -397,6 +404,11 @@ public function caretConstraints()
array('^201903.0', new Constraint('>=', '201903.0-dev'), new Constraint('<', '201904.0.0.0-dev')),
array('^201903.0-beta', new Constraint('>=', '201903.0-beta'), new Constraint('<', '201904.0.0.0-dev')),
array('^201903.205830.1-stable', new Constraint('>=', '201903.205830.1'), new Constraint('<', '201904.0.0.0-dev')),
array('^2.x.x.x-dev', new Constraint('>=', '2.9999999.9999999.9999999-dev'), new Constraint('<', '3.0.0.0-dev')),
array('^2.0.3.x-dev', new Constraint('>=', '2.0.3.9999999-dev'), new Constraint('<', '3.0.0.0-dev')),
array('^2.0.x-dev', new Constraint('>=', '2.0.9999999.9999999-dev'), new Constraint('<', '3.0.0.0-dev')),
array('^2.x-dev', new Constraint('>=', '2.9999999.9999999.9999999-dev'), new Constraint('<', '3.0.0.0-dev')),
array('^0.x-dev', new Constraint('>=', '0.9999999.9999999.9999999-dev'), new Constraint('<', '1.0.0.0-dev')),
);
}

Expand Down Expand Up @@ -435,6 +447,11 @@ public function hyphenConstraints()
array('1 - 2.1', new Constraint('>=', '1.0.0.0-dev'), new Constraint('<', '2.2.0.0-dev')),
array('1.2 - 2.1.0', new Constraint('>=', '1.2.0.0-dev'), new Constraint('<=', '2.1.0.0')),
array('1.3 - 2.1.3', new Constraint('>=', '1.3.0.0-dev'), new Constraint('<=', '2.1.3.0')),
array('2.x.x.x-dev - 3.x.x.x-dev', new Constraint('>=', '2.9999999.9999999.9999999-dev'), new Constraint('<=', '3.9999999.9999999.9999999-dev')),
array('2.0.3.x-dev - 3.0.3.x-dev', new Constraint('>=', '2.0.3.9999999-dev'), new Constraint('<=', '3.0.3.9999999-dev')),
array('2.0.x-dev - 3.0.x-dev', new Constraint('>=', '2.0.9999999.9999999-dev'), new Constraint('<=', '3.0.9999999.9999999-dev')),
array('2.x-dev - 3.x-dev', new Constraint('>=', '2.9999999.9999999.9999999-dev'), new Constraint('<=', '3.9999999.9999999.9999999-dev')),
array('0.x-dev - 1.x-dev', new Constraint('>=', '0.9999999.9999999.9999999-dev'), new Constraint('<=', '1.9999999.9999999.9999999-dev')),
);
}

Expand Down

0 comments on commit 71404e6

Please sign in to comment.