Skip to content
Open
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
13 changes: 10 additions & 3 deletions src/TodoByVersionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Rules\RuleErrorBuilder;
use staabm\PHPStanTodoBy\utils\CommentMatcher;
use staabm\PHPStanTodoBy\utils\ExpiredCommentErrorBuilder;
use staabm\PHPStanTodoBy\utils\InvalidTagException;
use staabm\PHPStanTodoBy\utils\LatestTagNotFoundException;
use staabm\PHPStanTodoBy\utils\ReferenceVersionFinder;
use UnexpectedValueException;
Expand Down Expand Up @@ -82,6 +83,12 @@ public function processNode(Node $node, Scope $scope): array
->identifier(ExpiredCommentErrorBuilder::ERROR_IDENTIFIER_PREFIX.self::ERROR_IDENTIFIER)
->build(),
];
} catch (InvalidTagException $e) {
return [
RuleErrorBuilder::message($e->getMessage())
->identifier(ExpiredCommentErrorBuilder::ERROR_IDENTIFIER_PREFIX.self::ERROR_IDENTIFIER)
->build(),
];
}
$provided = $versionParser->parseConstraints(
$referenceVersion
Expand All @@ -94,7 +101,7 @@ public function processNode(Node $node, Scope $scope): array

// assume a min version constraint, when the comment does not specify a comparator
if (null === $this->getVersionComparator($version)) {
$version = '>='. $version;
$version = '>=' . $version;
}

try {
Expand All @@ -118,7 +125,7 @@ public function processNode(Node $node, Scope $scope): array

// If there is further text, append it.
if ('' !== $todoText) {
$errorMessage = "Version requirement {$version} satisfied: ". rtrim($todoText, '.') .'.';
$errorMessage = "Version requirement {$version} satisfied: " . rtrim($todoText, '.') . '.';
} else {
$errorMessage = "Version requirement {$version} satisfied.";
}
Expand All @@ -128,7 +135,7 @@ public function processNode(Node $node, Scope $scope): array
$comment->getStartLine(),
$errorMessage,
self::ERROR_IDENTIFIER,
"Calculated reference version is '". $referenceVersion ."'.\n\n See also:\n https://github.com/staabm/phpstan-todo-by#reference-version",
"Calculated reference version is '" . $referenceVersion . "'.\n\n See also:\n https://github.com/staabm/phpstan-todo-by#reference-version",
$match[0][1]
);
}
Expand Down
9 changes: 9 additions & 0 deletions src/utils/InvalidTagException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace staabm\PHPStanTodoBy\utils;

use RuntimeException;

final class InvalidTagException extends RuntimeException
{
}
10 changes: 8 additions & 2 deletions src/utils/ReferenceVersionFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ public function find(?string $workingDirectory): string
}

// adopted from https://github.com/WyriHaximus/github-action-next-semvers/blob/master/src/Next.php
private function nextVersion(string $versionString): string
private function nextVersion(string $versionString, ?string $originalVersionString = null): string
{
$originalVersionString ??= $versionString;

try {
$version = Version::fromString($versionString);
} catch (InvalidVersionString $invalidVersionException) {
Expand All @@ -54,7 +56,11 @@ private function nextVersion(string $versionString): string
$versionString .= '.0';
}

return self::nextVersion($versionString);
try {
return $this->nextVersion($versionString, $originalVersionString);
} catch (InvalidVersionString $invalidVersionException) {
throw new InvalidTagException("Could not parse version from tag '{$originalVersionString}'", 0, $invalidVersionException);
}
}

$wasPreRelease = false;
Expand Down
39 changes: 39 additions & 0 deletions tests/TodoByVersionRuleNonSemverTagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace staabm\PHPStanTodoBy\Tests;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use staabm\PHPStanTodoBy\TodoByVersionRule;
use staabm\PHPStanTodoBy\utils\ExpiredCommentErrorBuilder;
use staabm\PHPStanTodoBy\utils\ReferenceVersionFinder;

/**
* @extends RuleTestCase<TodoByVersionRule>
* @internal
*/
final class TodoByVersionRuleNonSemverTagTest extends RuleTestCase
{
private string $referenceVersion;

protected function getRule(): Rule
{
return new TodoByVersionRule(
true,
new ReferenceVersionFinder($this->referenceVersion, new StaticTagFetcher('testing')),
new ExpiredCommentErrorBuilder(true)
);
}

public function testInvalidTagIsParsed(): void
{
$this->referenceVersion = 'nextMajor';

$this->analyse([__DIR__ . '/data/tagNotFound.php'], [
[
"Could not parse version from tag 'testing'",
5,
],
]);
}
}