From 736ea800e39ab6ca3380840d09d63db0172ecc41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Sat, 3 Mar 2012 14:07:13 +0100 Subject: [PATCH] Added isDev method to the VersionParser class --- src/Composer/Package/AliasPackage.php | 5 +++-- src/Composer/Package/MemoryPackage.php | 4 +++- .../Package/Version/VersionParser.php | 11 +++++++++++ .../Package/Version/VersionParserTest.php | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/Composer/Package/AliasPackage.php b/src/Composer/Package/AliasPackage.php index cb40486aa..1b6481d8e 100644 --- a/src/Composer/Package/AliasPackage.php +++ b/src/Composer/Package/AliasPackage.php @@ -16,6 +16,7 @@ use Composer\Package\LinkConstraint\LinkConstraintInterface; use Composer\Package\LinkConstraint\VersionConstraint; use Composer\Repository\RepositoryInterface; use Composer\Repository\PlatformRepository; +use Composer\Package\Version\VersionParser; /** * @author Jordi Boggiano @@ -41,14 +42,14 @@ class AliasPackage extends BasePackage * @param string $version The version the alias must report * @param string $prettyVersion The alias's non-normalized version */ - public function __construct($aliasOf, $version, $prettyVersion) + public function __construct(PackageInterface $aliasOf, $version, $prettyVersion) { parent::__construct($aliasOf->getName()); $this->version = $version; $this->prettyVersion = $prettyVersion; $this->aliasOf = $aliasOf; - $this->dev = 'dev-' === substr($version, 0, 4) || '-dev' === substr($version, -4); + $this->dev = VersionParser::isDev($version); // replace self.version dependencies foreach (array('requires', 'recommends', 'suggests') as $type) { diff --git a/src/Composer/Package/MemoryPackage.php b/src/Composer/Package/MemoryPackage.php index e266f754c..2c3502502 100644 --- a/src/Composer/Package/MemoryPackage.php +++ b/src/Composer/Package/MemoryPackage.php @@ -12,6 +12,8 @@ namespace Composer\Package; +use Composer\Package\Version\VersionParser; + /** * A package with setters for all members to create it dynamically in memory * @@ -69,7 +71,7 @@ class MemoryPackage extends BasePackage $this->version = $version; $this->prettyVersion = $prettyVersion; - $this->dev = 'dev-' === substr($version, 0, 4) || '-dev' === substr($version, -4); + $this->dev = VersionParser::isDev($version); } /** diff --git a/src/Composer/Package/Version/VersionParser.php b/src/Composer/Package/Version/VersionParser.php index 19cd240a8..763929613 100644 --- a/src/Composer/Package/Version/VersionParser.php +++ b/src/Composer/Package/Version/VersionParser.php @@ -24,6 +24,17 @@ class VersionParser { private $modifierRegex = '[.-]?(?:(beta|RC|alpha|patch|pl|p)(?:[.-]?(\d+))?)?([.-]?dev)?'; + /** + * Checks if a version is dev or not + * + * @param string $version + * @return Boolean + */ + static public function isDev($version) + { + return 'dev-' === substr($version, 0, 4) || '-dev' === substr($version, -4); + } + /** * Normalizes a version string to be able to perform comparisons on it * diff --git a/tests/Composer/Test/Package/Version/VersionParserTest.php b/tests/Composer/Test/Package/Version/VersionParserTest.php index f45d0a0dc..c33bb2860 100644 --- a/tests/Composer/Test/Package/Version/VersionParserTest.php +++ b/tests/Composer/Test/Package/Version/VersionParserTest.php @@ -187,4 +187,23 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase 'invalid version' => array('1.0.0-meh'), ); } + + /** + * @dataProvider dataIsDev + */ + public function testIsDev($expected, $version) + { + $this->assertSame($expected, VersionParser::isDev($version)); + } + + public function dataIsDev() + { + return array( + array(false, '1.0'), + array(false, 'v2.0.*'), + array(false, '3.0dev'), + array(true, 'dev-master'), + array(true, '3.1.2-dev'), + ); + } }