From 3fc7f095664ed11fa4168778cf576784200cd416 Mon Sep 17 00:00:00 2001 From: Sebastiaan Stok Date: Fri, 24 Nov 2017 11:55:03 +0100 Subject: [PATCH] Allow to increment with next When stable increment minor, for unstable with metaver increment metaver instead. --- README.md | 2 ++ src/Version.php | 17 +++++++++++++++-- tests/VersionTest.php | 7 +++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a3faa2c..f280f52 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ $version = Version::fromString('v1.3.2'); $newVersion = $version->increase('major'); // v2.0.0 $newVersion = $version->increase('minor'); // v1.4.0 +$newVersion = $version->increase('next'); // v1.4.0 $newVersion = $version->increase('patch'); // v1.3.3 $newVersion = $version->increase('stable'); // v1.4.0 @@ -54,6 +55,7 @@ $version = Version::fromString('v1.4.0-BETA1'); $newVersion = $version->increase('beta'); // v1.4.0-BETA2 $newVersion = $version->increase('rc'); // v1.4.0-RC1 $newVersion = $version->increase('major'); // v1.4.0 +$newVersion = $version->increase('next'); // v1.4.0-BETA2 $newVersion = $version->increase('stable'); // v1.4.0 // Version validation diff --git a/src/Version.php b/src/Version.php index 9784fdf..db8225b 100644 --- a/src/Version.php +++ b/src/Version.php @@ -202,11 +202,15 @@ public function increase(string $stability): Version case 'stable': return $this->increaseStable(); + case 'next': + return $this->increaseNext(); + default: throw new \InvalidArgumentException( sprintf( - 'Unknown stability "%s", accepts "%s" '.$stability, - implode('", "', ['alpha', 'beta', 'rc', 'stable', 'major', 'minor', 'patch']) + 'Unknown stability "%s", accepts "%s".', + $stability, + implode('", "', ['alpha', 'beta', 'rc', 'stable', 'major', 'next', 'minor', 'patch']) ) ); } @@ -233,4 +237,13 @@ private function increaseStable(): Version return new self($this->major, $this->minor + 1, 0, 3); } + + private function increaseNext(): Version + { + if ($this->major > 0 && $this->stability < 3) { + return new self($this->major, $this->minor, $this->patch, $this->stability, $this->metaver + 1); + } + + return new self($this->major, $this->minor + 1, 0, 3); + } } diff --git a/tests/VersionTest.php b/tests/VersionTest.php index a2c2185..6b5cd68 100644 --- a/tests/VersionTest.php +++ b/tests/VersionTest.php @@ -156,6 +156,13 @@ public function provideExpectedIncreasedVersion(): array 'major from 1.0' => ['1.0.0', '2.0.0', 'major'], 'major from 2.0-beta' => ['2.0.0-beta1', '2.0.0', 'major'], + // Next + 'new next from 0.1.0' => ['0.1.0', '0.2.0', 'next'], + 'new next from 0.1.1' => ['0.1.1', '0.2.0', 'next'], + 'new next from alpha' => ['1.0.0-alpha6', '1.0.0-alpha7', 'next'], + 'new next from beta' => ['1.0.0-beta1', '1.0.0-beta2', 'next'], + 'new next from current stable' => ['1.0.0', '1.1.0', 'next'], + // Alpha 'next alpha' => ['1.0.0-alpha1', '1.0.0-alpha2', 'alpha'], 'new alpha' => ['1.0.0', '1.1.0-alpha1', 'alpha'],