|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Generics; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Internal\SprintfHelper; |
| 8 | +use PHPStan\Node\InClassNode; |
| 9 | +use PHPStan\Rules\Rule; |
| 10 | +use PHPStan\Rules\RuleErrorBuilder; |
| 11 | +use PHPStan\Type\FileTypeMapper; |
| 12 | +use PHPStan\Type\Generic\TemplateTypeScope; |
| 13 | +use PHPStan\Type\VerbosityLevel; |
| 14 | +use function array_keys; |
| 15 | +use function sprintf; |
| 16 | + |
| 17 | +/** |
| 18 | + * @implements Rule<InClassNode> |
| 19 | + */ |
| 20 | +class MethodTagTemplateTypeRule implements Rule |
| 21 | +{ |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + private FileTypeMapper $fileTypeMapper, |
| 25 | + private TemplateTypeCheck $templateTypeCheck, |
| 26 | + ) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + public function getNodeType(): string |
| 31 | + { |
| 32 | + return InClassNode::class; |
| 33 | + } |
| 34 | + |
| 35 | + public function processNode(Node $node, Scope $scope): array |
| 36 | + { |
| 37 | + $docComment = $node->getDocComment(); |
| 38 | + if ($docComment === null) { |
| 39 | + return []; |
| 40 | + } |
| 41 | + |
| 42 | + $classReflection = $node->getClassReflection(); |
| 43 | + $className = $classReflection->getDisplayName(); |
| 44 | + $resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( |
| 45 | + $scope->getFile(), |
| 46 | + $classReflection->getName(), |
| 47 | + $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, |
| 48 | + null, |
| 49 | + $docComment->getText(), |
| 50 | + ); |
| 51 | + |
| 52 | + $messages = []; |
| 53 | + $escapedClassName = SprintfHelper::escapeFormatString($className); |
| 54 | + $classTemplateTypes = $classReflection->getTemplateTypeMap()->getTypes(); |
| 55 | + |
| 56 | + foreach ($resolvedPhpDoc->getMethodTags() as $methodName => $methodTag) { |
| 57 | + $methodTemplateTags = $methodTag->getTemplateTags(); |
| 58 | + $escapedMethodName = SprintfHelper::escapeFormatString($methodName); |
| 59 | + |
| 60 | + $messages = array_merge($messages, $this->templateTypeCheck->check( |
| 61 | + $scope, |
| 62 | + $node, |
| 63 | + TemplateTypeScope::createWithMethod($className, $methodName), |
| 64 | + $methodTemplateTags, |
| 65 | + sprintf('PHPDoc tag @method template for method %s::%s() cannot have existing class %%s as its name.', $escapedClassName, $escapedMethodName), |
| 66 | + sprintf('PHPDoc tag @method template for method %s::%s() cannot have existing type alias %%s as its name.', $escapedClassName, $escapedMethodName), |
| 67 | + sprintf('PHPDoc tag @method template %%s for method %s::%s() has invalid bound type %%s.', $escapedClassName, $escapedMethodName), |
| 68 | + sprintf('PHPDoc tag @method template %%s for method %s::%s() with bound type %%s is not supported.', $escapedClassName, $escapedMethodName), |
| 69 | + )); |
| 70 | + |
| 71 | + foreach (array_keys($methodTemplateTags) as $name) { |
| 72 | + if (!isset($classTemplateTypes[$name])) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + $messages[] = RuleErrorBuilder::message(sprintf('PHPDoc tag @method template %s for method %s::%s() shadows @template %s for class %s.', $name, $className, $methodName, $classTemplateTypes[$name]->describe(VerbosityLevel::typeOnly()), $classReflection->getDisplayName(false)))->build(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return $messages; |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments