-
Notifications
You must be signed in to change notification settings - Fork 516
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
Merged
+135
−0
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
25827d4
Playground: Implement PhpdocCommentRule
staabm 5a932e3
cs
staabm c503d73
ignore // and # comments
staabm 356eccd
cs
staabm 686f83c
improve wording
staabm aa70750
improve error identifier
staabm 6775b34
simplify
staabm 36d28d7
prevent false positive on phpDoc look-like comments
staabm f62b6a9
cs
staabm 7a96062
fix
staabm 923c1f8
Node\Stmt only
staabm 4eaf002
Update PhpdocCommentRule.php
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?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_starts_with; | ||
|
||
/** | ||
* @implements Rule<Node> | ||
*/ | ||
final class PhpdocCommentRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt::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|$)}') === null) { | ||
continue; | ||
} | ||
|
||
$errors[] = RuleErrorBuilder::message('Comment contains PHPDoc tag but does not start with /** prefix.') | ||
->identifier('phpstanPlayground.phpDoc') | ||
->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not synced with getNodeType