-
Notifications
You must be signed in to change notification settings - Fork 574
Report @inheritDoc when there is no PHPDoc to inherit from
#5583
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
kubawerlos
wants to merge
4
commits into
phpstan:2.1.x
Choose a base branch
from
6b7562617765726c6f73:invalid-inherit-doc-tag
base: 2.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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,111 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\PhpDoc; | ||
|
|
||
| use PhpParser\Node; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Node\InClassMethodNode; | ||
| use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode; | ||
| use PHPStan\PhpDocParser\Lexer\Lexer; | ||
| use PHPStan\PhpDocParser\Parser\PhpDocParser; | ||
| use PHPStan\PhpDocParser\Parser\TokenIterator; | ||
| use PHPStan\Rules\Methods\ParentMethodHelper; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use function preg_match; | ||
| use function sprintf; | ||
| use function strtolower; | ||
|
|
||
| /** | ||
| * @implements Rule<InClassMethodNode> | ||
| */ | ||
| final class InvalidInheritDocTagRule implements Rule | ||
| { | ||
|
|
||
| private const INLINE_INHERIT_DOC_REGEX = '~`[^`]*`(*SKIP)(*FAIL)|(?<![a-zA-Z0-9])\{@inheritDoc\b[^}]*\}~i'; | ||
|
|
||
| public function __construct( | ||
| private Lexer $phpDocLexer, | ||
| private PhpDocParser $phpDocParser, | ||
| private ParentMethodHelper $parentMethodHelper, | ||
| ) | ||
| { | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return InClassMethodNode::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| $docComment = $node->getOriginalNode()->getDocComment(); | ||
| if ($docComment === null) { | ||
| return []; | ||
| } | ||
|
|
||
| $tokens = new TokenIterator($this->phpDocLexer->tokenize($docComment->getText())); | ||
| $phpDocNode = $this->phpDocParser->parse($tokens); | ||
|
|
||
| $inheritDocTagName = null; | ||
| foreach ($phpDocNode->getTags() as $tag) { | ||
| if (strtolower($tag->name) !== '@inheritdoc') { | ||
| continue; | ||
| } | ||
|
|
||
| $inheritDocTagName = $tag->name; | ||
| break; | ||
| } | ||
|
|
||
| if ($inheritDocTagName === null) { | ||
| foreach ($phpDocNode->children as $child) { | ||
| if (!$child instanceof PhpDocTextNode) { | ||
| continue; | ||
| } | ||
|
|
||
| if (preg_match(self::INLINE_INHERIT_DOC_REGEX, $child->text, $matches) !== 1) { | ||
| continue; | ||
| } | ||
|
|
||
| $inheritDocTagName = $matches[0]; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if ($inheritDocTagName === null) { | ||
| return []; | ||
| } | ||
|
|
||
| $inheritanceClass = $scope->isInTrait() ? $scope->getTraitReflection() : $node->getClassReflection(); | ||
| $methodName = $node->getMethodReflection()->getName(); | ||
|
|
||
| $parentMethods = $this->parentMethodHelper->collectParentMethods($methodName, $inheritanceClass); | ||
|
|
||
| if ($parentMethods === []) { | ||
| return [ | ||
| RuleErrorBuilder::message(sprintf( | ||
| 'PHPDoc tag %s on method %s::%s() does not override or implement any other method.', | ||
| $inheritDocTagName, | ||
| $inheritanceClass->getDisplayName(), | ||
| $methodName, | ||
| ))->identifier('inheritDoc.noParent')->build(), | ||
| ]; | ||
| } | ||
|
|
||
| foreach ($parentMethods as [$parentMethod]) { | ||
| if ($parentMethod->getResolvedPhpDoc() !== null) { | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| return [ | ||
| RuleErrorBuilder::message(sprintf( | ||
| 'PHPDoc tag %s on method %s::%s() refers to a parent method that does not have a PHPDoc.', | ||
| $inheritDocTagName, | ||
| $inheritanceClass->getDisplayName(), | ||
| $methodName, | ||
| ))->identifier('inheritDoc.parentWithoutPhpDoc')->build(), | ||
| ]; | ||
| } | ||
|
|
||
| } | ||
72 changes: 72 additions & 0 deletions
72
tests/PHPStan/Rules/PhpDoc/InvalidInheritDocTagRuleTest.php
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,72 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\PhpDoc; | ||
|
|
||
| use PHPStan\PhpDocParser\Lexer\Lexer; | ||
| use PHPStan\PhpDocParser\Parser\PhpDocParser; | ||
| use PHPStan\Rules\Methods\ParentMethodHelper; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<InvalidInheritDocTagRule> | ||
| */ | ||
| class InvalidInheritDocTagRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return new InvalidInheritDocTagRule( | ||
| self::getContainer()->getByType(Lexer::class), | ||
| self::getContainer()->getByType(PhpDocParser::class), | ||
| self::getContainer()->getByType(ParentMethodHelper::class), | ||
| ); | ||
| } | ||
|
|
||
| public function testRule(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/invalid-inherit-doc-tag.php'], [ | ||
| [ | ||
| 'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\ChildWithInlineInheritDoc::methodWithoutPhpDoc() refers to a parent method that does not have a PHPDoc.', | ||
| 31, | ||
| ], | ||
| [ | ||
| 'PHPDoc tag @inheritdoc on method InvalidInheritDocTag\ChildWithBlockInheritDoc::methodWithoutPhpDoc() refers to a parent method that does not have a PHPDoc.', | ||
| 52, | ||
| ], | ||
| [ | ||
| 'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\ClassWithoutParent::orphanedInheritDoc() does not override or implement any other method.', | ||
| 73, | ||
| ], | ||
| [ | ||
| 'PHPDoc tag @inheritdoc on method InvalidInheritDocTag\ClassWithoutParent::orphanedBlockInheritDoc() does not override or implement any other method.', | ||
| 81, | ||
| ], | ||
| [ | ||
| 'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\ImplementsInterface::interfaceMethodWithoutPhpDoc() refers to a parent method that does not have a PHPDoc.', | ||
| 106, | ||
| ], | ||
| [ | ||
| 'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\UsesTraitWithoutPhpDoc::traitMethodWithoutPhpDoc() does not override or implement any other method.', | ||
| 216, | ||
| ], | ||
| [ | ||
| 'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\UsesTraitWithPhpDoc::traitMethodWithPhpDoc() does not override or implement any other method.', | ||
| 231, | ||
| ], | ||
| [ | ||
| 'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\IssueExampleChild::f() refers to a parent method that does not have a PHPDoc.', | ||
| 254, | ||
| ], | ||
| [ | ||
| 'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\ChildOfPrivateParentMethod::privateMethod() does not override or implement any other method.', | ||
| 280, | ||
| ], | ||
| [ | ||
| 'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\OrphanedInheritDocTrait::orphaned() does not override or implement any other method.', | ||
| 293, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
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.
Can't we do better ?
I know it'll be an edge case but you won't catch description like
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.
Like this?
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.
I'm not very good in regex but looking at the test called
InheritDocInsideBackticksi'm not sure we understood each other.My point was that, we might not want to consider that a method with
has an inheritDoc, cause it might be just a comment.
It wasn't related to backticks
(On the opposite,
might be considered as an inherit doc with an extra comment... ; I dunno)
I never use inheritDoc so I can't stay what we can find in codebase.
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.
Updated test.
@inheritDoc Barwill be tokenised as a tag with extra description.