Skip to content

Commit c033db6

Browse files
committed
Playground: Implement NonPhpdocCommentRule
1 parent 42eac28 commit c033db6

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

issue-bot/playground.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ rules:
33
- PHPStan\Rules\Playground\MethodNeverRule
44
- PHPStan\Rules\Playground\NotAnalysedTraitRule
55
- PHPStan\Rules\Playground\NoPhpCodeRule
6+
- PHPStan\Rules\Playground\PhpdocCommentRule
67

78
conditionalTags:
89
PHPStan\Rules\Playground\StaticVarWithoutTypeRule:
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Playground;
4+
5+
use PhpParser\Comment;
6+
use PhpParser\Node;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Node\VirtualNode;
9+
use PHPStan\Rules\Rule;
10+
use PHPStan\Rules\RuleErrorBuilder;
11+
use PHPStan\ShouldNotHappenException;
12+
use function str_contains;
13+
use function str_starts_with;
14+
15+
/**
16+
* @implements Rule<Node>
17+
*/
18+
final class PhpdocCommentRule implements Rule
19+
{
20+
21+
public function getNodeType(): string
22+
{
23+
return Node::class;
24+
}
25+
26+
public function processNode(Node $node, Scope $scope): array
27+
{
28+
if ($node instanceof VirtualNode) {
29+
return [];
30+
}
31+
32+
$comments = $node->getAttribute('comments', []);
33+
34+
$errors = [];
35+
foreach ($comments as $comment) {
36+
if (!$comment instanceof Comment) {
37+
throw new ShouldNotHappenException();
38+
}
39+
if (!str_contains($comment->getText(), '@')) {
40+
continue;
41+
}
42+
43+
if (str_starts_with($comment->getText(), '/**')) {
44+
continue;
45+
}
46+
47+
$errors[] = RuleErrorBuilder::message('Comment contains phpdoc-tag but does not start with /** tag.')
48+
->identifier('phpstanPlayground.noPhpdoc')
49+
->build();
50+
}
51+
52+
return $errors;
53+
}
54+
55+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Playground;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
8+
/**
9+
* @extends RuleTestCase<PhpdocCommentRule>
10+
*/
11+
class PhpdocCommentRuleTest extends RuleTestCase
12+
{
13+
14+
protected function getRule(): Rule
15+
{
16+
return new PhpdocCommentRule();
17+
}
18+
19+
public function testRule(): void
20+
{
21+
$this->analyse([__DIR__ . '/data/comments.php'], [
22+
[
23+
'Comment contains phpdoc-tag but does not start with /** tag.',
24+
13,
25+
],
26+
[
27+
'Comment contains phpdoc-tag but does not start with /** tag.',
28+
23,
29+
],
30+
]);
31+
}
32+
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace CommentTypes;
4+
5+
/**
6+
* @template T of FooInterface
7+
*/
8+
class Bar
9+
{
10+
/*
11+
* @var T $foo
12+
*/
13+
protected FooInterface $foo;
14+
15+
/**
16+
* @param T $foo
17+
*/
18+
public function __construct(FooInterface $foo) { $this->foo = $foo; }
19+
20+
/*
21+
* @return T
22+
*/
23+
public function getFoo(): FooInterface
24+
{
25+
return $this->foo;
26+
}
27+
28+
/*
29+
* some method
30+
*/
31+
public function getBar(): FooInterface
32+
{
33+
return $this->foo;
34+
}
35+
}

0 commit comments

Comments
 (0)