1
0
Fork 0

Clarify exception messages when invalid versions are detected in aliases

pull/1376/head
Jordi Boggiano 2012-11-29 12:05:45 +01:00
parent 365058ee99
commit 977fc3241c
2 changed files with 22 additions and 6 deletions

View File

@ -104,9 +104,9 @@ class RootPackageLoader extends ArrayLoader
if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $reqVersion, $match)) { if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $reqVersion, $match)) {
$aliases[] = array( $aliases[] = array(
'package' => strtolower($reqName), 'package' => strtolower($reqName),
'version' => $this->versionParser->normalize($match[1]), 'version' => $this->versionParser->normalize($match[1], $reqVersion),
'alias' => $match[2], 'alias' => $match[2],
'alias_normalized' => $this->versionParser->normalize($match[2]), 'alias_normalized' => $this->versionParser->normalize($match[2], $reqVersion),
); );
} }
} }

View File

@ -86,11 +86,15 @@ class VersionParser
* Normalizes a version string to be able to perform comparisons on it * Normalizes a version string to be able to perform comparisons on it
* *
* @param string $version * @param string $version
* @param string $fullVersion optional complete version string to give more context
* @return array * @return array
*/ */
public function normalize($version) public function normalize($version, $fullVersion = null)
{ {
$version = trim($version); $version = trim($version);
if (null === $fullVersion) {
$fullVersion = $version;
}
// ignore aliases and just assume the alias is required instead of the source // ignore aliases and just assume the alias is required instead of the source
if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $version, $match)) { if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $version, $match)) {
@ -144,7 +148,14 @@ class VersionParser
} catch (\Exception $e) {} } catch (\Exception $e) {}
} }
throw new \UnexpectedValueException('Invalid version string "'.$version.'"'); $extraMessage = '';
if (preg_match('{ +as +'.preg_quote($version).'$}', $fullVersion)) {
$extraMessage = ' in "'.$fullVersion.'", the alias must be an exact version';
} elseif (preg_match('{^'.preg_quote($version).' +as +}', $fullVersion)) {
$extraMessage = ' in "'.$fullVersion.'", the alias source must be an exact version, if it is a branch name you should prefix it with dev-';
}
throw new \UnexpectedValueException('Invalid version string "'.$version.'"'.$extraMessage);
} }
/** /**
@ -315,10 +326,15 @@ class VersionParser
} }
return array(new VersionConstraint($matches[1] ?: '=', $version)); return array(new VersionConstraint($matches[1] ?: '=', $version));
} catch (\Exception $e) {} } catch (\Exception $e) { }
} }
throw new \UnexpectedValueException('Could not parse version constraint '.$constraint); $message = 'Could not parse version constraint '.$constraint;
if (isset($e)) {
$message .= ': '.$e->getMessage();
}
throw new \UnexpectedValueException($message);
} }
/** /**