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 8 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
53 changes: 53 additions & 0 deletions src/Rules/Playground/PhpdocCommentRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Playground;

use Nette\Utils\Strings;
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) {
foreach (['/**', '//', '#'] as $startTag) {
if (str_starts_with($comment->getText(), $startTag)) {
continue 2;
}
}

if (!Strings::match($comment->getText(), '{(\s|^)@\w+(\s|$)}')) {

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Only booleans are allowed in a negated boolean, array|null given.

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Only booleans are allowed in a negated boolean, array|null given.

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Only booleans are allowed in a negated boolean, array|null given.

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Only booleans are allowed in a negated boolean, array|null given.

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Only booleans are allowed in a negated boolean, array|null given.

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Only booleans are allowed in a negated boolean, array|null given.

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Only booleans are allowed in a negated boolean, array|null given.

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Only booleans are allowed in a negated boolean, array|null given.

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Only booleans are allowed in a negated boolean, array|null given.

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Only booleans are allowed in a negated boolean, array|null given.

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Only booleans are allowed in a negated boolean, array|null given.

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Only booleans are allowed in a negated boolean, array|null given.

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Only booleans are allowed in a negated boolean, array|null given.

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Only booleans are allowed in a negated boolean, array|null given.

Check failure on line 41 in src/Rules/Playground/PhpdocCommentRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Only booleans are allowed in a negated boolean, array|null given.
continue;
}

$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,
],
]);
}

}
49 changes: 49 additions & 0 deletions tests/PHPStan/Rules/Playground/data/comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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

/*
* comments which look like phpdoc should be ignored
*
* x@x.cz
* 10 amps @ 1 volt
*/
public function ignoreComments(): FooInterface
{
return $this->foo;
}
}
Loading