Skip to content

Playground: Implement PhpdocCommentRule #4074

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

Open
wants to merge 7 commits into
base: 2.1.x
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions issue-bot/playground.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
52 changes: 52 additions & 0 deletions src/Rules/Playground/PhpdocCommentRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Playground;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\VirtualNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function str_contains;
use function str_starts_with;

/**
* @implements Rule<Node>
*/
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) {
if (!str_contains($comment->getText(), '@')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need something more sophiscated, like "contains a real phpdoc tag"?
if so, how to find out?

Copy link

@mvorisek mvorisek Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(\s|^)@\w+(\s|$) would be good to exclude these:

continue;
}

foreach (['/**', '//', '#'] as $startTag) {
if (str_starts_with($comment->getText(), $startTag)) {
continue 2;
}
}

$errors[] = RuleErrorBuilder::message('Comment contains PHPDoc tag but does not start with /** prefix.')
->identifier('phpstanPlayground.phpDoc')
->build();
}

return $errors;
}

}
33 changes: 33 additions & 0 deletions tests/PHPStan/Rules/Playground/PhpdocCommentRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Playground;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<PhpdocCommentRule>
*/
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,
],
]);
}

}
38 changes: 38 additions & 0 deletions tests/PHPStan/Rules/Playground/data/comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace CommentTypes;

/**
* @template T of FooInterface
*/
class Bar
{
/*
* @var T $foo
*/
protected FooInterface $foo;

/**
* @param T $foo
*/
public function __construct(FooInterface $foo) { $this->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
}
Loading