-
Notifications
You must be signed in to change notification settings - Fork 426
/
AnnotationsMethodsClassReflectionExtension.php
125 lines (104 loc) · 3.67 KB
/
AnnotationsMethodsClassReflectionExtension.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php declare(strict_types = 1);
namespace PHPStan\Reflection\Annotations;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
use PHPStan\Type\Generic\TemplateTypeHelper;
use function count;
class AnnotationsMethodsClassReflectionExtension implements MethodsClassReflectionExtension
{
/** @var ExtendedMethodReflection[][] */
private array $methods = [];
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
if (!isset($this->methods[$classReflection->getCacheKey()][$methodName])) {
$method = $this->findClassReflectionWithMethod($classReflection, $classReflection, $methodName);
if ($method === null) {
return false;
}
$this->methods[$classReflection->getCacheKey()][$methodName] = $method;
}
return isset($this->methods[$classReflection->getCacheKey()][$methodName]);
}
/**
* @return ExtendedMethodReflection
*/
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
{
return $this->methods[$classReflection->getCacheKey()][$methodName];
}
private function findClassReflectionWithMethod(
ClassReflection $classReflection,
ClassReflection $declaringClass,
string $methodName,
): ?MethodReflection
{
$methodTags = $classReflection->getMethodTags();
if (isset($methodTags[$methodName])) {
$parameters = [];
foreach ($methodTags[$methodName]->getParameters() as $parameterName => $parameterTag) {
$parameters[] = new AnnotationsMethodParameterReflection(
$parameterName,
$parameterTag->getType(),
$parameterTag->passedByReference(),
$parameterTag->isOptional(),
$parameterTag->isVariadic(),
$parameterTag->getDefaultValue(),
);
}
return new AnnotationMethodReflection(
$methodName,
$declaringClass,
TemplateTypeHelper::resolveTemplateTypes(
$methodTags[$methodName]->getReturnType(),
$classReflection->getActiveTemplateTypeMap(),
),
$parameters,
$methodTags[$methodName]->isStatic(),
$this->detectMethodVariadic($parameters),
);
}
foreach ($classReflection->getTraits() as $traitClass) {
$methodWithDeclaringClass = $this->findClassReflectionWithMethod($traitClass, $classReflection, $methodName);
if ($methodWithDeclaringClass === null) {
continue;
}
return $methodWithDeclaringClass;
}
foreach ($classReflection->getParents() as $parentClass) {
$methodWithDeclaringClass = $this->findClassReflectionWithMethod($parentClass, $parentClass, $methodName);
if ($methodWithDeclaringClass === null) {
foreach ($parentClass->getTraits() as $traitClass) {
$parentTraitMethodWithDeclaringClass = $this->findClassReflectionWithMethod($traitClass, $parentClass, $methodName);
if ($parentTraitMethodWithDeclaringClass === null) {
continue;
}
return $parentTraitMethodWithDeclaringClass;
}
continue;
}
return $methodWithDeclaringClass;
}
foreach ($classReflection->getInterfaces() as $interfaceClass) {
$methodWithDeclaringClass = $this->findClassReflectionWithMethod($interfaceClass, $interfaceClass, $methodName);
if ($methodWithDeclaringClass === null) {
continue;
}
return $methodWithDeclaringClass;
}
return null;
}
/**
* @param AnnotationsMethodParameterReflection[] $parameters
*/
private function detectMethodVariadic(array $parameters): bool
{
if ($parameters === []) {
return false;
}
$possibleVariadicParameterIndex = count($parameters) - 1;
$possibleVariadicParameter = $parameters[$possibleVariadicParameterIndex];
return $possibleVariadicParameter->isVariadic();
}
}