From 99dab8aebdef0f833294268af026069f6f1368cf Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Sat, 30 May 2015 23:24:20 -0700 Subject: [PATCH] Move VersionParser::formatVersion() to BasePackage::getFullPrettyVersion() Working towards #3545. formatVersion() does not belong in VersionParser since it depends upon a Package object, and is creating a more complete pretty formatted version, not parsing anything. The new getFullPrettyVersion() method can be seen as an extension to getPrettyVersion(), and is located in BasePackage as a result. Callers to VersionParser::formatVersion() were not updated in this commit to demonstrate that no functionality was changed in this refactor. They will be updated in a follow up commit. --- src/Composer/Package/BasePackage.php | 17 +++++++++++++++++ src/Composer/Package/PackageInterface.php | 10 ++++++++++ src/Composer/Package/Version/VersionParser.php | 11 +---------- .../Test/Package/Version/VersionParserTest.php | 2 +- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/Composer/Package/BasePackage.php b/src/Composer/Package/BasePackage.php index 965e5ddc8..9b2a683ca 100644 --- a/src/Composer/Package/BasePackage.php +++ b/src/Composer/Package/BasePackage.php @@ -206,6 +206,23 @@ abstract class BasePackage implements PackageInterface return $this->getPrettyName().' '.$this->getPrettyVersion(); } + /** + * {@inheritDoc} + */ + public function getFullPrettyVersion($truncate = true) + { + if (!$this->isDev() || !in_array($this->getSourceType(), array('hg', 'git'))) { + return $this->getPrettyVersion(); + } + + // if source reference is a sha1 hash -- truncate + if ($truncate && strlen($this->getSourceReference()) === 40) { + return $this->getPrettyVersion() . ' ' . substr($this->getSourceReference(), 0, 7); + } + + return $this->getPrettyVersion() . ' ' . $this->getSourceReference(); + } + public function __clone() { $this->repository = null; diff --git a/src/Composer/Package/PackageInterface.php b/src/Composer/Package/PackageInterface.php index a51274d5b..ba34619f1 100644 --- a/src/Composer/Package/PackageInterface.php +++ b/src/Composer/Package/PackageInterface.php @@ -192,6 +192,16 @@ interface PackageInterface */ public function getPrettyVersion(); + /** + * Returns the pretty version string plus a git or hg commit hash of this package + * + * @see getPrettyVersion + * + * @param bool $truncate If the source reference is a sha1 hash, truncate it + * @return string version + */ + public function getFullPrettyVersion($truncate = true); + /** * Returns the release date of the package * diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index e199838f2..ee49e81c8 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -71,16 +71,7 @@ class VersionParser public static function formatVersion(PackageInterface $package, $truncate = true) { - if (!$package->isDev() || !in_array($package->getSourceType(), array('hg', 'git'))) { - return $package->getPrettyVersion(); - } - - // if source reference is a sha1 hash -- truncate - if ($truncate && strlen($package->getSourceReference()) === 40) { - return $package->getPrettyVersion() . ' ' . substr($package->getSourceReference(), 0, 7); - } - - return $package->getPrettyVersion() . ' ' . $package->getSourceReference(); + return $package->getFullPrettyVersion($truncate); } /** diff --git a/tests/Composer/Test/Package/Version/VersionParserTest.php b/tests/Composer/Test/Package/Version/VersionParserTest.php index d8f351a27..18551570d 100644 --- a/tests/Composer/Test/Package/Version/VersionParserTest.php +++ b/tests/Composer/Test/Package/Version/VersionParserTest.php @@ -56,7 +56,7 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase $self = $this; $createPackage = function ($arr) use ($self) { - $package = $self->getMock('\Composer\Package\PackageInterface'); + $package = $self->getMockForAbstractClass('\Composer\Package\BasePackage', array(), '', false); $package->expects($self->once())->method('isDev')->will($self->returnValue(true)); $package->expects($self->once())->method('getSourceType')->will($self->returnValue('git')); $package->expects($self->once())->method('getPrettyVersion')->will($self->returnValue('PrettyVersion'));