Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support less strict versioning schemes #21

Merged
merged 6 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/ReferenceVersionFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace staabm\PHPStanTodoBy;

use Version\Exception\InvalidVersionString;
use Version\Version;

final class ReferenceVersionFinder
Expand All @@ -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();
}
Expand Down
82 changes: 82 additions & 0 deletions tests/ReferenceVersionFinderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace staabm\PHPStanTodoBy\Tests;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\TestCase;
use staabm\PHPStanTodoBy\ReferenceVersionFinder;
use staabm\PHPStanTodoBy\TodoByDateRule;

final class ReferenceVersionFinderTest extends TestCase
{
/**
* @dataProvider provideData
*/
public function testReferenceFinder(string $refernceVersion, string $staticTag, string $expected): void
{
$finder = new ReferenceVersionFinder($refernceVersion, new StaticTagFetcher($staticTag));
$this->assertSame($expected, $finder->find());
}

/**
* @return iterable<array{string, string, string}>
*/
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',
];
}
}
19 changes: 19 additions & 0 deletions tests/utils/StaticTagFetcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace staabm\PHPStanTodoBy\Tests;

use staabm\PHPStanTodoBy\TagFetcher;

final class StaticTagFetcher implements TagFetcher
{
private string $tag;

public function __construct(string $tag)
{
$this->tag = $tag;
}
public function fetchLatestTagVersion(): string
{
return $this->tag;
}
}