diff --git a/src/TodoByVersionRule.php b/src/TodoByVersionRule.php index 73f972e..6411cc5 100644 --- a/src/TodoByVersionRule.php +++ b/src/TodoByVersionRule.php @@ -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; @@ -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 @@ -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 { @@ -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."; } @@ -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] ); } diff --git a/src/utils/InvalidTagException.php b/src/utils/InvalidTagException.php new file mode 100644 index 0000000..6d5a50c --- /dev/null +++ b/src/utils/InvalidTagException.php @@ -0,0 +1,9 @@ +nextVersion($versionString, $originalVersionString); + } catch (InvalidVersionString $invalidVersionException) { + throw new InvalidTagException("Could not parse version from tag '{$originalVersionString}'", 0, $invalidVersionException); + } } $wasPreRelease = false; diff --git a/tests/TodoByVersionRuleNonSemverTagTest.php b/tests/TodoByVersionRuleNonSemverTagTest.php new file mode 100644 index 0000000..c50bd0f --- /dev/null +++ b/tests/TodoByVersionRuleNonSemverTagTest.php @@ -0,0 +1,39 @@ + + * @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, + ], + ]); + } +}