Skip to content

Commit 36d28d7

Browse files
committed
prevent false positive on phpDoc look-like comments
1 parent 6775b34 commit 36d28d7

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Rules/Playground/PhpdocCommentRule.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Playground;
44

5+
use Nette\Utils\Strings;
56
use PhpParser\Node;
67
use PHPStan\Analyser\Scope;
78
use PHPStan\Node\VirtualNode;
@@ -31,16 +32,16 @@ public function processNode(Node $node, Scope $scope): array
3132

3233
$errors = [];
3334
foreach ($comments as $comment) {
34-
if (!str_contains($comment->getText(), '@')) {
35-
continue;
36-
}
37-
3835
foreach (['/**', '//', '#'] as $startTag) {
3936
if (str_starts_with($comment->getText(), $startTag)) {
4037
continue 2;
4138
}
4239
}
4340

41+
if (!Strings::match($comment->getText(), '{(\s|^)@\w+(\s|$)}')) {
42+
continue;
43+
}
44+
4445
$errors[] = RuleErrorBuilder::message('Comment contains PHPDoc tag but does not start with /** prefix.')
4546
->identifier('phpstanPlayground.phpDoc')
4647
->build();

tests/PHPStan/Rules/Playground/data/comments.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,15 @@ public function getBar(): FooInterface
3535

3636
// this should not error: @var
3737
# this should not error: @var
38+
39+
/*
40+
* comments which look like phpdoc should be ignored
41+
*
42+
43+
* 10 amps @ 1 volt
44+
*/
45+
public function ignoreComments(): FooInterface
46+
{
47+
return $this->foo;
48+
}
3849
}

0 commit comments

Comments
 (0)