diff --git a/issue-bot/playground.neon b/issue-bot/playground.neon index d4072a4170..a252e3bac8 100644 --- a/issue-bot/playground.neon +++ b/issue-bot/playground.neon @@ -3,6 +3,7 @@ rules: - PHPStan\Rules\Playground\MethodNeverRule - PHPStan\Rules\Playground\NotAnalysedTraitRule - PHPStan\Rules\Playground\NoPhpCodeRule + - PHPStan\Rules\Playground\PhpdocCommentRule conditionalTags: PHPStan\Rules\Playground\StaticVarWithoutTypeRule: diff --git a/src/Rules/Playground/PhpdocCommentRule.php b/src/Rules/Playground/PhpdocCommentRule.php new file mode 100644 index 0000000000..73d0536a9d --- /dev/null +++ b/src/Rules/Playground/PhpdocCommentRule.php @@ -0,0 +1,53 @@ + + */ +final class PhpdocCommentRule implements Rule +{ + + public function getNodeType(): string + { + return Node::class; + } + + public function processNode(Node $node, Scope $scope): array + { + if ($node instanceof VirtualNode) { + return []; + } + + $comments = $node->getComments(); + + $errors = []; + foreach ($comments as $comment) { + foreach (['/**', '//', '#'] as $startTag) { + if (str_starts_with($comment->getText(), $startTag)) { + continue 2; + } + } + + if (!Strings::match($comment->getText(), '{(\s|^)@\w+(\s|$)}')) { + continue; + } + + $errors[] = RuleErrorBuilder::message('Comment contains PHPDoc tag but does not start with /** prefix.') + ->identifier('phpstanPlayground.phpDoc') + ->build(); + } + + return $errors; + } + +} diff --git a/tests/PHPStan/Rules/Playground/PhpdocCommentRuleTest.php b/tests/PHPStan/Rules/Playground/PhpdocCommentRuleTest.php new file mode 100644 index 0000000000..7642b2356f --- /dev/null +++ b/tests/PHPStan/Rules/Playground/PhpdocCommentRuleTest.php @@ -0,0 +1,33 @@ + + */ +class PhpdocCommentRuleTest extends RuleTestCase +{ + + protected function getRule(): Rule + { + return new PhpdocCommentRule(); + } + + public function testRule(): void + { + $this->analyse([__DIR__ . '/data/comments.php'], [ + [ + 'Comment contains PHPDoc tag but does not start with /** prefix.', + 13, + ], + [ + 'Comment contains PHPDoc tag but does not start with /** prefix.', + 23, + ], + ]); + } + +} diff --git a/tests/PHPStan/Rules/Playground/data/comments.php b/tests/PHPStan/Rules/Playground/data/comments.php new file mode 100644 index 0000000000..83f67ba589 --- /dev/null +++ b/tests/PHPStan/Rules/Playground/data/comments.php @@ -0,0 +1,49 @@ +foo = $foo; } + + /* + * @return T + */ + public function getFoo(): FooInterface + { + return $this->foo; + } + + /* + * some method + */ + public function getBar(): FooInterface + { + return $this->foo; + } + + // this should not error: @var + # this should not error: @var + + /* + * comments which look like phpdoc should be ignored + * + * x@x.cz + * 10 amps @ 1 volt + */ + public function ignoreComments(): FooInterface + { + return $this->foo; + } +}