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