|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Symplify\PHPStanRules\Rules; |
| 6 | + |
| 7 | +use Nette\Utils\Strings; |
| 8 | +use PhpParser\Node; |
| 9 | +use PhpParser\Node\Stmt\Class_; |
| 10 | +use PHPStan\Analyser\Scope; |
| 11 | +use PHPStan\Rules\Rule; |
| 12 | +use PHPStan\Rules\RuleError; |
| 13 | +use PHPStan\Rules\RuleErrorBuilder; |
| 14 | +use Symplify\PHPStanRules\Enum\RuleIdentifier; |
| 15 | + |
| 16 | +/** |
| 17 | + * @implements Rule<Class_> |
| 18 | + * |
| 19 | + * @see \Symplify\PHPStanRules\Tests\Rules\NoMissnamedDocTagRule\NoMissnamedDocTagRuleTest |
| 20 | + */ |
| 21 | +final class NoMissnamedDocTagRule implements Rule |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @api used in tests |
| 25 | + */ |
| 26 | + public const CONSTANT_ERROR_MESSAGE = 'Constant doc comment tag must be @var, "%s" given'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @api used in tests |
| 30 | + */ |
| 31 | + public const PROPERTY_ERROR_MESSAGE = 'Property doc comment tag must be @var, "%s" given'; |
| 32 | + |
| 33 | + /** |
| 34 | + * @api used in tests |
| 35 | + */ |
| 36 | + public const METHOD_ERROR_MESSAGE = 'Method doc comment tag must be @param or @return, "%s" given'; |
| 37 | + |
| 38 | + public function getNodeType(): string |
| 39 | + { |
| 40 | + return Class_::class; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param Class_ $node |
| 45 | + * @return array<RuleError> |
| 46 | + */ |
| 47 | + public function processNode(Node $node, Scope $scope): array |
| 48 | + { |
| 49 | + $ruleErrors = []; |
| 50 | + |
| 51 | + foreach ($node->getMethods() as $classMethod) { |
| 52 | + // match "@return" and "@param" tags |
| 53 | + if ($classMethod->getDocComment() === null) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + $matches = Strings::match($classMethod->getDocComment()->getText(), '#(@var)\b#mi'); |
| 58 | + if ($matches === null) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + $ruleErrors[] = RuleErrorBuilder::message(sprintf(self::METHOD_ERROR_MESSAGE, $matches[1])) |
| 63 | + ->identifier(RuleIdentifier::NO_MISSNAMED_DOC_TAG) |
| 64 | + ->line($classMethod->getStartLine()) |
| 65 | + ->build(); |
| 66 | + } |
| 67 | + |
| 68 | + foreach ($node->getProperties() as $property) { |
| 69 | + // match "@return" and "@param" tags |
| 70 | + if ($property->getDocComment() === null) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + $matches = Strings::match($property->getDocComment()->getText(), '#(@param|@return)\b#mi'); |
| 75 | + if ($matches === null) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + $ruleErrors[] = RuleErrorBuilder::message(sprintf(self::PROPERTY_ERROR_MESSAGE, $matches[1])) |
| 80 | + ->identifier(RuleIdentifier::NO_MISSNAMED_DOC_TAG) |
| 81 | + ->line($property->getStartLine()) |
| 82 | + ->build(); |
| 83 | + } |
| 84 | + |
| 85 | + foreach ($node->getConstants() as $classConst) { |
| 86 | + if ($classConst->getDocComment() === null) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + $matches = Strings::match($classConst->getDocComment()->getText(), '#(@param|@return)\b#mi'); |
| 91 | + if ($matches === null) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + $ruleErrors[] = RuleErrorBuilder::message(sprintf(self::CONSTANT_ERROR_MESSAGE, $matches[1])) |
| 96 | + ->identifier(RuleIdentifier::NO_MISSNAMED_DOC_TAG) |
| 97 | + ->line($classConst->getStartLine()) |
| 98 | + ->build(); |
| 99 | + } |
| 100 | + |
| 101 | + return $ruleErrors; |
| 102 | + } |
| 103 | +} |
0 commit comments