Skip to content

Commit

Permalink
Fix laziness of TagFetcher (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Dec 19, 2023
1 parent aec7f7a commit c648493
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/GitTagFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace staabm\PHPStanTodoBy;

final class GitTagFetcher {
final class GitTagFetcher implements TagFetcher {
// fetch version of the latest created git tag
public function fetchLatestTagVersion(): string
{
Expand Down
4 changes: 2 additions & 2 deletions src/ReferenceVersionFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use Version\Version;

final class ReferenceVersionFinder {
private GitTagFetcher $fetcher;
private TagFetcher $fetcher;
private string $referenceVersion;

public function __construct(string $referenceVersion, GitTagFetcher $fetcher) {
public function __construct(string $referenceVersion, TagFetcher $fetcher) {
$this->referenceVersion = $referenceVersion;
$this->fetcher = $fetcher;
}
Expand Down
8 changes: 8 additions & 0 deletions src/TagFetcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace staabm\PHPStanTodoBy;

interface TagFetcher
{
public function fetchLatestTagVersion(): string;
}
5 changes: 4 additions & 1 deletion src/TodoByDateRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ public function processNode(Node $node, Scope $scope): array
* PREG_OFFSET_CAPTURE: Track where each "todo" comment starts within the whole comment text.
* PREG_SET_ORDER: Make each value of $matches be structured the same as if from preg_match().
*/
if (preg_match_all(self::PATTERN, $text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER) === FALSE) {
if (
preg_match_all(self::PATTERN, $text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER) === FALSE
|| count($matches) === 0
) {
continue;
}

Expand Down
5 changes: 4 additions & 1 deletion src/TodoByVersionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ public function processNode(Node $node, Scope $scope): array
* PREG_OFFSET_CAPTURE: Track where each "todo" comment starts within the whole comment text.
* PREG_SET_ORDER: Make each value of $matches be structured the same as if from preg_match().
*/
if (preg_match_all(self::PATTERN, $text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER) === FALSE) {
if (
preg_match_all(self::PATTERN, $text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER) === FALSE
|| count($matches) === 0
) {
continue;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/AlwaysThrowingTagFetcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace staabm\PHPStanTodoBy\Tests;

use staabm\PHPStanTodoBy\TagFetcher;

final class AlwaysThrowingTagFetcher implements TagFetcher {
public function fetchLatestTagVersion(): string
{
throw new \RuntimeException('Could not determine latest git tag');
}
}
28 changes: 28 additions & 0 deletions tests/TodoByVersionRuleAlwaysThrowingFetchterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace staabm\PHPStanTodoBy\Tests;

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

/**
* @extends RuleTestCase<TodoByVersionRule>
*/
final class TodoByVersionRuleAlwaysThrowingFetchterTest extends RuleTestCase
{
private string $referenceVersion;
protected function getRule(): Rule
{
return new TodoByVersionRule(true, new ReferenceVersionFinder($this->referenceVersion, new AlwaysThrowingTagFetcher()));
}

public function testRule(): void
{
$this->referenceVersion = "nextMajor";

$this->analyse([__DIR__ . '/data/regularComments.php'], []);
}

}
13 changes: 13 additions & 0 deletions tests/data/regularComments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace RegularComments;

// a file comment

/**
* @return void
*/
function doFoo() {
// inner function comment
$x = 1;
}

0 comments on commit c648493

Please sign in to comment.