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