diff --git a/src/ReferenceVersionFinder.php b/src/ReferenceVersionFinder.php index 3dae5d9..c1a20a2 100644 --- a/src/ReferenceVersionFinder.php +++ b/src/ReferenceVersionFinder.php @@ -2,6 +2,7 @@ namespace staabm\PHPStanTodoBy; +use Version\Exception\InvalidVersionString; use Version\Version; final class ReferenceVersionFinder @@ -19,7 +20,20 @@ public function find(): string if (in_array($this->referenceVersion, ['nextMajor', 'nextMinor', 'nextPatch'], true)) { $latestTagVersion = $this->fetcher->fetchLatestTagVersion(); - $version = Version::fromString($latestTagVersion); + try { + $version = Version::fromString($latestTagVersion); + } catch (InvalidVersionString $originException) { + try { + $version = Version::fromString($latestTagVersion.'.0'); + } catch (InvalidVersionString $innerException) { + try { + $version = Version::fromString($latestTagVersion.'.0.0'); + } catch (InvalidVersionString $innerInnerException) { + throw $originException; + } + } + } + if ($this->referenceVersion === 'nextMajor') { return $version->incrementMajor()->toString(); } diff --git a/tests/ReferenceVersionFinderTest.php b/tests/ReferenceVersionFinderTest.php new file mode 100644 index 0000000..a9e7d28 --- /dev/null +++ b/tests/ReferenceVersionFinderTest.php @@ -0,0 +1,82 @@ +assertSame($expected, $finder->find()); + } + + /** + * @return iterable + */ + public static function provideData(): iterable + { + yield [ + '1.2.3', + '1.0.3', + '1.2.3', + ]; + + + yield [ + 'nextMajor', + '1.0.3', + '2.0.0', + ]; + yield [ + 'nextMinor', + '1.0.3', + '1.1.0', + ]; + yield [ + 'nextPatch', + '1.0.3', + '1.0.4', + ]; + + yield [ + 'nextMajor', + '1.0', + '2.0.0', + ]; + yield [ + 'nextMinor', + '1.0', + '1.1.0', + ]; + yield [ + 'nextPatch', + '1.0', + '1.0.1', + ]; + + yield [ + 'nextMajor', + '1', + '2.0.0', + ]; + yield [ + 'nextMinor', + '1', + '1.1.0', + ]; + yield [ + 'nextPatch', + '1', + '1.0.1', + ]; + } +} diff --git a/tests/utils/StaticTagFetcher.php b/tests/utils/StaticTagFetcher.php new file mode 100644 index 0000000..572a4cc --- /dev/null +++ b/tests/utils/StaticTagFetcher.php @@ -0,0 +1,19 @@ +tag = $tag; + } + public function fetchLatestTagVersion(): string + { + return $this->tag; + } +}