|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\PhpDoc; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Node\InClassMethodNode; |
| 8 | +use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode; |
| 9 | +use PHPStan\PhpDocParser\Lexer\Lexer; |
| 10 | +use PHPStan\PhpDocParser\Parser\PhpDocParser; |
| 11 | +use PHPStan\PhpDocParser\Parser\TokenIterator; |
| 12 | +use PHPStan\Reflection\ClassReflection; |
| 13 | +use PHPStan\Rules\Rule; |
| 14 | +use PHPStan\Rules\RuleErrorBuilder; |
| 15 | +use function preg_match; |
| 16 | +use function sprintf; |
| 17 | +use function strtolower; |
| 18 | + |
| 19 | +/** |
| 20 | + * @implements Rule<InClassMethodNode> |
| 21 | + */ |
| 22 | +final class InvalidInheritDocTagRule implements Rule |
| 23 | +{ |
| 24 | + |
| 25 | + private const INLINE_INHERIT_DOC_REGEX = '~(?<![a-zA-Z0-9])\{@inheritDoc\b[^}]*\}~i'; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + private Lexer $phpDocLexer, |
| 29 | + private PhpDocParser $phpDocParser, |
| 30 | + ) |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + public function getNodeType(): string |
| 35 | + { |
| 36 | + return InClassMethodNode::class; |
| 37 | + } |
| 38 | + |
| 39 | + public function processNode(Node $node, Scope $scope): array |
| 40 | + { |
| 41 | + $docComment = $node->getOriginalNode()->getDocComment(); |
| 42 | + if ($docComment === null) { |
| 43 | + return []; |
| 44 | + } |
| 45 | + |
| 46 | + $tokens = new TokenIterator($this->phpDocLexer->tokenize($docComment->getText())); |
| 47 | + $phpDocNode = $this->phpDocParser->parse($tokens); |
| 48 | + |
| 49 | + $inheritDocTagName = null; |
| 50 | + foreach ($phpDocNode->getTags() as $tag) { |
| 51 | + if (strtolower($tag->name) !== '@inheritdoc') { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + $inheritDocTagName = $tag->name; |
| 56 | + break; |
| 57 | + } |
| 58 | + |
| 59 | + if ($inheritDocTagName === null) { |
| 60 | + foreach ($phpDocNode->children as $child) { |
| 61 | + if (!$child instanceof PhpDocTextNode) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + if (preg_match(self::INLINE_INHERIT_DOC_REGEX, $child->text, $matches) !== 1) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + $inheritDocTagName = $matches[0]; |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if ($inheritDocTagName === null) { |
| 75 | + return []; |
| 76 | + } |
| 77 | + |
| 78 | + $inheritanceClass = $scope->isInTrait() ? $scope->getTraitReflection() : $node->getClassReflection(); |
| 79 | + $methodName = $node->getMethodReflection()->getName(); |
| 80 | + |
| 81 | + if ($this->hasInheritablePhpDoc($inheritanceClass, $methodName)) { |
| 82 | + return []; |
| 83 | + } |
| 84 | + |
| 85 | + return [ |
| 86 | + RuleErrorBuilder::message(sprintf( |
| 87 | + 'PHPDoc tag %s on method %s::%s() refers to a parent method that does not have a PHPDoc.', |
| 88 | + $inheritDocTagName, |
| 89 | + $inheritanceClass->getDisplayName(), |
| 90 | + $methodName, |
| 91 | + ))->identifier('phpDoc.invalidInheritDoc')->build(), |
| 92 | + ]; |
| 93 | + } |
| 94 | + |
| 95 | + private function hasInheritablePhpDoc(ClassReflection $classReflection, string $methodName): bool |
| 96 | + { |
| 97 | + $parent = $classReflection->getParentClass(); |
| 98 | + if ($parent !== null && $this->parentHasPhpDocForMethod($parent, $methodName)) { |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + foreach ($classReflection->getImmediateInterfaces() as $interface) { |
| 103 | + if ($this->parentHasPhpDocForMethod($interface, $methodName)) { |
| 104 | + return true; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + foreach ($classReflection->getTraits() as $trait) { |
| 109 | + if ($this->parentHasPhpDocForMethod($trait, $methodName)) { |
| 110 | + return true; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + private function parentHasPhpDocForMethod(ClassReflection $parent, string $methodName): bool |
| 118 | + { |
| 119 | + if (!$parent->hasNativeMethod($methodName)) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + $parentMethod = $parent->getNativeMethod($methodName); |
| 124 | + if ($parentMethod->isPrivate()) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + return $parentMethod->getResolvedPhpDoc() !== null; |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments