From 977fc3241cc515acf8a793498085a11fcf1a7de7 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 29 Nov 2012 12:05:45 +0100 Subject: [PATCH] Clarify exception messages when invalid versions are detected in aliases --- .../Package/Loader/RootPackageLoader.php | 4 ++-- .../Package/Version/VersionParser.php | 24 +++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Composer/Package/Loader/RootPackageLoader.php b/src/Composer/Package/Loader/RootPackageLoader.php index dc7d29554..22275d988 100644 --- a/src/Composer/Package/Loader/RootPackageLoader.php +++ b/src/Composer/Package/Loader/RootPackageLoader.php @@ -104,9 +104,9 @@ class RootPackageLoader extends ArrayLoader if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $reqVersion, $match)) { $aliases[] = array( 'package' => strtolower($reqName), - 'version' => $this->versionParser->normalize($match[1]), + 'version' => $this->versionParser->normalize($match[1], $reqVersion), 'alias' => $match[2], - 'alias_normalized' => $this->versionParser->normalize($match[2]), + 'alias_normalized' => $this->versionParser->normalize($match[2], $reqVersion), ); } } diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index dbafae4af..43f2b87e2 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -86,11 +86,15 @@ class VersionParser * Normalizes a version string to be able to perform comparisons on it * * @param string $version + * @param string $fullVersion optional complete version string to give more context * @return array */ - public function normalize($version) + public function normalize($version, $fullVersion = null) { $version = trim($version); + if (null === $fullVersion) { + $fullVersion = $version; + } // ignore aliases and just assume the alias is required instead of the source if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $version, $match)) { @@ -144,7 +148,14 @@ class VersionParser } 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)); - } 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); } /**