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

Fix double error reporting if multiple nodes reference the same comment #102

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions src/TodoByTicketCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use staabm\PHPStanTodoBy\utils\CommentMatcher;
use staabm\PHPStanTodoBy\utils\ticket\TicketRuleConfiguration;

use function in_array;
use function trim;

/**
Expand All @@ -18,6 +19,9 @@ final class TodoByTicketCollector implements Collector
{
private TicketRuleConfiguration $configuration;

/** @var string[] */
private array $processedComments = [];

public function __construct(TicketRuleConfiguration $configuration)
{
$this->configuration = $configuration;
Expand All @@ -31,11 +35,18 @@ public function getNodeType(): string
public function processNode(Node $node, Scope $scope)
{
$it = CommentMatcher::matchComments($node, $this->createPattern());
$file = $scope->getFile();

$tickets = [];
foreach ($it as $comment => $matches) {
// use deprecated method for nikic/php-parser 4.x compat
$line = $comment->getLine();
$commentIdentifier = "$file:$line";

if (in_array($commentIdentifier, $this->processedComments, true)) {
continue;
}
$this->processedComments[] = $commentIdentifier;

/** @var array<int, array<array{0: string, 1: int}>> $matches */
foreach ($matches as $match) {
Expand Down
12 changes: 12 additions & 0 deletions tests/TodoByDateRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,16 @@ public function testBug64(): void
],
]);
}

public function testBug101(): void
{
$this->referenceTime = '2022-12-01';

$this->analyse([__DIR__ . '/data/bug101.php'], [
[
'Expired on 2020-01-01: do not forget about me.',
10,
],
]);
}
}
10 changes: 10 additions & 0 deletions tests/TodoByIssueUrlRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public static function provideErrors(): iterable
];
}

public function testBug101(): void
{
$this->analyse([__DIR__ . '/data/bug101.php'], [
[
'Comment should have been resolved with https://github.com/staabm/phpstan-todo-by/issues/47.',
5,
],
]);
}

public static function getAdditionalConfigFiles(): array
{
return [
Expand Down
10 changes: 10 additions & 0 deletions tests/TodoByPackageVersionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,14 @@ public function testInvalidVirtualPackage(): void
],
]);
}

public function testBug101(): void
{
$this->analyse([__DIR__ . '/data/bug101.php'], [
[
'"phpunit/phpunit" version requirement ">=5.3" satisfied.',
15,
],
]);
}
}
11 changes: 11 additions & 0 deletions tests/TodoByTicketRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,15 @@ public function testBug64(): void
],
]);
}

public function testBug101(): void
{
$this->analyse([__DIR__ . '/data/bug101.php'], [
[
'Should have been resolved in APP-123: please change me.',
3,
'See https://issue-tracker.com/APP-123',
],
]);
}
}
13 changes: 13 additions & 0 deletions tests/TodoByVersionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,17 @@ public function testBug64(): void
],
]);
}

public function testBug101(): void
{
$this->referenceVersion = '1.0';

$this->analyse([__DIR__ . '/data/bug101.php'], [
[
'Version requirement >=1.0 satisfied: fix me.',
21,
"Calculated reference version is '1.0.0.0'.\n\n See also:\n https://github.com/staabm/phpstan-todo-by#reference-version",
],
]);
}
}
27 changes: 27 additions & 0 deletions tests/data/bug101.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* TODO APP-123 please change me
* TODO https://github.com/staabm/phpstan-todo-by/issues/47
*/
#[SomeAttribute]
class Foo {}

/** TODO 2020-01-01 do not forget about me */
#[SomeAttribute]
class Bar {}

/**
* TODO phpunit/phpunit:5.3
*/
#[SomeAttribute]
class Baz {}

/**
* TODO: 1.0 fix me
*/
#[SomeAttribute]
class FooBar {}

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class SomeAttribute {}
Loading